View的测量

Android系统在绘制View前,须对View进行测量,即告诉系统绘制多大的View,这个过程在onMeasure()方法中执行。

  1. 测量View,Android系统使用MeasureSpec类,这个类定义了3种测量模式

    private static final int MODE_SHIFT = 30;
    private static final int MODE_MASK  = 0x3 << MODE_SHIFT;
        /**
         * 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;

    UNSPECIFIED(不指定其大小测量模式):View指定多大就多大,通常情况下绘制自定义View时才会使用。
    EXACTLY(精确值模式):当控件的layout_width属性和layout_height属性指定为具体数值时,或是为match_parent属性时,系统使用的是EXACTLY模式
    AT_MOST(最大值模式):当控件的layout_width属性和layout_height属性指定为wrap_content时使用这种测量模式。

  2. 获取测量模式
    MeasureSpec类内部提供了getMode方法

    /*
     *@param measureSpec参数是32位int类型
     *MODE_MASK为0x3 << 30为3左移30位
     *measureSpec与MODE_MASK取与运算正好得到高2位的值
     *比如16进制0111 1111 1111 1111 1111 1111 1111 1111 
     *         &
     *         1100 0000 0000 0000 0000 0000 0000 0000
     * 得到0x01 << 30 为精确值模式(之前定义常量EXACTLY)
     */
    public static int getMode(int measureSpec) {
            //noinspection ResourceType
            // MODE_MASK是之前定义的0x3 << MODE_SHIFT
            return (measureSpec & MODE_MASK);
        }
  3. 同理获取测量大小值取int低30位

           public static int getSize(int measureSpec) {
            return (measureSpec & ~MODE_MASK);
        }
  4. onMeasure方法
    我们自定义一个CustomView继承自View类,重写onMeasure方法

    public class CustomView extends View {
    public CustomView(Context context) {
        super(context);
        init();
    }
    
    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    private void init() {
        setBackgroundColor(0xffff0000);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /*
         *在AS中按住Ctrl键查看super.onMeasure()方法在父类中的实现
         *系统最终会调用setMeasuredDimension(int,int);
         *将测量后的宽高值设置进去,从而完成测量过程。
         */
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    }

    现在我们重写onMeasure方法

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getMeasureWidthOrHeight(widthMeasureSpec),
                getMeasureWidthOrHeight(heightMeasureSpec));
    }
    
    
    /*
     *我们自己写一个获取测量模式并返回测量结果的方法
     */
    private int getMeasureWidthOrHeight(int measureSpec){
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
    
        switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
                result = specSize;
                break;
            case MeasureSpec.AT_MOST:
                result = 200;
                result = Math.min(result, specSize);
                break;
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }

    然后将自定义View写到XML布局文件中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.tlkg.welcomehome.cursorview.MainActivity">

    <com.tlkg.welcomehome.cursorview.CustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent" />

</LinearLayout>

这里我们将宽和高属性设置为wrap_content,run代码后可以看到这里写图片描述
wrap_content代表了AT_MOST测量模式,在我们自己定义的Custom类中如果测量模式为AT_MOST返回测量大小200。
如果我们继承View类,没有重写onMeasure方法的话,将宽和高指定为wrap_content属性,将会和match_parent属性显示的效果相同,这是因为在View类中有这样一段代码:

    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;
    }

到这里,onMeasure方法就讲到这里了,有兴趣的小伙伴可以试下哈。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值