Asynctask, view. Post (runnable), and viewtreeobserver

Source: Internet
Author: User

In some requirementsProgramThe animation is automatically started at runtime. We also know the tween animation and Frame Animation provided in Android. However, when Frame Animation is usedCodeAnim. start (); cannot be in oncreate (), because in oncreate (), animationdrawable is not completely bound to imageview, And the animation is started in oncreate, you can only see the first image. Now the question is, how can I enable the animation automatically when the program starts? You can try it in the onstart method, but the results cannot be as expected. This is not the case. Continue and try it with handler! The Code is as follows:

 
Private runnable = new runnable () {public void run () {frameanimation. start () ;}}; handler = new handler (); // In the oncreate method: Handler. post (runnable );

The handler object will put the runnable object in the POST method into the UI execution queue. The UI consumes this queue and calls the runnable run method. No new thread is generated here, And the runnable is running in the main thread where the UI is located. But this method does not work!

The following are three methods to automatically start Frame Animation:

First use asynctask: Handler and asynctask are designed to not block the main thread (ui thread), and UI Updates can only be completed in the main thread, so asynchronous processing is inevitable. Asynctask makes it easier to create long-running tasks that require interaction with the user interface. It can be implemented without using threads and handler. I will not talk about asynctask here, that is, a little bit.

 
Imagev. setbackgroundresource (R. anim. myframeanimation); frameanim = (animationdrawable) imagev. getbackground ();
 
Class runanim extends asynctask <string, String, string >{@ overrideprotected string doinbackground (string... Params) {If (! Frameanim. isrunning () {frameanim. stop (); frameanim. start ();} return "" ;}}// in the oncreate method, run runanim = new runanim (); runanim.exe cute ("");

In this way, the program can automatically execute frame animation.

Next, use the view. Post (runnable) method:

 
Imagev. Post (New runnable () {@ overridepublic void run () {frameanim. Start ();}});

Documentation:Boolean Android. View. View. Post (runnable action)

Causes the runnable to be added to the Message Queue. the runnable will be run on the user interface thread. You can add your runnable object to the UI thread for running.

In this way, Frame Animation can be started normally.

The third is to use viewtreeobserver. onpredrawlistener listener.:When an event is generated when a view tree is to be drawn, you can add an event processing function: onpredraw

 
Onpredrawlistener opdl = new onpredrawlistener () {@ overridepublic Boolean onpredraw () {animdraw. start (); Return true ;}}; // imagev in the oncreate method. getviewtreeobserver (). addonpredrawlistener (opdl );

the preceding three methods are used to automatically start Frame Animation. Of course, there are certainly other methods for Android thread processing and UI update operations. If there are any errors in the above description, I would like to provide more information !!!

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.