AsyncTask, View. post (Runnable), and ViewTreeObserver,
This article Reprinted from: http://www.cnblogs.com/hellope/archive/2011/08/23/2150094.html
In some requirements, the Animation must be automatically started when the program is running. We also know the Tween animation and frame Animation provided in android. However, when frame animation is used, the anim code of the Frame Animation animation is started. 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:
| 12345678 |
private Runnable runnable= new Runnable() { public void run() { frameAnimation.start(); } };Handler 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>{ @Override protected String doInBackground(String... params) { if(!frameAnim.isRunning()){ frameAnim.stop(); frameAnim.start(); } return ""; } }// Execute the onCreate MethodRunAnim runAnim=new RunAnim(); runAnim.execute(""); |
In this way, the program can automatically execute frame animation.
Next, use the View. post (Runnable) method:
imageV.post(new Runnable(){ @Override public void run() { frameAnim.start(); } }); |
Document: 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(){ @Override public boolean onPreDraw() { animDraw.start(); return true; } }; // In the onCreate MethodimageV.getViewTreeObserver().addOnPreDrawListener(opdl); |
The above are the three methods to automatically start frame animation. Of course, there must be other methods for processing android threads and implementing UI update operations. If there are any errors in the above description, I would like to provide more information !!!