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() 绘制图形