Android中onMesure研究

                                                                      

         当一个Activity取得焦点后就会向Android系统请求绘制它的布局。Android框架会处理这个绘制的过程,一个View的显示,需要先后调用onMeasure,onLayout和onDraw方法。从字面意思理解onMeasure,为计算,测量。上图所示,A,B,C分别表示为三个View,其中,A View包含B View,B View 又包含C View。这三个View在屏幕上显示出来, 会调用每一个View中的onMeasure方法,其调用的顺序是怎样的呢?A ----> B---->C 还是神马呢???先上代码吧。分别为A,B,C三个类源码码。                                                                                    

      

[java]  view plain copy
  1. <span style="color:#330033;">public class LayoutA extends LinearLayout{  
  2.       
  3.     private String TAG = LayoutA.class.getSimpleName();  
  4.       
  5.     public LayoutA(Context context) {  
  6.         super(context);  
  7.         // TODO Auto-generated constructor stub  
  8.     }  
  9.   
  10.     public LayoutA(Context context, AttributeSet attrs) {  
  11.         super(context, attrs);  
  12.         // TODO Auto-generated constructor stub  
  13.     }  
  14.       
  15.     @Override  
  16.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  17.         // TODO Auto-generated method stub  
  18.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  19.         int widthMode = MeasureSpec.getMode(widthMeasureSpec);  
  20.         int heightMode = MeasureSpec.getMode(heightMeasureSpec);  
  21.           
  22.         int widthSize = MeasureSpec.getSize(widthMeasureSpec);  
  23.         int heightSize = MeasureSpec.getSize(heightMeasureSpec);  
  24.         if(widthMode == MeasureSpec.AT_MOST){  
  25.             Log.e(TAG,"LayoutA onMeasure() called,widthMode = MeasureSpec.AT_MOST,widthSize = " + widthSize + " ,heightSize = " + heightSize);  
  26.         }  
  27.           
  28.         if(widthMode == MeasureSpec.EXACTLY){  
  29.             Log.e(TAG,"LayoutA onMeasure() called,widthMode = MeasureSpec.EXACTLY,widthSize = " + widthSize + " ,heightSize = " + heightSize);  
  30.         }  
  31.           
  32.         if(widthMode == MeasureSpec.UNSPECIFIED){  
  33.             Log.e(TAG,"LayoutA onMeasure() called,widthMode = MeasureSpec.UNSPECIFIED,widthSize = " + widthSize + " ,heightSize = " + heightSize);  
  34.         }  
  35.           
  36.         if(heightMode == MeasureSpec.AT_MOST){  
  37.               
  38.         }  
  39.     }  
  40.   
  41.     @Override  
  42.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  43.         // TODO Auto-generated method stub  
  44.         super.onLayout(changed, l, t, r, b);  
  45. //      Log.e(TAG,"  LayoutA  onLayout() called...");  
  46.     }  
  47.   
  48.   
  49.     @Override  
  50.     protected void onDraw(Canvas canvas) {  
  51.         // TODO Auto-generated method stub  
  52.         super.onDraw(canvas);  
  53. //      Log.e(TAG,"  LayoutA  onDraw() called...");  
  54.     }  
  55.   
  56. }</span>  

    ... ... ... ...

my_main.xml文件为:

[html]  view plain copy
  1. <com.example.measuretest.LayoutA xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"  
  6.     android:gravity="center"  
  7.     android:background="#ffff0000">  
  8.      <com.example.measuretest.LayoutB  
  9.         android:layout_width="300dip"  
  10.         android:layout_height="300dip"  
  11.         android:gravity="center"  
  12.         android:layout_marginTop="40dip"  
  13.         android:background="#ff00ff00">  
  14.         <com.example.measuretest.LayoutC  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:layout_gravity="center"  
  18.             android:text="@string/hello_world">  
  19.         </com.example.measuretest.LayoutC>  
  20.      </com.example.measuretest.LayoutB>  
  21. </com.example.measuretest.LayoutA>  

类LayoutB与LayoutA相似,LayoutC的代码为:

