onSizeChanged、onDraw、onMeasure、onLayout 执行顺序和作用

import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * @author Jiang zinc
 * @date 创建时间:2018/10/22
 * @description
 */
public class XFerModeView extends View {

    private String TAG = "XFerModeView";

    public XFerModeView(Context context) {
        this(context, null, 0);
    }

    public XFerModeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public XFerModeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        Log.i(TAG, "XFerModeView 构造");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        Log.i(TAG, "onMeasure");

        // 获取宽
        // 方式一:
        int width1 = MeasureSpec.getSize(widthMeasureSpec);
        // 方式二:
        int width2 = getMeasuredWidth();

        // 获取宽
        // 方式一:
        int height1 = MeasureSpec.getSize(heightMeasureSpec);
        // 方式二:
        int height2 = getMeasuredHeight();

        Log.i(TAG, "onMeasure [ width1:" + width1 +
                "; width2:" + width2 +
                ";height1:" + height1 +
                ";height2:" + height2 + " ]");

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.i(TAG, "onSizeChanged");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(TAG, "onDraw");
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.i(TAG, "onLayout");
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        Log.i(TAG, "onFinishInflate");
    }
}

结果:

10-22 11:08:51.312 com.zinc.ui2018 I/XFerModeView: XFerModeView 构造
10-22 11:08:51.312 com.zinc.ui2018 I/XFerModeView: onFinishInflate
10-22 11:08:51.341 com.zinc.ui2018 I/XFerModeView: onMeasure [ width1:720; width2:720;height1:1132;height2:1132 ]
10-22 11:08:51.378 com.zinc.ui2018 I/XFerModeView: onSizeChanged
10-22 11:08:51.312 com.zinc.ui2018 I/XFerModeView: onLayout
10-22 11:08:51.395 com.zinc.ui2018 I/XFerModeView: onMeasure [ width1:720; width2:720;height1:1132;height2:1132 ]
10-22 11:08:51.312 com.zinc.ui2018 I/XFerModeView: onLayout
10-22 11:08:51.396 com.zinc.ui2018 I/XFerModeView: onDraw

1、MyView() 构造方法,第一个被调用。

2、onFinishInflate() 当View中所有的子控件均被映射成xml后触发。

3、onMeasure() 在View放置到父容器时调用

作用:测量View的大小,也可以通过下面方式,修改View的大小

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	// 补充自己的逻辑,最后记得调用 setMeasuredDimension 将测量的宽高设置回去
	setMeasuredDimension(100,100);
}

获取控件的宽和高的方式(两种方式获取是一样的,具体结果可看上面日志,或自行运行)

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    // 获取宽
    // 方式一:
    int width1 = MeasureSpec.getSize(widthMeasureSpec);
    // 方式二:
    int width2 = getMeasuredWidth();

    // 获取宽
    // 方式一:
    int height1 = MeasureSpec.getSize(heightMeasureSpec);
    // 方式二:
    int height2 = getMeasuredHeight();

    Log.i(TAG, "onMeasure [ width1:" + width1 +
            "; width2:" + width2 +
            ";height1:" + height1 +
            ";height2:" + height2 + "]");
    
}

4、onSizeChanged() 在控件大小发生改变时调用。所以这里初始化会被调用一次
作用:获取控件的宽和高度

5、onDraw() 绘制图形

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值