public class DrawView extends View { public DrawView(Context context) { super(context); } }
//画笔 private Paint paint; //当前X坐标 private float currentX; //当前Y坐标 private float currentY; public DrawView(Context context) { super(context); this.paint = new Paint(); this.currentX = 100; this.currentY = 100; }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //设置画笔颜色 paint.setColor(Color.RED); //绘制圆形,中心位置(currentX, currentY),半径10 canvas.drawCircle(currentX, currentY, 10, paint); }
@Override public boolean onTouchEvent(MotionEvent event) { //获取点击事件的坐标x,y currentX = event.getX(); currentY = event.getY(); //重绘控件 invalidate(); return true; }
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout); DrawView draw = new DrawView(this); layout.addView(draw);