[java]  view plain copy
  1. public class LayoutC extends Button{  
  2.       
  3.     private String TAG = LayoutC.class.getSimpleName();  
  4.     private float mRadius = 4;  
  5.       
  6.     public LayoutC(Context context) {  
  7.         super(context);  
  8.         // TODO Auto-generated constructor stub  
  9.     }  
  10.   
  11.     public LayoutC(Context context, AttributeSet attrs) {  
  12.         super(context, attrs);  
  13.         // TODO Auto-generated constructor stub  
  14.     }  
  15.       
  16.     @Override  
  17.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  18. ... ... ... ... ...  


运行后,log信息为:

[plain]  view plain copy
  1. 08-20 21:15:53.338: E/LayoutC(2377): LayoutC onMeasure() called,widthMode = MeasureSpec.AT_MOST,widthSize = 450 ,heightSize = 450  
  2. 08-20 21:15:53.338: E/LayoutB(2377): LayoutB onMeasure() called,widthMode = MeasureSpec.EXACTLY,widthSize = 450 ,heightSize = 450  
  3. 08-20 21:15:53.338: E/LayoutA(2377): LayoutA onMeasure() called,widthMode = MeasureSpec.EXACTLY,widthSize = 480 ,heightSize = 778  
  4. 08-20 21:15:53.370: E/LayoutC(2377): LayoutC onMeasure() called,widthMode = MeasureSpec.AT_MOST,widthSize = 450 ,heightSize = 450  
  5. 08-20 21:15:53.370: E/LayoutB(2377): LayoutB onMeasure() called,widthMode = MeasureSpec.EXACTLY,widthSize = 450 ,heightSize = 450  
  6. 08-20 21:15:53.370: E/LayoutA(2377): LayoutA onMeasure() called,widthMode = MeasureSpec.EXACTLY,widthSize = 480 ,heightSize = 778  
  7. 08-20 21:15:53.424: E/LayoutC(2377): LayoutC onMeasure() called,widthMode = MeasureSpec.AT_MOST,widthSize = 450 ,heightSize = 450  
  8. 08-20 21:15:53.424: E/LayoutB(2377): LayoutB onMeasure() called,widthMode = MeasureSpec.EXACTLY,widthSize = 450 ,heightSize = 450  
  9. 08-20 21:15:53.424: E/LayoutA(2377): LayoutA onMeasure() called,widthMode = MeasureSpec.EXACTLY,widthSize = 480 ,heightSize = 778  


从log信息显示为:先调用LayoutC 的onMeasure 方法,而后是LayoutB,最后才是调用LayoutA方法的onMeasure方法。

        绘制View有两个过程,一个messure过程和一个layout过程。  messure过程只通过measure(int, int)方法中实现的,messure过程会自顶向下遍历Viewtree。在递归过程中,每个View都设置自己的尺寸规格。在measure过程的最后,每个View都已经存储了它自身的大小了。

        由于是递归调用,所以当然是从最里层的view开始调用其onMeasure方法,计算其自身大小,然后调用次外层View 的onMeasure方法。也就是说,上图中A,B,C的调用顺序为:C------>B------->A

(关于measure的调用,请参看Google文档:http://developer.android.com/guide/topics/ui/how-android-draws.html

      接着对上篇文章进行谈论。Android中onMesure研究(1) 。上一篇文章介绍,每一个View在显示的时候,对onMeasure方法的调用,是从最里层的View开始measure。从数据结构上讲述,是对二叉树最外层的儿子(View)开始measure。上篇博文提到是因为递归调用。

      在一个Activity中,调用SetContentView后,最终就会触发ViewRoot中的scheduleTraversals这个函数。

[java]  view plain copy
  1. public void scheduleTraversals() {  
  2.     if (!mTraversalScheduled) {  
  3.         mTraversalScheduled = true;  
  4.         sendEmptyMessage(DO_TRAVERSAL);  
  5.     }  
  6. }  

       该函数会发送一个DO_TRAVERSAL消息,在handleMessage中处理,调用到performTraversals函数,这个函数涉及到measure和onLayout方法。该函数有600行,并且是一个递归函数。     

[java]  view plain copy
  1. private void performTraversals() {  
  2.        // cache mView since it is used so much below...  
  3.        final View host = mView;  
  4.        if (DBG) {  
  5.            System.out.println("======================================");  
  6.            System.out.println("performTraversals");  
  7.            host.debug();  
  8.        }  
  9.        if (host == null || !mAdded)  
  10.            return;  
  11.   
  12.        mTraversalScheduled = false;  
  13.        mWillDrawSoon = true;  
  14.     ... ... ...   
  15.     host.measure(childWidthMeasureSpec, childHeightMeasureSpec);  
  16.     ... ... ...   
  17.     host.layout(00, host.mMeasuredWidth, host.mMeasuredHeight);  
  18.     } else {  
  19.            // We were supposed to report when we are done drawing. Since we canceled the  
  20.            // draw, remember it here.  
  21.            if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {  
  22.                mReportNextDraw = true;  
  23.            }  
  24.            if (fullRedrawNeeded) {  
  25.                mFullRedrawNeeded = true;  
  26.            }  
  27.            // Try again  
  28.            scheduleTraversals();  
  29. }  
  30.    }  
       从上述代码中,我们可以看到,onLayout方法的调用,与onMeasure调用顺序是一致的。 接下来,我们开始研究View类中的measure和layout方法了。 
          
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值