自定义的PopupWindow

public class SelectPicPopupWindow extends PopupWindow {

    private TextView popup_tv_camera, popup_tv_photo, popup_tv_cancel;
    private View mMenuView;
    private Activity mActivity;
    public SelectPicPopupWindow(Activity context, View.OnClickListener itemsOnClick) {
        super(context);
        mActivity=context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.popup, null);
        popup_tv_camera = (TextView) mMenuView.findViewById(R.id.popup_tv_camera);
        popup_tv_photo = (TextView) mMenuView.findViewById(R.id.popup_tv_photo);
        popup_tv_cancel = (TextView) mMenuView.findViewById(R.id.popup_tv_cancel);
        //取消按钮
        popup_tv_cancel.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                dismiss();
            }
        });
        //设置按钮监听
        popup_tv_photo.setOnClickListener(itemsOnClick);
        popup_tv_camera.setOnClickListener(itemsOnClick);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.style_selectpopwindow_anim);
        //实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(0xb0000000);
        ColorDrawable dw = new ColorDrawable(-00000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
        backgroundAlpha(0.5f);

        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.popup_layout).getTop();
                int y=(int) event.getY();
                if(event.getAction()== MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
    /*
    * 设置添加屏幕的背景透明度
    * @param bgAlpha
    */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mActivity.getWindow().setAttributes(lp);
    }
}

popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/popup_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <TextView
        android:id="@+id/popup_tv_camera"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@android:color/white"
        android:gravity="center"
        android:text="拍照"
        android:textSize="16sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#F0F0F0"/>

    <TextView
        android:id="@+id/popup_tv_photo"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@android:color/white"
        android:gravity="center"
        android:text="从相册选择"
        android:textSize="16sp"/>

   <!-- <TextView
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@android:color/darker_gray"/>-->

    <TextView
        android:id="@+id/popup_tv_cancel"
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        android:layout_height="60dp"
        android:background="@android:color/white"
        android:gravity="center"
        android:text="取消"
        android:textSize="16sp"/>
    <!--<LinearLayout
        android:id="@+id/popup_layout"
        android:orientation="vertical"
        android:background="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/popup_tv_camera"
            android:text="拍照"
            android:background="@android:color/white"
            android:textSize="16sp"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="60dp"/>
        <TextView
            android:background="#F0F0F0"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"/>
        <TextView
            android:background="@android:color/white"
            android:id="@+id/popup_tv_photo"
            android:text="从相册选择"
            android:textSize="16sp"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="60dp"/>
        <TextView
            android:background="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="20dp"/>
        <TextView
            android:id="@+id/popup_tv_cancel"
            android:text="取消"
            android:background="@android:color/white"
            android:textSize="16sp"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="60dp"/>
    </LinearLayout>-->
</LinearLayout>

style 里面

<style name="style_selectpopwindow_anim">
        <!-- 指定显示的动画xml -->
        <item name="android:windowEnterAnimation">@anim/anim_popup_show</item>
        <!-- 指定消失的动画xml -->
        <item name="android:windowExitAnimation">@anim/anim_popup_hide</item>
    </style>

@anim/anim_popup_show

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:duration="500"
               android:fromYDelta="50%"
               android:toYDelta="0"/>
    <alpha
        android:duration="500"
        android:fromAlpha="0"
        android:toAlpha="1"/>
</set>

@anim/anim_popup_hide

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:duration="500"
               android:fromYDelta="0"
               android:toYDelta="50%"/>
    <alpha
        android:duration="500"
        android:fromAlpha="1"
        android:toAlpha="0"/>
</set>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值