Android invalidate () Auto clear screen, screen refresh

Source: Internet
Author: User
Tags clear screen

Invalidate () is used to refresh the view and must be working in the UI thread. For example, when you modify the display of a view, call invalidate () to see the redrawn interface. Invalidate () is called to pop the old view from the main UI thread queue.
For screen refreshes there are the following concentrations to consider:
1. Do not use multithreading and double buffering
This is the simplest scenario, and it's generally just a wish to redraw the UI when the view changes. You only need to explicitly invoke the Invalidate () method in the View object in the activity. The OnDraw () method of the view is automatically called.
2. Use multithreading and do not use double buffering
This situation requires a new thread to be opened, and the new thread will not have access to the View object. Forced access will be reported: Android.view.viewroot$calledfromwrongthreadexception:only the original thread that created a view hierarchy can touch its views.
At this point you need to create a subclass that inherits the Android.os.Handler and override the Handlemessage (Message msg) method. Android.os.Handler is capable of sending and processing messages, you need to emit a message in the activity that updates the UI, and then your handler (you can use anonymous inner classes) to process the message (because the anonymous inner class can access the parent class variable, You can directly invoke the invalidate () method in the View object. That is, a message is created and sent on a new thread, and then captured and processed in the main threads.

3. Using multithreading and double buffering
Surfaceview is a subclass of view in Android, and she also implements double buffering. You can define a subclass of her and implement the Surfaceholder.callback interface. With the implementation of the Surfaceholder.callback interface, the new thread does not need Android.os.Handler to help. Surfaceholder the Lockcanvas () method can lock the canvas, draw a new image after it is called unlockcanvasandpost (canvas) to unlock (display), or more convenient.

Android invalidate () Auto clear screen, screen refresh

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.