现在的位置: 首页 > 综合 > 正文

android view 以及 viewGroup

2017年11月07日 ⁄ 综合 ⁄ 共 9940字 ⁄ 字号 评论关闭

转载自 http://blog.csdn.net/oney139/article/details/8045779

View 有一个属性为 mParent(ViewParent型)
View 有一个属性为 mLayoutParams(ViewGroup.LayoutParams型)
无论是 mParent还是mLayoutParams 都是在系统在解析 XML 时自动进行初始化的.

ViewGroup 有一个 View[] mChildren 数组,用来保存自己的孩子;ViewGroup实现了ViewManager接口,可以增加/删除 孩子;ViewGroup实现了ViewParent接口可以执行针对特定child View的一些操作.

无论是View还是ViewGroup,其重点都在 layout(确定大小和位置) 方法和 draw(确定如何画) 方法上.

每一个View都具有一个Rect区域,它的坐标是相对于其父亲的.
对于一个View

1.应该首先进行 measure(int widthMeasureSpec, int heightMeasureSpec) (用于决定大小),然后再进行layout() (用于决定位置)

2.layout(int l, int t, int r, int b) 内部调用 protected void onLayout(boolean changed, int left, int top, int right, int bottom)

3.draw(Canvas) 内部调用 onDraw(Canvas),而 onDraw 由继承View的类实现.


================================================Draw===============================================================

draw(Canvas) 的作用: 把一个View呈递到一个Canvas上,在呈递之前layout应该被设置好,draw的过程如下:

[java] view
plain
copy

  1. /* 
  2.  * Draw traversal performs several drawing steps which must be executed 
  3.  * in the appropriate order: 
  4.  * 
  5.  *      1. Draw the background 
  6.  *      2. If necessary, save the canvas' layers to prepare for fading 
  7.  *      3. Draw view's content 
  8.  *      4. Draw children 
  9.  *      5. If necessary, draw the fading edges and restore layers 
  10.  *      6. Draw decorations (scrollbars for instance) 
  11.  */  

下面是 View.draw方法中的一些摘抄

[java] view
plain
copy

  1. // Step 3, draw the content  
  2. if (!dirtyOpaque) onDraw(canvas);  
  3.   
  4. // Step 4, draw the children  
  5. dispatchDraw(canvas);  
  6.   
  7. // Step 6, draw decorations (scrollbars)  
  8. onDrawScrollBars(canvas);  

ViewGroup中的 dispatchDraw(canvas)的实现如下:

[java] view
plain
copy

  1. protected void dispatchDraw(Canvas canvas) {  
  2.     final int count = mChildrenCount;  
  3.     final View[] children = mChildren;  
  4.     int flags = mGroupFlags;  
  5.   
  6.   
  7.     if ((flags & FLAG_USE_CHILD_DRAWING_ORDER) == 0) {  
  8.         for (int i = 0; i < count; i++) {  
  9.             final View child = children[i];  
  10.             if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {  
  11.                 more |= drawChild(canvas, child, drawingTime);  
  12.             }  
  13.         }  
  14.     } else {  
  15.         for (int i = 0; i < count; i++) {  
  16.             final View child = children[getChildDrawingOrder(count, i)];  
  17.             if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {  
  18.                 more |= drawChild(canvas, child, drawingTime);  
  19.             }  
  20.         }  
  21.     }  
  22. }  
  23.   
  24. protected boolean drawChild(Canvas canvas, View child, long drawingTime) {  
  25.     return child.draw(canvas, this, drawingTime);  
  26. }  


===========================================layout===========================================================

layout(int l, int t, int r, int b) 内部调用 protected void onLayout(boolean changed, int left, int top, int right, int bottom)


===========================================measure========================================================

一个View和一个ViewGroup不一样的地方在于:

View.measure及layout是针对自己的,而ViewGroup.measure及layout是针对自己及孩子的.


mode共有三种情况,取值分别为MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY, MeasureSpec.AT_MOST。

MeasureSpec.EXACTLY是精确尺寸,当我们将控件的layout_width或layout_height指定为具体数值时如andorid:layout_width="50dip",或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。

MeasureSpec.AT_MOST是最大尺寸,当控件的layout_width或layout_height指定为WRAP_CONTENT时,控件大小一般随着控件的子空间或内容进行变化,此时控件尺寸只要不超过父控件允许的最大尺寸即可。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。

