2021-02-26 关于自定义view

直接甩一串代码 

public class CustomView extends View {

private final static StringTAG="customView";

//java直接调用 走一参

public CustomView (Context context) {

this(context,null);

}

//xml调用 走两参

public CustomView (Context context,@Nullable AttributeSet attrs) {

this(context, attrs,0);

}

//xml调用 且有自定义属性 三参

public CustomView (Context context,@Nullable AttributeSet attrs,int defStyleAttr) {

this(context, attrs, defStyleAttr,0);

}

//xml调用 且attrs无值 

public CustomView (Context context,@Nullable AttributeSet attrs,int defStyleAttr,int defStyleRes) {

super(context, attrs, defStyleAttr, defStyleRes);

Log.i(TAG,"构造");

//获取xml属性   不需死记  点击看下view源码里就有方法

TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.QQBezierView,defStyleAttr,defStyleRes);

typedArray.getDimension(R.styleable.QQBezierView_textSize,10);

}

@Override

    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    Log.i(TAG,"onMeasure");

}

@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 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 onDraw(Canvas canvas) {

super.onDraw(canvas);

Log.i(TAG,"onDraw");

}

}

输出语句

2021-02-26 11:19:40.598 12237-12237/com.whc.paintapp I/customView: 构造

2021-02-26 11:19:40.660 12237-12237/com.whc.paintapp I/customView: onMeasure

2021-02-26 11:19:40.670 12237-12237/com.whc.paintapp I/customView: onMeasure

2021-02-26 11:19:40.671 12237-12237/com.whc.paintapp I/customView: onSizeChanged

2021-02-26 11:19:40.671 12237-12237/com.whc.paintapp I/customView: onLayout

2021-02-26 11:19:40.693 12237-12237/com.whc.paintapp I/customView: onDraw

你可能感兴趣的:(2021-02-26 关于自定义view)