Android Window 整理

来源:互联网 发布:歌词软件 乐 编辑:程序博客网 时间:2024/06/07 10:41

首先,Window是一个窗体的概念。创建一个Window很简单,通过WindowManager来完成。

mButton =new Button(this);mLayoutParams=new WindowManager.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0,0,PixelFormat.TRANSPARENT);mLayoutParams.flags=LayoutParams.FLAG_NOT_TOUCH_MOAL|LayoutParams.FLAG_NOT_FOCUSBLE);mLayoutParams.X=100;mLayoutParams.y=300;mWindowManager.addView(mButton,mLayoutParams);

上述代码就是向一个Wwindow里添加一个View;

向Window中添加一个View有一个很重要的属性LayoutParams.FLAG :

所有Window flag如下:
int FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
Window flag: as long as this window is visible to the user, allow the lock screen to activate while the screen is on.
当该window对用户可见的时候,允许锁屏。

int FLAG_ALT_FOCUSABLE_IM
Window flag: invert the state of FLAG_NOT_FOCUSABLE with respect to how this window interacts with the current method.

int FLAG_BLUR_BEHIND
Window flag: blur everything behind this window.
让该window后所有东西都模糊(blur)

int FLAG_DIM_BEHIND
Window flag: everything behind this window will be dimmed.
让该window后所有的东西都成暗淡(dim)

int FLAG_DISMISS_KEYGUARD
Window flag: when set the window will cause the keyguard to be dismissed,
only if it is not a secure lock keyguard.

int FLAG_DITHER
Window flag: turn on dithering when compositing this window to the screen.
开启抖动(dithering)
int FLAG_FORCE_NOT_FULLSCREEN

