SlidingupPanel

来源:互联网 发布:ehr人力资源软件 编辑:程序博客网 时间:2024/06/05 20:53

一,继承ViewGroup的相关知识  setWIllNotDraw(false)

setWillNotDraw(false),在继承ViewGroup--容器的时候,本身没有任何可以画的东西,是一个透明的控件,因此不会触发onDraw方法,但是如果给viewgroup设置一个背景色,系统就认为,viewgroup有东西可画了,就会调用ondraw方法。

源码当中,调用onDraw方法的条件是

if (!dirtyOpaque) onDraw(canvas);  

 在ViewGroup初始化的时候,它调用了一个私有放阿飞,initViewGroup,它里面有一句setFlags(WILLL_NOT_DRAW,DRAW_MASK);相当于调用了setWillNotDraw(true),所以说,对于ViewGroup,它就认为是透明的了,所以我们想要重写onDraw,就需要调用setWillNotDraw(false);

总结  

1)ViewGroup默认情况下,会被设置成WILL_NOT_DRAW,这是从性能考虑,这样一来,onDraw就不会被调用了。

2)如果我们要重要一个ViweGroup的onDraw方法,有两种方法:

        1,在构造函数里面,给其设置一个颜色,如#00000000。

        2,在构造函数里面,调用setWillNotDraw(false),去掉其WILL_NOT_DRAW flag。

      继承ViewGroup的相关知识   ViewDragHelper

      ViewDragHelper is a utility class for writing custom ViewGroups. It offers a number of useful operations and state tracking for allowing a user to drag and reposition views within their parent ViewGroup.

   

   二,setMeasuredDimension(widthSize, heightSize);决定了当前view的大小。

   三,View的测量过程

       

    public final void measure(int widthMeasureSpec, int heightMeasureSpec) {    //这里的两个参数分别是父视图提供的测量规格,   //当父视图调用子视图的measure函数对子视图进行测量时,会传入这两个参数,通过这两个参数以及子视图本身的LayoutParams来共同决            //定子视图的测量规格,//MeasureSpec参数的值为int型,分为高32位和低16为,高32位保存的是specMode,低16位表示specSize//specMode分三种: //1、MeasureSpec.UNSPECIFIED,父视图不对子视图施加任何限制,子视图可以得到任意想要的大小; //2、MeasureSpec.EXACTLY,父视图希望子视图的大小是specSize中指定的大小; //3、MeasureSpec.AT_MOST,子视图的大小最多是specSize中的大小。        if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||                widthMeasureSpec != mOldWidthMeasureSpec ||                heightMeasureSpec != mOldHeightMeasureSpec) {             // first clears the measured dimension flag            mPrivateFlags &= ~MEASURED_DIMENSION_SET;             if (ViewDebug.TRACE_HIERARCHY) {                ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);            }             // measure ourselves, this should set the measured dimension flag back            onMeasure(widthMeasureSpec, heightMeasureSpec);            // flag not set, setMeasuredDimension() was not invoked, we raise            // an exception to warn the developer            if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {                throw new IllegalStateException("onMeasure() did not set the"                        + " measured dimension by calling"                        + " setMeasuredDimension()");            }             mPrivateFlags |= LAYOUT_REQUIRED;        }         mOldWidthMeasureSpec = widthMeasureSpec;        mOldHeightMeasureSpec = heightMeasureSpec;    }      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));     }   public static int getDefaultSize(int size, int measureSpec) {        int result = size;        int specMode = MeasureSpec.getMode(measureSpec);        int specSize = MeasureSpec.getSize(measureSpec);        switch (specMode) {        case MeasureSpec.UNSPECIFIED:            result = size;            break;        case MeasureSpec.AT_MOST:        case MeasureSpec.EXACTLY:            result = specSize;            break;        }        return result;    } 总结在measure方法中会调用onMeasure再调用setMeasuredDimension      设置view的大小。 ViewGroup的子类而言,往往会重载onMeasure函数负责其children的measure工作,重载时不要忘记调用setMeasuredDimension来设置自身的mMeasuredWidth和mMeasuredHeight。如果我们在layout的时候不需要依赖子视图的大小,那么不重载onMeasure也可以,但是必须重载onLayout来安排子视图的位置