自定义控件之onMeasure解析01

好吧,为何要写这个系列的文章呢,因为最近看了一些源码,很多源控件都是自定义控件,因此有必要重新对自定义控件进行系统的学习。

知识点:自定义控件一般继承View,也可直接继承已有的控件。不管哪种情况,核心思想还是按照:onMeasure->onLayout->onDraw这个步骤来。

1.onMeasure()

布局文件:




    

        

    


@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int withMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int withSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        L.e("onMeasure-->withMeasureSpec:"+widthMeasureSpec+"  ---withMode:"+withMode+"  ---withSize:"+withSize);
    }

当自定义控件设置了具体的宽高时,我们看输出结果:

onMeasure-->withMeasureSpec:-2147482748  ---withMode:-2147483648  ---withSize:900

发现:withMeayousureSpec = withMode + withSize.可以看到:
withMode = 2^31,并且withMeasureSpec为int型,占32位,所以withMode应该是withMeasureSpec的高位,而withSize为其低位上的数值。再通过MeasureSepc源码得知它是通过装载和卸载mode、size元组于int数中来简化对象的分配。也就是说MeasureSpec是由高两位size+低30位mode组成的32位int数来表示。
withMode有三种:

private static final int MODE_SHIFT = 30;
/**
         * Measure specification mode: The parent has not imposed any constraint
         * on the child. It can be whatever size it wants.
         */
        public static final int UNSPECIFIED = 0 << MODE_SHIFT;

        /**
         * Measure specification mode: The parent has determined an exact size
         * for the child. The child is going to be given those bounds regardless
         * of how big it wants to be.
         */
        public static final int EXACTLY     = 1 << MODE_SHIFT;

        /**
         * Measure specification mode: The child can be as large as it wants up
         * to the specified size.
         */
        public static final int AT_MOST     = 2 << MODE_SHIFT;

可以看出,三种mode分别为00、01、10左移30位得到,因此,上面打印得到的mode=-2147483648对应这里的ModeSpec.AT_MOST;而size则对应的是自定义控件父布局的宽。
然后,我们将布局文件修改一下:




    

        

    


输出结果为:onMeasure-->withMeasureSpec:1073742424 ---withMode:1073741824 ---withSize:600
这时,withMode = 2^30 = SpecMode.EXACTLY;withsize对应其本身的宽。
接下来,我们将布局文件再改一下:




    

        

    


打印结果:onMeasure-->withMeasureSpec:-2147482568 ---withMode:-2147483648 ---withSize:1080
这时:mode = 2^31 = MeasureSpec.AT_MOST ;size则为手机宽的尺寸。
其他更改布局就补贴出来了,通过更改布局打印结果,我们基本可以得出以下结论:
这里我们假定自定义控件为customView,其父窗体为PcustomView.那么:

  1. 当PcustomView.Mode = MeasureSpec.EXACTLY时:
    a.如果customView设置with 为具体的值,那么customView.Mode = MeasureSpec.EXACTLY, customView.size = 它设置的具体值;
    b.如果customView设置with为wrap_content,那么customView.Mode = MeasureSpec.AT_MOST,customView.size =父窗体所提供的大小;
    c.如果customView设置为match_parent,那么customView.Mode = MeasureSpec.EXACTLY,customView.size = 父窗体提供的大小

  2. 当PcustomView.Mode = MeasureSpec.AT_MOST时:
    a.如果customView设置with 为具体的值,那么customView.Mode = MeasureSpec.EXACTLY, customView.size = 它设置的具体值;
    b.如果customView设置with为wrap_content,那么customView.Mode = MeasureSpec.AT_MOST,customView.size =父窗体所提供的大小;
    c.如果customView设置为match_parent,那么customView.Mode = MeasureSpec.EXACTLY,customView.size = 父窗体提供的大小.

  3. 当PcustomView.Mode = MeasureSpec. UNSPECIFIED时:
    a.如果customView设置with 为具体的值,那么customView.Mode = MeasureSpec.EXACTLY, customView.size = 它设置的具体值;
    b.如果customView设置with为wrap_content,那么customView.Mode = MeasureSpec.UNSPECIFIED,customView.size =0;
    c.如果customView设置为match_parent,那么customView.Mode = MeasureSpec.UNSPECIFIED,customView.size = 0.

有了以上结论,我们就知道如何在onMeasure方法中设置自己想要的尺寸了,记得确定尺寸后要调用` setMeasuredDimension(int measuredWidth, int measuredHeight)方法。这个方法是将确定的长宽值设置到画布中。

你可能感兴趣的:(Android)