MeasureSpec.UNSPECIFIED是未指定尺寸,这种情况不多,一般都是父控件是AdapterView,通过measure方法传入的模式。

View类中的方法 public final void measure(int widthMeasureSpec, int heightMeasureSpec),那么此方法是被谁调用,且参数是如何传递的呢? 应该是被ViewGroup的measureChild

ViewGroup通过measureChild, 根据View的ViewGroup.LayoutParams, 产生一个 MeasureSpec,并把这个 MeasureSpec 传给 View.measure()
下面为ViewGroup.measureChild

[java] view
plain
copy

  1. protected void measureChild(View child, int parentWidthMeasureSpec,  
  2.         int parentHeightMeasureSpec) {  
  3.     final LayoutParams lp = child.getLayoutParams();  
  4.   
  5.     final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,  
  6.             mPaddingLeft + mPaddingRight, lp.width);  
  7.     final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,  
  8.             mPaddingTop + mPaddingBottom, lp.height);  
  9.   
  10.     child.measure(childWidthMeasureSpec, childHeightMeasureSpec);  
  11. }  

  1. public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
     //前两参数为父的元素,最后一个为子本身属性
  2.         int specMode = MeasureSpec.getMode(spec);  
  3.         int specSize = MeasureSpec.getSize(spec);  
  4.   
  5.         int size = Math.max(0, specSize - padding);  
  6.   
  7.         int resultSize = 0;  
  8.         int resultMode = 0;  
  9.   
  10.         switch (specMode) {  
  11.         // Parent has imposed an exact size on us  
/*
父viewGroup自身设置了固定的大小,例如50dp
子view若设置了自身大小20dp,则大小自然为20dp, 同时模式为exactly
      若为match_parent,则大小为父view大小50dp-padding, 同时模式为exactly
      若未指定,则为父view的大小50dp-padding, 同时模式为at_most
*/
  1.         case MeasureSpec.EXACTLY:  //父viewGroup设置的大小为50dip
  2.             if (childDimension >= 0) {  
  3.                 resultSize = childDimension;  
  4.                 resultMode = MeasureSpec.EXACTLY;  
  5.             } else if (childDimension == LayoutParams.MATCH_PARENT) {
     //父viewGroup设置的大小为match_parent
  6.                 // Child wants to be our size. So be it.  
  7.                 resultSize = size;  
  8.                 resultMode = MeasureSpec.EXACTLY;  
  9.             } else if (childDimension == LayoutParams.WRAP_CONTENT) {
      //父viewGroup设置的大小为wrap_content
  10.                 // Child wants to determine its own size. It can't be  
  11.                 // bigger than us.  
  12.                 resultSize = size;  
  13.                 resultMode = MeasureSpec.AT_MOST;  
  14.             }  
  15.             break;  
  16.   
  17.         // Parent has imposed a maximum size on us  
  18.         case MeasureSpec.AT_MOST:  
  19.             if (childDimension >= 0) {  
  20.                 // Child wants a specific size... so be it  
  21.                 resultSize = childDimension;  
  22.                 resultMode = MeasureSpec.EXACTLY;  
  23.             } else if (childDimension == LayoutParams.MATCH_PARENT) {  
  24.                 // Child wants to be our size, but our size is not fixed.  
  25.                 // Constrain child to not be bigger than us.  
  26.                 resultSize = size;  
  27.                 resultMode = MeasureSpec.AT_MOST;  
  28.             } else if (childDimension == LayoutParams.WRAP_CONTENT) {  
  29.                 // Child wants to determine its own size. It can't be  
  30.                 // bigger than us.  
  31.                 resultSize = size;  
  32.                 resultMode = MeasureSpec.AT_MOST;  
  33.             }  
  34.             break;  
  35.   
  36.         // Parent asked to see how big we want to be  
  37.         case MeasureSpec.UNSPECIFIED:  
  38.             if (childDimension >= 0) {  
  39.                 // Child wants a specific size... let him have it  
  40.                 resultSize = childDimension;  
  41.                 resultMode = MeasureSpec.EXACTLY;  
  42.             } else if (childDimension == LayoutParams.MATCH_PARENT) {  
  43.                 // Child wants to be our size... find out how big it should  
  44.                 // be  
  45.                 resultSize = 0;  
  46.                 resultMode = MeasureSpec.UNSPECIFIED;  
  47.             } else if (childDimension == LayoutParams.WRAP_CONTENT) {  
  48.                 // Child wants to determine its own size.... find out how  
  49.                 // big it should be  
  50.                 resultSize = 0;  
  51.                 resultMode = MeasureSpec.UNSPECIFIED;  
  52.             }  
  53.             break;  
  54.         }  
  55.         //把这个结果传递给子View的 measure 方法  
  56.         return MeasureSpec.makeMeasureSpec(resultSize, resultMode);  
  57.     }  


