设为首页 加入收藏

TOP

Android PopupWindow增加半透明蒙层(一)
2019-08-26 06:31:28 】 浏览:62
Tags:Android PopupWindow 增加 透明 蒙层

先看效果图:

BasePopupWindowWithMask.class

 1 package com.example.popupwindowwithmask;
 2   
 3 import android.content.Context;
 4 import android.graphics.PixelFormat;
 5 import android.graphics.drawable.ColorDrawable;
 6 import android.os.IBinder;
 7 import android.view.KeyEvent;
 8 import android.view.View;
 9 import android.view.WindowManager;
10 import android.widget.PopupWindow;
11   
12 /**
13  * Created by kk on 2017/7/22.
14  */
15   
16 public abstract class BasePopupWindowWithMask extends PopupWindow {
17  protected Context context;
18  private WindowManager windowManager;
19  private View maskView;
20   
21  public BasePopupWindowWithMask(Context context) {
22   super(context);
23   this.context = context;
24   windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
25   setContentView(initContentView());
26   setHeight(initHeight());
27   setWidth(initWidth());
28   setOutsideTouchable(true);
29   setFocusable(true);
30   setTouchable(true);
31   setBackgroundDrawable(new ColorDrawable());
32  }
33   
34  protected abstract View initContentView();
35   
36  protected abstract int initHeight();
37   
38  protected abstract int initWidth();
39   
40  @Override
41  public void showAsDropDown(View anchor) {
42   addMask(anchor.getWindowToken());
43   super.showAsDropDown(anchor);
44  }
45   
46  private void addMask(IBinder token) {
47   WindowManager.LayoutParams wl = new WindowManager.LayoutParams();
48   wl.width = WindowManager.LayoutParams.MATCH_PARENT;
49   wl.height = WindowManager.LayoutParams.MATCH_PARENT;
50   wl.format = PixelFormat.TRANSLUCENT;//不设置这个弹出框的透明遮罩显示为黑色
51   wl.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;//该Type描述的是形成的窗口的层级关系
52   wl.token = token;//获取当前Activity中的View中的token,来依附Activity
53   maskView = new View(context);
54   maskView.setBackgroundColor(0x7f000000);
55   maskView.setFitsSystemWindows(false);
56   maskView.setOnKeyListener(new View.OnKeyListener() {
57    @Override
58    public boolean onKey(View v, int keyCode, KeyEvent event) {
59     if (keyCode == KeyEvent.KEYCODE_BACK) {
60      removeMask();
61      return true;
62     }
63     return false;
64    }
65   });
66   /**
67    * 通过WindowManager的addView方法创建View,产生出来的View根据WindowManager.LayoutParams属性不同,效果也就不同了。
68    * 比如创建系统顶级窗口,实现悬浮窗口效果!
69    */
70   windowManager.addView(maskView, wl);
71  }
72   
73  private void removeMask() {
74   if (null != maskView) {
75    windowManager.removeViewImmediate(maskView);
76    maskView = null;
77   }
78  }
79   
80  @Override
81  public void dismiss() {
82   removeMask();
83   super.dismiss();
84  }
85 }

TestPopupWindow.class

  1 package com.example.popupwindowwithmask;
  2   
  3 import android.content.Context;
  4 import android.view.LayoutInflater;
  5 import android.view.View;
  6 import android.view.WindowManager;
  7   
  8 /**
  9  * Created by kk on 2017/7/22.
 10  */
 11   
 12 public class TestPopupWindow extends BasePopupWindowWithMask {
 13  private int[] mIds;
 14  private View contentView;
 15  private OnItemClickListener listener;
 16   
 17  public interface OnItemClickListener {
 18   void OnItemClick(View v);
 19  }
 20   
 21  public void setOnItemClickListener(OnItemClickListener listener) {
 22   this.listener = listener;
 23  }
 24   
 25  public TestPopupWindow(Context context, int[] mIds) {
 26   s
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【Android 安卓源码分享】影音播.. 下一篇Android native进程间通信实例-bi..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目