android的window基础介绍。

来源:互联网 发布:java基础游戏开发实例 编辑:程序博客网 时间:2024/06/02 04:09
           原来,整个 Android的窗口机制是基于一个叫做 WindowManager,这个接口可以添加view到屏幕,也可以从屏幕删除view。它面向的对象一端是屏幕,另一端就是View,直接忽略我们以前的Activity或者Dialog之类的东东。其实我们的Activity 或者Diolog底层的实现也是通过WindowManager,这个 WindowManager是全局的,整个系统就是这个唯一的东东。它是显示 View的最底层了。但是要注 意,显示出来就要销毁掉,这个是必须的,销毁其实就是一个remove


Android中悬浮窗口的实现原理和示例代码 
http://www.xsmile.net/?p=404 

调用WindowManager,并设置WindowManager.LayoutParams的相关属性,通过WindowManager的addView方法创建View,这样产生出来的View根据WindowManager.LayoutParams属性不同,效果也就不同了。比如创建系统顶级窗口,实现悬浮窗口效果! 

WindowManager的方法很简单,基本用到的就三个addView,removeView,updateViewLayout。 

而WindowManager.LayoutParams的属性就多了,非常丰富,具体请查看SDK文档。这里给出Android中的WindowManager.java源码,可以具体看一下。 
WindowManager 的源码地址: 
http://www.netmite.com/android/mydroid/frameworks/base/core/java/android/view/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();
        wmParams.type=2002;  //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
    }
}

 

PS:本代码在Android2.3下测试无错(从API Level来看,实际上android1.5都可行)!另外别忘了在AndroidManifest.xml文件中加入如下权限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

关于代码中wmParams.type的值的问题,下面给出一些数值参考: 
/** 
         * Window type: the status bar.  There can be only one status bar 
         * window; it is placed at the top of the screen, and all other 
         * windows are shifted down so they are below it. 
         */ 
        public static final int TYPE_STATUS_BAR         = FIRST_SYSTEM_WINDOW; 
    
        /** 
         * Window type: the search bar.  There can be only one search bar 
         * window; it is placed at the top of the screen. 
         */ 
        public static final int TYPE_SEARCH_BAR         = FIRST_SYSTEM_WINDOW+1; 
    
        /** 
         * 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; 
        
        /** 
         * Window type: keyguard window. 
         */ 
        public static final int TYPE_KEYGUARD           = FIRST_SYSTEM_WINDOW+4; 
        
        /** 
         * Window type: transient notifications. 
         */ 
        public static final int TYPE_TOAST              = FIRST_SYSTEM_WINDOW+5; 
        
        /** 
         * Window type: system overlay windows, which need to be displayed 
         * on top of everything else.  These windows must not take input 
         * focus, or they will interfere with the keyguard. 
         */ 
        public static final int TYPE_SYSTEM_OVERLAY     = FIRST_SYSTEM_WINDOW+6; 
        
        /** 
         * Window type: priority phone UI, which needs to be displayed even if 
         * the keyguard is active.  These windows must not take input 
         * focus, or they will interfere with the keyguard. 
         */ 
        public static final int TYPE_PRIORITY_PHONE     = FIRST_SYSTEM_WINDOW+7; 
        
        /** 
         * Window type: panel that slides out from the status bar 
         */ 
        public static final int TYPE_STATUS_BAR_PANEL   = FIRST_SYSTEM_WINDOW+8; 
        
        /** 
         * Window type: panel that slides out from the status bar 
         */ 
        public static final int TYPE_SYSTEM_DIALOG      = FIRST_SYSTEM_WINDOW+8; 
    
        /** 
         * Window type: dialogs that the keyguard shows 
         */ 
        public static final int TYPE_KEYGUARD_DIALOG    = FIRST_SYSTEM_WINDOW+9; 
        
        /** 
         * Window type: internal system error windows, appear on top of 
         * everything they can. 
         */ 
        public static final int TYPE_SYSTEM_ERROR       = FIRST_SYSTEM_WINDOW+10; 
        
        /** 
         * End of types of system windows. 
         */ 
        public static final int LAST_SYSTEM_WINDOW      = 2999; 

--------------------------------- 
这个FIRST_SYSTEM_WINDOW的值就是2000。2003和2002的区别就在于2003类型的View比2002类型的还要top,能显示在系统下拉状态栏之上! 

    可以看出来,2002的值的含义其实就是2000+2。数值2000的含义就是系统级窗口,2002和2003的区别就是 2003 比 2002还要更上一层!比如,type为2003的view,能显示在手机下拉状态栏之上! 

而关于flags等其他的属性请参考SDK文档 

http://www.cnblogs.com/GnagWang/archive/2011/02/06/1949569.html

http://gundumw100.javaeye.com/blog/899977

原创粉丝点击