一个定制View的onMeasure方法:

[java] view
plain
copy

  1. @Override  
  2.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  3.         setMeasuredDimension(measureWidth(widthMeasureSpec),   
  4.                 measureHeight(heightMeasureSpec));  
  5.     }  
  6.       
  7.     private int measureWidth(int widthMeasureSpec) {  
  8.         int result = 0;  
  9.         int specMode = MeasureSpec.getMode(widthMeasureSpec);  
  10.         int specSize = MeasureSpec.getSize(widthMeasureSpec);  
  11.         if(MeasureSpec.EXACTLY == specMode) {  
  12.             result = specSize;  
  13.         } else {  
  14.             result = (int)mPaint.measureText(mText) + getPaddingLeft()   
  15.                         + getPaddingRight();  
  16.             if(MeasureSpec.AT_MOST == specMode) {  
  17.                 result = Math.min(result, specSize);  
  18.             }  
  19.         }  
  20.         return result;  
  21.     }  
  22.       
  23.     private int measureHeight(int heightMeasureSpec) {  
  24.         int result = 0;  
  25.         int specMode = MeasureSpec.getMode(heightMeasureSpec);  
  26.         int specSize = MeasureSpec.getSize(heightMeasureSpec);  
  27.         mAscent = (int)mPaint.ascent();  
  28.           
  29.         if(MeasureSpec.EXACTLY == specMode) {  
  30.             result = specSize;  
  31.         } else {  
  32.             result = -mAscent + (int)mPaint.descent() + getPaddingTop()   
  33.                         + getPaddingBottom();  
  34.             if(MeasureSpec.AT_MOST == specMode) {  
  35.                 result = Math.min(result, specSize);  
  36.             }  
  37.         }  
  38.         return result;  
  39.     }  

一般来说,自定义控件都会去重写View的onMeasure方法,因为该方法指定该控件在屏幕上的大小。

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

onMeasure传入的两个参数是由上一层控件传入的大小,有多种情况,重写该方法时需要对计算控件的实际大小,然后调用setMeasuredDimension(int, int)设置实际大小。

onMeasure传入的widthMeasureSpec和heightMeasureSpec不是一般的尺寸数值,而是将模式和尺寸组合在一起的数值。我们需要通过int mode = MeasureSpec.getMode(widthMeasureSpec)得到模式,用int size = MeasureSpec.getSize(widthMeasureSpec)得到尺寸。


因此,在重写onMeasure方法时要根据模式不同进行尺寸计算。下面代码就是一种比较典型的方式:

@Override    
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    
    setMeasuredDimension(getMeasuredLength(widthMeasureSpec, true), getMeasuredLength(heightMeasureSpec, false));    
}    
private int getMeasuredLength(int length, boolean isWidth) {    
    int specMode = MeasureSpec.getMode(length);    
    int specSize = MeasureSpec.getSize(length);    
    int size;    
    int padding = isWidth ? getPaddingLeft() + getPaddingRight()    
            : getPaddingTop() + getPaddingBottom();    
    if (specMode == MeasureSpec.EXACTLY) {    
        size = specSize;    
    } else {    
        size = isWidth ? padding + mWave.length / 4 : DEFAULT_HEIGHT    
                + padding;    
        if (specMode == MeasureSpec.AT_MOST) {    
            size = Math.min(size, specSize);    
        }    
    }    
    return size;    
} 

待研究http://my.eoe.cn/bethkede/archive/489.html

http://blog.csdn.net/lilybaobei/article/details/8021868

抱歉!评论已关闭.