Android Development Animationdrawable control frame play animation

Source: Internet
Author: User

When we click on the button, the picture will rotate continuously, and when we click the button again, it will stop in the current state.

Activity code:

The code is as follows Copy Code


Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.ImageView;
* @description frame-by-step animation in Android.
* Frame animation principle is very simple, like the movie playback, a picture of a similar continuous switch, when the switch speed reached a certain value,
* Our vision will be a residual shadow, The appearance of the shadow ensures the continuity of the visual changes, and the image switching is as real as it looks in our eyes.
* Want to use frame-by-line animation:
* First step: You need to create a new XML file under the Res/drawable folder, which defines the pictures used for animation playback, and toggles each picture
*         time spent, continuous playback, and so on. (Some articles say that the file is placed under the Res/anim folder and it turns out to be a mistake.)
* Step two: In code, assign the animated layout file to a specific picture display control, as in this example, ImageView.
* Step three: Get the corresponding Animationdrawable object by Imageview.getbackground (), and then control the animation through the method of the object
* @author Chenzheng_java
public class Animation1activity extends activity {
ImageView imageview;
public void OnC Reate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.animation1) ;

ImageView = (ImageView) Findviewbyid (;
Imageview.setbackgroundresource (r.drawable.animation1_drawable);


public void Myclickhandler (View Targetbutton) {
Get Animationdrawable Object
Animationdrawable animationdrawable = (animationdrawable) imageview.getbackground ();

Whether the animation is running
if (animationdrawable.isrunning ()) {
Stop Animation playing
Animationdrawable.stop ();
To start or resume an animated play
Animationdrawable.start ();


Animation1.xml file:

The code is as follows Copy Code

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://"
Android:layout_width= "Match_parent" android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<button android:id= "@+id/button_animation1" android:text= "animation start"
Android:layout_gravity= "Center_horizontal" android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" android:onclick= "Myclickhandler" ></Button>
<imageview android:id= "@+id/imageview_animation1"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:layout_weight= "1" ></ImageView>

XML file that holds the animated file:

  code is as follows copy code
<?xml Version= "1.0" encoding= "Utf-8"
Root label is Animation-list, where oneshot represents whether to show only once, set to false will not stop looping animation
Under the root tag, declare each picture in the animation with the item label
Android:duration represents the length of time that the picture was displayed

xmlns: Android= ""
android:oneshot= "false"

<item android: drawable= "@drawable/a1" android:duration= "a" ></ITEM>
<item android:drawable= "@drawable/a2" android:duration= "></item>"
<item android:drawable= "@drawable/a3" android:duration= "><" /item>
<item android:drawable= "@drawable/a4" android:duration= "></item>";
<item android: drawable= "@drawable/a5" android:duration= "a" ></ITEM>
<item android:drawable= "@drawable/a6" Android:duration= "></item>";

In addition: In Animationdrawable, we can also see several important methods as follows:

Setoneshot (Boolean flag) and configured in the configuration file, you can set whether the animation is played once, false for continuous playback;

Addframe (drawable frame, int duration) dynamically adds a picture into the animation

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.