PopupWindowFromBottom 从底部弹出popupwindow

自定义PopupWindowFromBottom

 1 public class PopupWindowFromBottom extends PopupWindow {
 2 
 3 
 4     public PopupWindowFromBottom(Context context, final View view) {
 5         super(context);  
 6         LayoutInflater inflater = (LayoutInflater) context  
 7                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
 8 
 9         //设置按钮监听
10         //设置SelectPicPopupWindow的View
11         this.setContentView(view);
12         //设置SelectPicPopupWindow弹出窗体的宽  
13         this.setWidth(LayoutParams.FILL_PARENT);  
14         //设置SelectPicPopupWindow弹出窗体的高  
15         this.setHeight(LayoutParams.WRAP_CONTENT);  
16         //设置SelectPicPopupWindow弹出窗体可点击  
17         this.setFocusable(true);  
18         //设置SelectPicPopupWindow弹出窗体动画效果  
19         this.setAnimationStyle(R.style.PopupAnimation);
20         //实例化一个ColorDrawable颜色为半透明  
21         ColorDrawable dw = new ColorDrawable(0xb0000000);  
22         //设置SelectPicPopupWindow弹出窗体的背景  
23         this.setBackgroundDrawable(dw);  
24         //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框  
25         view.setOnTouchListener(new OnTouchListener() {
26 
27             public boolean onTouch(View v, MotionEvent event) {  
28 
29                 int height = view.findViewById(R.id.picker_cancel).getTop();
30                 int y=(int) event.getY();  
31                 if(event.getAction()==MotionEvent.ACTION_UP){  
32                     if(y<height){  
33                         dismiss();  
34                     }  
35                 }                 
36                 return true;  
37             }  
38         });  
39 
40     }  
41 
42 }

涉及到了动画

push_bottom_in.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
    android:duration="200"
    android:fromYDelta="100%p"
    android:toYDelta="0"
    />
<alpha
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="200"
    />
</set>
push_bottom_out.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >


    <translate
        android:duration="200"
        android:fromYDelta="0"
        android:toYDelta="50%p" />
 <alpha
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="200"
    />  
</set>

style.xml

1 <style name="PopupAnimation" parent="android:Animation">
2         <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
3         <item name="android:windowExitAnimation">@anim/push_bottom_out</item>
4     </style>

使用了

  if(!menuWindow.isShowing()){
            menuWindow.showAtLocation(mParentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
        }
原文地址:https://www.cnblogs.com/wangying222/p/6289398.html