弹出PopupWindow使屏幕变暗的效果

弹出PopupWindow使屏幕变暗的效果

第一种
先上效果图
效果图1

首先初始化你的PopupWindow
  private void initPopupMenu() {
        View popupView = getLayoutInflater().inflate(R.layout.popup_window, null);

        mPopupWindow = new PopupWindow(popupView, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));

        ibtPopupMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                } else {
                    mPopupWindow.showAsDropDown(itemDetailSplitLine);
                    setBackgroundAlpha(0.7f);
                }
            }
        });

        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1.0f);
            }
        });
    }
 public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }    

下面一行代码给窗口覆盖上一个黑幕
getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,WindowManager.LayoutParams.FLAG_DIM_BEHIND);

然后再给窗口设置一下透明度
lp.alpha = bgAlpha;

让黑幕透过窗口,这跟ps中的图层叠加的效果相似,用过ps的朋友都知道吧。最后把属性设置到窗口上面去,就完成效果了

可能有的朋友想要除了toolbar以外的区域变暗,其实也很简单,利用图层叠加的原理,我们在PopupWindow的布局文件里加上一个暗灰色背景的view就可以了,如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<LinearLayout
    android:orientation="horizontal" android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="wrap_content">
    <TextView
        android:gravity="center"
        android:layout_weight="1"
        android:text="分享"
        android:drawableTop="@mipmap/comm_share"
        android:clickable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:gravity="center"
        android:layout_weight="1"
        android:text="搜索"
        android:drawableTop="@mipmap/icon_search_white"
        android:clickable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:gravity="center"
        android:layout_weight="1"
        android:text="首页"
        android:drawableTop="@mipmap/top_bar_right_home_btn"
        android:clickable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
    <View
        android:alpha="0.3"
        android:background="#a0000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

然后在android studio的preview里面是这样的
preview

然后在代码里面启动PopupWindow就行了,效果是这样的
这里写图片描述

猜你喜欢

转载自blog.csdn.net/u012859430/article/details/54580960
今日推荐