Window flag: Override {@link #FLAG_FULLSCREEN and force the screen decorations (such as status bar) to be shown.
恢复window非全屏显示 (让背景不是暗淡的)

int FLAG_FULLSCREEN
Window flag: Hide all screen decorations (e.g.
让window进行全屏显示

int FLAG_HARDWARE_ACCELERATED
Indicates whether this window should be hardware accelerated.
对该window进行硬件加速.
该flag必须在设置你的Activity或Dialog的Content View之前进行设置,
而且如果你在mainfest文件中用android:hardwareAccelerated开启了该属性的话,那么你在程序中就不能再改变它。mainfest文件中android:hardwareAccelerated属性默认是开启的(“true”)。

int FLAG_IGNORE_CHEEK_PRESSES
Window flag: intended for windows that will often be used when the user is holding the screen against their face, it will aggressively filter the event stream to prevent unintended presses in this situation that may not be desired for a particular window, when such an event stream is detected, the application will receive a CANCEL motion event to indicate this so applications can handle this accordingly by taking no action on the event until the finger is released.

int FLAG_KEEP_SCREEN_ON
Window flag: as long as this window is visible to the user, keep the device’s screen turned on and bright.
当该window对用户可见时,让设备屏幕处于高亮(bright)状态。

int FLAG_LAYOUT_INSET_DECOR
Window flag: a special option only for use in combination with

int FLAG_LAYOUT_IN_SCREEN Window flag: place the window within the entire screen, ignoring decorations around the border (a.k.a.
让window占满整个手机屏幕,不留任何边界(border)

int FLAG_LAYOUT_NO_LIMITS
Window flag: allow window to extend outside of the screen.
window大小不再不受手机屏幕大小限制,即window可能超出屏幕之外,这时部分内容在屏幕之外。

int FLAG_NOT_FOCUSABLE
Window flag: this window won’t ever get key input focus, so the user can not send key or other button events to it.
让window不能获得焦点,这样用户快就不能向该window发送按键事件及按钮事件

int FLAG_NOT_TOUCHABLE
Window flag: this window can never receive touch events.
让该window不接受触摸屏事件

int FLAG_NOT_TOUCH_MODAL
Window flag: Even when this window is focusable (its {@link #FLAG_NOT_FOCUSABLE is not set),
allow any pointer events outside of the window to be sent to the windows behind it.
即使在该window在可获得焦点情况下,仍然把该window之外的任何event发送到该window之后的其他window.

int FLAG_SCALED
Window flag: a special mode where the layout parameters are used to perform scaling of the surface when it is composited to the screen.
int FLAG_SECURE Window flag: don’t allow screen shots while this window is displayed.
当该window在进行显示的时候,不允许截屏。

int FLAG_SHOW_WALLPAPER
Window flag: ask that the system wallpaper be shown behind your window.
在该window后显示系统的墙纸(wallpaper)
int FLAG_SHOW_WHEN_LOCKED Window flag: special flag to let windows be shown when the screen is locked.
当锁屏的时候,显示该window.

int FLAG_SPLIT_TOUCH
Window flag: when set the window will accept for touch events outside of its bounds to be sent to other windows that also support split touch. When this flag is not set, the first pointer that goes down determines the window to which all subsequent touches go until all pointers go up. When this flag is set, each pointer (not necessarily the first) that goes down determines the window to which all subsequent touches of that pointer will go until that pointer goes up thereby enabling touches with multiple pointers to be split across multiple windows
当该window在可以接受触摸屏情况下,让因在该window之外,而发送到后面的window的触摸屏可以支持split touch.

int FLAG_TOUCHABLE_WHEN_WAKING
Window flag: When set, if the device is asleep when the touch screen is pressed, you will receive this first touch event.
当手机处于睡眠状态时,如果屏幕被按下,那么该window将第一个收到到事件

int FLAG_TURN_SCREEN_ON
Window flag: when set as a window is being added or made visible, once the window has been shown then the system will poke the power manager’s user activity (as if the user had woken up the device) to turn the screen on.
当然window被显示的时候,系统将把它当做一个用户活动事件,以点亮手机屏幕。

int FLAG_WATCH_OUTSIDE_TOUCH
Window flag: if you have set FLAG_NOT_TOUCH_MODAL, you can set this flag to receive a single special MotionEvent with the action MotionEvent.ACTION_OUTSIDE
for touches that occur outside of your window.
如果你设置了该flag,那么在你FLAG_NOT_TOUNCH_MODAL的情况下,即使触摸屏事件发送在该window之外,其事件被发送到了后面的window,那么该window仍然将以MotionEvent.ACTION_OUTSIDE形式收到该触摸屏事件


WindowType

Window 有3种类型,分别是:应用Window、子Window、系统Window。
应用Winow对应Acitivity
子Window对应 Dialog等
系统Window需要权限,对应Toast、状态栏等

Windowmanager主要是3个方法

addview
updateview
removeview


Window 是一个抽象的概念,每一个window都对应着一个View和一个ViewRootImpl,Window和View通过ViewrootImpl来建立联系。因此Window并不是实际存在的,它是以View的形式存在。

Window的3大操作,并不是在Windowmanagerimpl中实现的

public void addView(View view,ViewGroup.LayoutParams params){    mGlobal.addView(view,params,mDisplay,mParentWindow);}......

Window的3大操作实际上都是通过WindowManagerGlobal来实现。

WindowManagerGlobal 内部有几个比较重要的列表。

ArrayList<View> mViews;ArrayList<ViewRootImpl> mRoots;ArrayList<WindowManager.LayouParams> mParams;ArraySet<View> mDyingViews;

mViews 存储所有Window对应的view
mRoots存储所有Window对应的ViewRootImpl
mParams 存储了所有Window对应的布局参数
mDyingViews 存储了正在被删除,还没被删除完成的view

Activity的Window

在Activity的attach方法里,系统会创建Actiivity所属window对象,并且为他设置回调接口。所有对window的操作都会通过Callback接口回调到Activity来。创建一个Window调用了

public Window makeNewWindow(Context context){return new PhoneWindow(context)}

setContentView

DecorView 是Activity中顶级的View。它实际上是一个FrameLayout。它包括2个部分,一个是标题栏,一个是内容栏。标题栏会随着主题的改变而改变。而内容栏必须要存在,它有一个id:android.R.id.content.
创建一个Activity的步骤有以下几个:

  • 1.如果没有DecorView,创建DecorView。
protected DecorView generateDecor(){retrun new DecorView(getContext(),-1);}

为了初始化DecorView的结构,PhoneWindow还会具体的布局文件加载到DecorView中来,具体的布局文件跟主题有关。

View in =mLayoutInflater.inflate(layoutResource,null);DecorView.addView(in,new ViewGroup.LayoutParams(MATCH_PARENT,MATCH_PARENT));mContentRoot=(ViewGroup)in;ViewGroup contentParent =(ViewGroup) findViewById(ID_ANDROID_CONTENT);
  • 2.将View添加到DecorView的mContentParent
    步骤1中已经初始化了DecorView,所以这一步可以直接将布局文件加载到DecorView中的ContentParant中
mLayoutInflater.inflate(layoutid,mContentParent);

所以叫setContentView!!而不是setView,非常的形象。

  • 3.回调Activity的onContentChanged方法通知Activity视图已经发生了改变。
    由于Activity实现了Window的Callback接口,所以Activity可以收到通知。
  • 4.添加DecorView到Window中

ActivityThread 的handleResumeActivity方法中,首先会调用Activity的onResume()方法,接着调用makeVisible()。在makeVisible这个方法中,DecorView完成了添加和显示的操作。

if(!mWindowAdded){    ViewManager wm=getWindowManaer();    wm.addView(mDecor,getWindow().getAttributtes());    mWindowAdded=ture;    }    mDecor.setVisibility(View.Visible);    }

Dialog的Window创建过程和Activity类似。

0 0
原创粉丝点击