Android Popupwindow弹框后,背景变暗实现方法

项目中使用到了popupwindow,现在没有让popupwindow占满手机屏幕,而是从手机底部弹出,popupwindow的高度是手机屏幕的0.618(黄金比例)。基于这个前提下,当popupwindow创建完成并显示后,将popupwindow的背景变暗;当popupwindow消失时,恢复背景亮度。这样做可以提高用户体验。

/**
     * 设置窗口的背景透明度
     * @param f 0.0-1.0
     */
    private  void bgAlpha(float f){
        WindowManager.LayoutParams layoutParams = getActivity().getWindow().getAttributes();
        layoutParams.alpha = f;
        getActivity().getWindow().setAttributes(layoutParams);
    }

给出我在项目中的使用该方法的图示

private void showPopupWindow(View view) {

        // 一个自定义的布局,作为显示的内容
        View contentView = LayoutInflater.from(MyApplication.getContext()).inflate(
                R.layout.pop_no_buy, null);
        // 设置按钮的点击事件


        TextView tvExhibitionChineseShortName = contentView.findViewById(R.id.tv_exhibitionChineseShortName);
        ImageView imgLogo = contentView.findViewById(R.id.img_logo);
        TextView tvTime = contentView.findViewById(R.id.tv_time);
        TextView tvExhibitionHallChineseName = contentView.findViewById(R.id.tv_exhibitionHallChineseName);
        TextView tvTicketPrice = contentView.findViewById(R.id.tv_ticketPrice);

        ImageView imgWechatPay = contentView.findViewById(R.id.img_wechat_pay);
        ImageView imgAliPay = contentView.findViewById(R.id.img_ali_pay);



        MyPreferences myPreferences = new MyPreferences(MyApplication.getContext());
        String exhibitionChineseShortName = myPreferences.getActivityChineseName();
        String logo = myPreferences.getLogo();
        String activeAddress = myPreferences.getActiveAddress();
        String time = myPreferences.getTime();


       /* //1 发起费用
        String openActivitiesTicketPrice = String.valueOf(signInformationBean.getData().getOpenActivitiesTicketPrice());
        myPreferences.setOpenActivitiesTicketPrice(openActivitiesTicketPrice);

        //2 参与费用
        String participateActivityTicketPrice = String.valueOf(signInformationBean.getData().getOpenActivitiesTicketPrice());
        myPreferences.setParticipateActivityTicketPrice(participateActivityTicketPrice);

        //1 发起人是否付费
        boolean openActivitiesTicketPaymentStatus =  signInformationBean.getData().isOpenActivitiesTicketPaymentStatus();
        myPreferences.setOpenActivitiesTicketPaymentStatus(openActivitiesTicketPaymentStatus);
        //2
        boolean participateActivityTicketPaymentStatus = signInformationBean.getData().isParticipateActivityTicketPaymentStatus();
        myPreferences.setParticipateActivityTicketPaymentStatus(participateActivityTicketPaymentStatus);*/

        //1 发起人是否付费
        boolean openActivitiesTicketPaymentStatus =myPreferences.getOpenActivitiesTicketPaymentStatus();
        //2 参与者是否付费

        String ticketPrice;
        if(!openActivitiesTicketPaymentStatus){
             ticketPrice = myPreferences.getOpenActivitiesTicketPrice();
        }else {
            ticketPrice = myPreferences.getParticipateActivityTicketPrice();
        }

        tvExhibitionChineseShortName.setText(exhibitionChineseShortName);

        Glide.with(MyApplication.getContext()).load(logo).into(imgLogo);

        tvExhibitionHallChineseName.setText(activeAddress);
        tvTime.setText(time);
        tvTicketPrice.setText("¥" + ticketPrice + "元");


        imgAliPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("mengdd", "onTouch : ");
                return false;
                // 这里如果返回true的话,touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
            }
        });

        // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        // popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.selectmenu_bg_downward));

        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        bgAlpha(0.618f);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                bgAlpha(1.0f);//消失后,恢复亮度
            }
        });

        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);

    }
发布了184 篇原创文章 · 获赞 70 · 访问量 37万+

猜你喜欢

转载自blog.csdn.net/qq_31939617/article/details/96113446
今日推荐