The custom View circle moves with the finger, and the view circle

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.