自定义View三个小圆

  1. 自定义View刚学到新知识,菜鸟正在成长,画了三个圆,自定义控件四个步骤比较重要onMeasure 主要是测量大小,onDraw绘制,onLayout 确定显示的位置 onTouch触摸事件
    简单的东西,迅速成长

public class ViewKey extends View{

/**
*自动生成的四个构造方法
/
public ViewKey(Context context) {
super(context);
}

public ViewKey(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ViewKey(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public ViewKey(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

/**
*onDraw在这绘制
/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

    Paint p1 = new Paint();

    Paint p2 = new Paint();

    Paint p3 = new Paint();

    float wi = canvas.getWidth()/2;
    float he = canvas.getHeight()/2;

    p1.setColor(Color.RED);
    p2.setColor(Color.YELLOW);
    p3.setColor(Color.BLUE);

    canvas.drawColor(Color.WHITE);

    canvas.drawCircle(wi-50, he-50,40, p2);
    canvas.drawCircle(wi, he,60, p1);

    canvas.drawCircle(wi+50, he+50,80, p3);





}

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

@Override
public boolean onTouchEvent(MotionEvent event) {
    return super.onTouchEvent(event);
}

}

自定义View三个小圆_第1张图片

你可能感兴趣的:(自定义View三个小圆)