The custom View circle moves with the finger, and the view circle
Custom View is used to implement functions that cannot be implemented by built-in controls.
To customize a View (small circle), you must first inherit a View
Public class CircleView extends View {private float cx = 100; private float cy = 100; public CircleView (Context context) {super (context);} public CircleView (Context context, @ Nullable AttributeSet attrs) {super (context, attrs);} public CircleView (Context context, @ Nullable AttributeSet attrs, int records) {super (context, attrs, defStyleAttr );} @ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas); Paint paint = new Paint (); paint. setColor (Color. RED); paint. setAntiAlias (true); paint. setStyle (Paint. style. FILL); canvas. drawCircle (cx, cy, 100, paint);} // This is to solve the event conflict @ Override public boolean onTouchEvent (MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: // press cx = event. getX (); cy = event. getY (); break; case MotionEvent. ACTION_MOVE: // move cx = event. getX (); cy = event. getY (); break; case MotionEvent. ACTION_UP: // lift cx = event. getX (); cy = event. getY (); break;} // You Need to redraw a new shape // invalidate (); // However, you can only draw a graph in the main thread postInvalidate (); // you can use both the main thread and sub-thread .... when the two methods are called, The onDraw method will be automatically called // The touch event will handle the return true ;}
And then use it in the layout.