zoukankan      html  css  js  c++  java
  • Android中悬浮窗口的实现原理和示例代码

    用了我一个周末的时间,个中愤懑就不说了,就这个问题,我翻遍全球网络没有一篇像样的资料,现在将实现原理简单叙述如下:
    
    调用WindowManager,并设置WindowManager.LayoutParams的相关属性,通过WindowManager的addView方法创建View,这样产生出来的View根据WindowManager.LayoutParams属性不同,效果也就不同了。比如创建系统顶级窗口,实现悬浮窗口效果!
    
    WindowManager的方法很简单,基本用到的就三个addView,removeView,updateViewLayout。
    
    而WindowManager.LayoutParams的属性就多了,非常丰富,具体请查看SDK文档。这里给出Android中的WindowManager.java源码,可以具体看一下。
    
    下面是简单示例代码:
    
    public class myFloatView extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button bb=new Button(getApplicationContext());
            WindowManager wm=(WindowManager)getApplicationContext().getSystemService("window");
            WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
    
            /**
             *以下都是WindowManager.LayoutParams的相关属性
             * 具体用途请参考SDK文档
             */
            wmParams.type=2002;   //这里是关键,你也可以试试2003
            wmParams.format=1;
             /**
             *这里的flags也很关键
             *代码实际是wmParams.flags |= FLAG_NOT_FOCUSABLE;
             *40的由来是wmParams的默认属性(32)+ FLAG_NOT_FOCUSABLE(8)
             */
            wmParams.flags=40;
            wmParams.width=40;
            wmParams.height=40;
            wm.addView(bb, wmParams);  //创建View
        }
    }
    别忘了在AndroidManifest.xml中添加权限:
    
    PS:这里举例说明一下type的值的意思:
    
            /**
             * Window type: phone.  These are non-application windows providing
             * user interaction with the phone (in particular incoming calls).
             * These windows are normally placed above all applications, but behind
             * the status bar.
             */
            public static final int TYPE_PHONE              = FIRST_SYSTEM_WINDOW+2;
    
            /**
             * Window type: system window, such as low power alert. These windows
             * are always on top of application windows.
             */
            public static final int TYPE_SYSTEM_ALERT       = FIRST_SYSTEM_WINDOW+3;
    这个FIRST_SYSTEM_WINDOW的值就是2000。2003和2002的区别就在于2003类型的View比2002类型的还要top,能显示在系统下拉状态栏之上!
    
    ————————————————————————————-
    
    已经给出可自由移动悬浮窗口的Demo,参加这里。

     http://blog.csdn.net/pipisky2006/article/details/6336845

  • 相关阅读:
    oracle update join
    ResultSet转换为List的方法
    oracle创建用户操作
    ArcGIS打开工具无响应
    当前不会命中断点。源代码与原始版本不同
    使用Skin Editor自定义主题皮肤
    Spring boot项目设置加载静态资源的路径(spring.resources.static-locations)
    linux下文件的复制、移动与删除
    【珍藏】清华大学状元笔记 (初中+高中)全套高清笔记电子版 PDF
    【珍藏】全付费 (共204本)图灵程序设计丛书 高清电子书 带书签 PDF
  • 原文地址:https://www.cnblogs.com/yaowen/p/4912232.html
Copyright © 2011-2022 走看看