Android uses animation-list to animate frames by frame

Source: Internet
Author: User

This article reproduced: http://blog.csdn.net/aminfo/article/details/7847761

The first step: the first piece of material, the following materials are placed in the Res/drawable directory:

The second step: on the animation animation-list frame layout file, there are 2, one is to display the animation sequentially, one is the reverse display animation, the file is stored in the Res/drawable directory

Sequential display of animation files: Animation1.xml

<?XML version= "1.0" encoding= "Utf-8"?><!--The root tag is animation-list, where oneshot represents whether to show only once, set to false will not stop looping the animation root tag, through the item tag each picture in the animation declaration android:duration Show The length of time that the picture was used -<animation-listxmlns:android= "Http://schemas.android.com/apk/res/android"Android:oneshot= "true"  >      <Itemandroid:drawable= "@drawable/icon1"android:duration= "Max"></Item>      <Itemandroid:drawable= "@drawable/icon2"android:duration= "Max"></Item>      <Itemandroid:drawable= "@drawable/icon3"android:duration= "Max"></Item>      <Itemandroid:drawable= "@drawable/icon4"android:duration= "Max"></Item>      <Itemandroid:drawable= "@drawable/icon5"android:duration= "Max"></Item>      <Itemandroid:drawable= "@drawable/icon6"android:duration= "Max"></Item></animation-list>

Show animation files in reverse: animation2.xml

<?XML version= "1.0" encoding= "Utf-8"?><!--The root tag is animation-list, where oneshot represents whether to show only once, set to false will not stop looping the animation root tag, through the item tag to each picture in the animation declaration android:duration Show The length of time it takes to display the picture -<animation-listxmlns:android= "Http://schemas.android.com/apk/res/android"Android:oneshot= "true"  >      <Itemandroid:drawable= "@drawable/icon6"android:duration= "Max"></Item>      <Itemandroid:drawable= "@drawable/icon5"android:duration= "Max"></Item>      <Itemandroid:drawable= "@drawable/icon4"android:duration= "Max"></Item>      <Itemandroid:drawable= "@drawable/icon3"android:duration= "Max"></Item>      <Itemandroid:drawable= "@drawable/icon2"android:duration= "Max"></Item>      <Itemandroid:drawable= "@drawable/icon1"android:duration= "Max"></Item></animation-list>

The third step: on the layout file, placed in the Res/layout directory, the file name Main.xml:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical">       <ImageViewAndroid:id= "@+id/animationiv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:padding= "5px"android:src= "@drawable/animation1"/>                 <ButtonAndroid:id= "@+id/buttona"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:padding= "5px"Android:text= "Sequential display" />        <ButtonAndroid:id= "@+id/buttonb"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:padding= "5px"Android:text= "Stop" />        <ButtonAndroid:id= "@+id/buttonc"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:padding= "5px"Android:text= "Reverse display" /></LinearLayout>

Fourth step: The activity file, file name: Mainactivity.java

 Packageorg.shuxiang.test;Importandroid.app.Activity;Importandroid.graphics.drawable.AnimationDrawable;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.Window;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView; Public classActivity10extendsactivity{PrivateImageView Animationiv; PrivateButton Buttona, buttonb, Buttonc; Privateanimationdrawable animationdrawable; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);                       Setcontentview (R.LAYOUT.TEST10); Animationiv=(ImageView) Findviewbyid (R.id.animationiv); Buttona=(Button) Findviewbyid (R.id.buttona); BUTTONB=(Button) Findviewbyid (R.ID.BUTTONB); Buttonc=(Button) Findviewbyid (R.id.buttonc); Buttona.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubAnimationiv.setimageresource (R.drawable.animation1); Animationdrawable=(animationdrawable) animationiv.getdrawable ();            Animationdrawable.start ();                 }                    }); Buttonb.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubAnimationdrawable =(animationdrawable) animationiv.getdrawable ();            Animationdrawable.stop ();                }                    }); Buttonc.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubAnimationiv.setimageresource (R.drawable.animation2); Animationdrawable=(animationdrawable) animationiv.getdrawable ();            Animationdrawable.start ();            }                    }); }}

Android uses animation-list to animate frames by frame

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.