戏说Android view 工作流程《上》

view基本认识

回想你第一次看到Android设备时,上面各种酷炫的应用和游戏是不是让多年使用塞班手机的你感到amazing?能看到这篇文章说明你现在的工作多少和android开发相关。或许你是跟着教程写了一个HelloWold运行在模拟器或者真机上,结果出来的瞬间会有些自豪。心想着自己也是会android应用开发的人了。

接着可能陆陆续续的写了几个小Demo,这时候对android应用的印象多了四大组件等一些概念,提到view,你可能会说,视图不就是窗口,而一个窗口往往就是一个activity。所以会有一个activity就是一个view的错觉。其实也不能说完全错了,这其中确实有玄机。

setContentView说起

为啥会让我们有activity即view的印象,原因就在于Activity一般都在onCreate函数中里使用setContentView设置UI界面。我们略作分析:

mWindow为何物?

Activity.java

  

[java]  view plain copy
  1.  /** 
  2.      * 根据布局文件来填充activity的view。 
  3.      */  
  4.     public void setContentView(int layoutResID) {  
  5.         getWindow().setContentView(layoutResID);//我们需要看下getWindow()返回的是什么。  
  6. }  
  7. /** 
  8.      * Retrieve the current {@link android.view.Window} for the activity. 
  9.      * This can be used to directly access parts of the Window API that 
  10.      * are not available through Activity/Screen. 
  11.      *  
  12.      * @return Window The current window, or null if the activity is not 
  13.      *         visual. 
  14.      */  
  15.     public Window getWindow() {  
  16.         return mWindow;// 是window的一个实例  
  17.     }  

mWindow是何方神圣,又是谁创造了它,我们control+F一下在attach函数中找到了它mWindow =PolicyManager.makeNewWindow(this),不过又多出来一个PolicyManager,这货又是干啥的,我们进去看看先。

[java]  view plain copy
  1. public final class PolicyManager {  
  2.     private static final String POLICY_IMPL_CLASS_NAME =  
  3.         "com.android.internal.policy.impl.Policy";  
  4.   
  5.     private static final IPolicy sPolicy;  
  6.   
  7.     static {  
  8.         // Pull in the actual implementation of the policy at run-time  
  9.         try {  
  10.             Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);  
  11.             sPolicy = (IPolicy)policyClass.newInstance();  
  12.         } catch (ClassNotFoundException ex) {  
  13.             throw new RuntimeException(  
  14.                     POLICY_IMPL_CLASS_NAME + " could not be loaded", ex);  
  15. 。。。。。。  
  16. // The static methods to spawn new policy-specific objects  
  17.     public static Window makeNewWindow(Context context) {  
  18.         return sPolicy.makeNewWindow(context);  
  19.     }  
  20. }  


该类代码比较少,申明了一个Policy类型的单例对象,我们再看看它是神马。

[java]  view plain copy
  1. public PhoneWindow makeNewWindow(Context context) {  
  2.         return new PhoneWindow(context);//亲,注意啦,出现关键词PhoneWindow。  
  3. }  

到这里我们弄清楚了一个事实,mWindow就是一个PhoneWindow对象同样的手法,我们可以知道mWindowManager成员变量的真实类型是LocalWindowManager。

深入setContentView

弄清楚了以上概念,我们可以重新回到setContenView函数了。根据上面的结论,直接到PhoneWindow去找它。

  

[java]  view plain copy
  1. public void setContentView(View view, ViewGroup.LayoutParams params) {  
  2.        if (mContentParent == null) {  
  3.            installDecor();  
  4.        } else {  
  5.            mContentParent.removeAllViews();  
  6.        }  
  7.        mContentParent.addView(view, params);  
  8.        final Callback cb = getCallback();  
  9.        if (cb != null) {  
  10.            cb.onContentChanged();  
  11.        }  
  12.    }  

可以知道mContentParent是viewGroup类型,它存在时就负责把view加载出来,不存在的时候会走installDecor方法。

    

[java]  view plain copy
  1.  private void installDecor() {  
  2.         if (mDecor == null) {  
  3.             mDecor = generateDecor();//创建mDecor,它是DecorView类型,继承于FrameLayout。  
  4.             mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);  
  5.             mDecor.setIsRootNamespace(true);  
  6.         }  
  7.         if (mContentParent == null) {  
  8.             mContentParent = generateLayout(mDecor);  
  9.             //创建标题栏  
  10.             mTitleView = (TextView)findViewById(com.android.internal.R.id.title);  
  11.           ......  
  12.         }  
  13. }  

这里需要我们进一步看下generateDecor()方法:

