Android Development Animationdrawable control frame play animation

Source: Internet
Author: User
Tags xmlns

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

Package cn.com.chenzheng_java.animation;

Import android.app.Activity;
Import android.graphics.drawable.AnimationDrawable;
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;
@Override
public void OnC Reate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.animation1) ;

ImageView = (ImageView) Findviewbyid (R.id.imageview_animation1);
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 ();
}
else{
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://schemas.android.com/apk/res/android"
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>
</LinearLayout>

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

<animation-list
xmlns: Android= "http://schemas.android.com/apk/res/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>";
</animation-list>

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

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.