android自定义view之画圆随着手指移动

public class MyView extends View {

private Paint mFanPaint,mTextPaint;//扇形画笔和文字画笔
public float AxisX=100;
public float AxisY=100;

public MyView(Context context) {
    super(context);
    init();
}

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

public MyView(Context context,  AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

private void init(){
    mFanPaint = new Paint();
    mFanPaint.setStyle(Paint.Style.FILL);//设置画笔为全填充
    mFanPaint.setColor(Color.BLACK);//画笔颜色
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
	//开始画圆,AxisX开始x的位置,AxisY开始Y的位置, 50是圆的半径 mFanPaint画笔
    canvas.drawCircle(AxisX,AxisY,50,mFanPaint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
	//getX获得点击x位置
    AxisX = event.getX();
	//getY获得点击Y的位置
    AxisY = event.getY();
	//重绘,或者说刷新View
    invalidate();
	返回true自身消费
    return true;
}
}

你可能感兴趣的:(进阶,View画圆可以跟随手指移动)