Implementation of automatic playback of Android animation background images

Source: Internet
Author: User

 

Automatic playback of Android animated background images
When developing Android applications, we often encounter a situation similar to loading a large image from the network. During the loading process, we first display a circular loading animation in the imageview position of the image to be displayed, it provides a better user experience. It is very easy to implement this animation. It is usually used to define XML in/RES/anim .... . For example:
<? XML version = "1.0" encoding = "UTF-8"?>
<Animation-list Android: oneshot = "false"
Xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: Duration = "100" Android: drawable = "@ drawable/loading_1"/>
<Item Android: Duration = "100" Android: drawable = "@ drawable/loading_2"/>
<Item Android: Duration = "100" Android: drawable = "@ drawable/loading_3"/>
<Item Android: Duration = "100" Android: drawable = "@ drawable/loading_4"/>
<Item Android: Duration = "100" Android: drawable = "@ drawable/loading_5"/>
<Item Android: Duration = "100" Android: drawable = "@ drawable/loading_6"/>
<Item Android: Duration = "100" Android: drawable = "@ drawable/loading_7"/>
<Item Android: Duration = "100" Android: drawable = "@ drawable/loading_8"/>
<Item Android: Duration = "100" Android: drawable = "@ drawable/loading_9"/>
<Item Android: Duration = "100" Android: drawable = "@ drawable/loading_10"/>
<Item Android: Duration = "100" Android: drawable = "@ drawable/loading_11"/>
<Item Android: Duration = "100" Android: drawable = "@ drawable/loading_12"/>
</Animation-List>

However, most of my friends may encounter the problem that the animation is ready, but when the interface is loaded, the animation will not be automatically played, and it has to be triggered by events such as screen clicks, this is meaningless. In fact, the android animation animationdrawable component has a start () method used to start the animation playback. However, this method cannot be directly written in onclick, onstart, and onresume, writing in does not work, and animation cannot be started. It can only be written in event listening for example. So we can use tips to enable automatic playback.
Currently, I know three methods:

Imageview = (imageview) findviewbyid (R. Id. XXX );

Method 1: Use runnalbe () to load
Imageview. setbackgroundresource (R. anim. XXXXX );
Final animationdrawable = (animationdrawable) imageview. getbackground ();
Imageview. Post (New runnable (){
@ Override
Public void run (){
Animationdrawable. Start ();
}
});

Note: getbackground () must be used here; the getdrawable () method cannot be used, and it will not work. The reason is as follows:

We use the resource background set by the setbackgroundresource method of imageview, which is equivalent to the Android: Background attribute in the layout file. This attribute is a property of the View class and must be obtained through the getbackground () method; getdrawable () is the imageview class method. You must use setimageresource (INT) (Android: SRC corresponding to the layout file) or setimagedrawable (drawable) in the code) to use the getdrawable () method.
(For details, refer to the post: http://www.eoeandroid.com/forum... 1 & extra = # pid1627412)

Method 2: Use asynctask to asynchronously load and start
Imageview. setbackgroundresource (R. anim. XXXXX );
Animationdrawable = (animationdrawable) imageview. getbackground ();
Runanim = new runanim ();
Runanim.exe cute ("");

Class runanim extends asynctask <string, String, string> {
@ Override
Protected string doinbackground (string... Params ){
If (! Animationdrawable. isrunning ()){
Animationdrawable. Stop ();
Animationdrawable. Start ();
}
Return "";
}
}

Method 3: Add addonpredrawlistener to automatically load the file (I use this method and it feels very useful)
Imageview. setbackgroundresource (R. anim. XXXXX );
Animationdrawable = (animationdrawable) imageview. getbackground ();

// Note that if your image control uses setimageresource, you should use getdrawable ();
Imageview. getviewtreeobserver (). addonpredrawlistener (predrawlistener );

Onpredrawlistener predrawlistener = new onpredrawlistener (){
@ Override
Public Boolean onpredraw (){
Animationdrawable. Start ();
Return true; // The value true must be returned.
}
};

The above three methods have not been tested. In addition, there are some methods on the Internet that use the onwindowfocuschanged () method to rewrite the activity, but there are still some shortcomings. You must change the focus to trigger them, although the theory can automatically change the focus, it is not desirable.
Http://www.toplee.com/blog/1345.html)

Related Article

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.