//Main
public class MyView extends View {
Paint paint;
float mTouchX=300,mTouchY=300,mRadiu=50;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
paint=new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.FILL);
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mTouchX=event.getX();
mTouchY=event.getY();
//刷新view
invalidate();
return true;
}
});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(mTouchX,mTouchY,mRadiu,paint);
}
}
//XML
<com.example.tuodong.MyView2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccc"
/>