android_studio的canvas画圆

1.怎样设置代码

package com.example.rk1130;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/*
自定义view
*/
public class BallView extends View {
private String ball_size;
private float tx=200f;
private float ty=200f;
private Paint paint;

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

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

public BallView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}
private void init(){
//设置画笔
    paint = new Paint();
    //颜色
    paint.setColor(Color.BLUE);
    //抗锯齿
    paint.setAntiAlias(true);
    //画笔粗细
    paint.setStrokeWidth(2);
    //填充
    paint.setStyle(Paint.Style.FILL);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Path path=new Path();
    canvas.drawCircle(tx,ty,50,paint);

}

@Override
public boolean onTouchEvent(MotionEvent event) {
//两种方式 一种switch 一种直接写
    switch (event.getAction()){
        case MotionEvent.ACTION_UP:
            tx=event.getX();
            ty=event.getY();
            break;
        case MotionEvent.ACTION_DOWN:
            tx=event.getX();
            ty=event.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            tx=event.getX();
            ty=event.getY();
            break;
    }
    /*tx=event.getX();
    ty=event.getY();*/
    this.invalidate();
    return true;
}

}

2.布局


你可能感兴趣的:(android_studio的canvas画圆)