public class DrawView extends View { public DrawView(Context context) { super(context); } public DrawView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } private int cx = 100; private int cy = 100; /** * Canvas canvas画布.....paint画笔 * @param canvas */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //中心点x坐标,y坐标,半径,画笔 Paint paint = new Paint(); paint.setColor(Color.RED);//设置颜色 paint.setStrokeWidth(2);//描边的宽度2个像素 //Paint.Style.FILL填充,,Paint.Style.STROKE描边 ,,Paint.Style.FILL_AND_STROKE填充并且描边 paint.setStyle(Paint.Style.FILL); paint.setAntiAlias(true);//设置抗锯齿 canvas.drawCircle(cx,cy,100,paint); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: cx = (int) event.getX(); cy = (int) event.getY(); break; case MotionEvent.ACTION_MOVE: cx = (int) event.getX(); cy = (int) event.getY(); break; case MotionEvent.ACTION_UP: cx = (int) event.getX(); cy = (int) event.getY(); break; } //重新绘制....自动去调用onDraw postInvalidate();//.........可以使用在子线程 //invalidate();//...只能用在主线程 return true; } }
在activity中引用即可