弹出PopupWindow使屏幕变暗的效果
来源:互联网 发布:windows驱动开发培训 编辑:程序博客网 时间:2024/04/26 01:38
弹出PopupWindow使屏幕变暗的效果
第一种
先上效果图
首先初始化你的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里面是这样的
然后在代码里面启动PopupWindow就行了,效果是这样的
0 0
- 弹出PopupWindow使屏幕变暗的效果
- Android 动画效果弹出PopupWindow,同时屏幕背景变暗
- Android 弹出popupWindow 屏幕变暗
- PopupWindow弹出后其他地方变暗的效果如何实现
- PopupWindow弹出后屏幕其他部分变暗
- PopupWindow弹出时底层变暗(像Dialog弹出时的效果)
- Android PopupWindow弹出窗口的完美实现(实现弹出背景变暗效果)
- 弹出PopupWindow背景变暗的实现
- 动画弹出PopupWindow并使背景变暗
- 动画弹出PopupWindow并使背景变暗
- 弹出popupwindow背景变暗
- PopupWindow弹出选择框,其他部分变暗的效果的实现
- PopupWindow弹出框与背景变暗的实现(附带动画效果)
- 点击PopUpWindow出现背景变暗的效果
- PopupWindow设置其他Activity变暗的效果
- Android实现底部弹出PopupWindow背景逐渐变暗效果
- PopupWindow屏幕自动变暗半透明
- android中popupwindow的实现,弹出后页面变暗
- 当你输入网址到网站出现中间经历了什么?
- linux 安装配置java环境 jdk1.6 jdk-6u45-linux-x64.bin
- java-String类
- 数论专题 T3
- MySQL Binary Log
- 弹出PopupWindow使屏幕变暗的效果
- 用@ExceptionHandler 来进行异常处理
- NATLAB中矩阵乘法与矩阵点乘(对应位相乘)
- Hadoop-2.7.3集群(HDFS)搭建
- ZCMU-1231-Antimonotonicity
- 清新脱俗的网页设计,给你不一样的视觉感受
- Linux下安装和配置Git
- hadoop自带示例wordcount
- leetcode 325 Maximum Size Subarray Sum Equals k