[java]  view plain copy
  1. protected ViewGroup generateLayout(DecorView decor) {  
  2.         // Apply data from current theme.  
  3. //1,根据getWindowStyle()返回的数组来设定一些窗口属性值feature,如是否全屏,是否带标题栏。  
  4.         TypedArray a = getWindowStyle();  
  5.         mIsFloating = a.getBoolean(com.android.internal.R.styleable.Window_windowIsFloating, false);  
  6.         int flagsToUpdate = (FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR)  
  7.                 & (~getForcedWindowFlags());  
  8.         if (mIsFloating) {  
  9.             setLayout(WRAP_CONTENT, WRAP_CONTENT);  
  10.             setFlags(0, flagsToUpdate);  
  11.         } else {  
  12.             setFlags(FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR, flagsToUpdate);  
  13.         }  
  14.   
  15.         if (a.getBoolean(com.android.internal.R.styleable.Window_windowNoTitle, false)) {  
  16.             requestFeature(FEATURE_NO_TITLE);  
  17.         }  
  18.   
  19.         if (a.getBoolean(com.android.internal.R.styleable.Window_windowFullscreen, false)) {  
  20.             setFlags(FLAG_FULLSCREEN, FLAG_FULLSCREEN&(~getForcedWindowFlags()));  
  21.         }  
  22.   
  23.         if (a.getBoolean(com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {  
  24.             setFlags(FLAG_SHOW_WALLPAPER, FLAG_SHOW_WALLPAPER&(~getForcedWindowFlags()));  
  25.         }  
  26.         ......  
  27.         // Inflate the window decor.  
  28. //2,根据上面设定的features值,决定加载何种窗口布局文件。  
  29.         int layoutResource;  
  30.         int features = getLocalFeatures();  
  31.         // System.out.println("Features: 0x" + Integer.toHexString(features));  
  32.         if ((features & ((1 << FEATURE_LEFT_ICON) | (1 << FEATURE_RIGHT_ICON))) != 0) {  
  33.             if (mIsFloating) {  
  34.                 layoutResource = com.android.internal.R.layout.dialog_title_icons;  
  35.             } else {  
  36.                 layoutResource = com.android.internal.R.layout.screen_title_icons;  
  37.             }  
  38.         ......  
  39.         }  
  40.         mDecor.startChanging();  
  41. //3,把特定的view添加到decorView里。  
  42.         View in = mLayoutInflater.inflate(layoutResource, null);  
  43.         decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));  
  44. //4,这个contentParent由findViewById返回,实际上就是mDecorView一部分。  
  45.         ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);  
  46.         ......  
  47.         mDecor.finishChanging();  
  48.         return contentParent;  
  49. }  

窗口布局文件解析

上面第二步提到根据上面设定的features值,决定加载何种窗口布局文件,我们找一个系统窗口布局文件分析一下:

路径在:.frameworks/base/core/res/layout/screen_title.xml 

[java]  view plain copy
  1. <!--  
  2. This is an optimized layout for a screen, with the minimum set of features  
  3. enabled.  
  4. -->  
  5.   
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  7.     android:orientation="vertical"  
  8.     android:fitsSystemWindows="true">  
  9.     <FrameLayout  
  10.         android:layout_width="match_parent"   
  11.         android:layout_height="?android:attr/windowTitleSize"  
  12.         style="?android:attr/windowTitleBackgroundStyle">  
  13.         <TextView android:id="@android:id/title"   
  14.             style="?android:attr/windowTitleStyle"  
  15.             android:background="@null"  
  16.             android:fadingEdge="horizontal"  
  17.             android:gravity="center_vertical"  
  18.             android:layout_width="match_parent"  
  19.             android:layout_height="match_parent" />  
  20.     </FrameLayout>  
  21.     <FrameLayout android:id="@android:id/content"  
  22.         android:layout_width="match_parent"   
  23.         android:layout_height="0dip"  
  24.         android:layout_weight="1"  
  25.         android:foregroundGravity="fill_horizontal|top"  
  26.         android:foreground="?android:attr/windowContentOverlay" />  
  27. </LinearLayout>  

 可以看到是一个简单的布局文件,一个LinearLayout下包含了两个子FrameLayout视图,第一个FrameLayout用来显示标题栏(TitleBar);第二个FrameLayout用来显示id为content视图。

穿越回顾一下view结构


以上我们由setcontentView()引出了两个在view界举足轻重的大佬PhoneWindow和DecorView,它们之间的关系可以简单用下面的图来表示。

        

总结

举个不太恰当的类比:如果说PhoneWindow是画,一种抽象概念的画,那DecorView会更具体一点,它可以是山水画。而MyView就是具体的唐伯虎手下的凤凰傲意图了。

这里说的View、DecorView等都是UI单元,这些UI单元工作都在onDraw函数中完成。如果把onDraw想象成画图过程,那我们需要知道画布是什么?查阅资料后,得出答案就是Surface,关于它的前世今生还需看一下源码ViewRoot.java。

[java]  view plain copy
  1. public final class ViewRoot extends Handler implements ViewParent,  
  2.         View.AttachInfo.Callbacks {  
  3.     View mView;  
  4. private final Surface mSurface = new Surface();  
  5.  }  

以上我们收集齐了PhoneWindow,DecorView,ViewRoot,Surface四颗神奇的龙珠,让我们一起把view召唤到手机屏幕上吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值