参考了网上的一些代码,自己写了个类,上代码
/** * Created by Lee on 2016/2/26. */ public class CameraPopupWindow { private PopupWindow popupWindow; private Context context; public CameraPopupWindow(Context context){ this.context = context; } /** * 初始化PopupWindow * @param view */ public void initPopupWindow(View view){ popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true); popupWindow.setTouchable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable(context.getResources(), (Bitmap) null)); popupWindow.getContentView().setFocusableInTouchMode(true); popupWindow.getContentView().setFocusable(true); popupWindow.setAnimationStyle(R.style.anim_menu_bottombar); popupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0 && event.getAction() == KeyEvent.ACTION_DOWN){ if(popupWindow != null && popupWindow.isShowing()){ popupWindow.dismiss(); } return true; } return false; } }); } /** * 设置PopupWindow位于底部 * @param parent * @param view */ public void showAtBottom(View parent, View view){ popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0); popupWindow.showAsDropDown(view); } /** * dismiss PopupWindow */ public void dismiss(){ popupWindow.dismiss(); } }
style文件
<style name="anim_menu_bottombar"> <item name="android:windowEnterAnimation">@anim/menu_bottombar_in</item> <item name="android:windowExitAnimation">@anim/menu_bottombar_out</item> </style>
anim文件 menu_bottombar_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="250" android:fromYDelta="100.0%" android:toYDelta="0.0" /> </set>
menu_bottombar_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="250" android:fromYDelta="0.0" android:toYDelta="100%" /> </set>
外部调用
View popupView = getLayoutInflater().inflate(R.layout.layout_popupwindow_camera, null); cameraPopupWindow = new CameraPopupWindow(this); cameraPopupWindow.initPopupWindow(popupView);