Android plotting Animation --- Drawable Animation

Source: Internet
Author: User

Drawing animation allows you to load a series of drawing resources in sequence to create an animation. This type of animation is a traditional animation. It is created and played in sequence using different image sequences, and looks like a movie. The AnimationDrawable class is the base class of drawing animation.

You can use the AnimationDrawable API to define animation frames in Code. However, it is easier to use an XML file that separately defines the list of animation frames to complete the animation. The XML file of this type of animation should be placed in the res/drawable/directory of your Android project. In this case, the instruction is the playing sequence of the animation and the playing time of each frame of the animation.
The animation XML file is composed of a <animation-list> root element and a series of <item> child elements defining each frame. <Item> The element defines the drawing resource and playback duration of each frame. The following is an example XML file for drawing Animation:

<Animation-list xmlns: android = "http://schemas.android.com/apk/res/android"

Android: oneshot = "true">

<Item android: drawable = "@ drawable/rocket_thrust1" android: duration = "200"/>

<Item android: drawable = "@ drawable/rocket_thrust2" android: duration = "200"/>

<Item android: drawable = "@ drawable/rocket_thrust3" android: duration = "200"/>

</Animation-list>

This animation has only three frames. By setting the android: oneshot attribute of the first animation to true, the animation is played once and then stopped at the last frame. If it is set to false, the animation will be played cyclically. You can use the rocket_thrust.xml file saved in the res/drawable/directory of the project to add a background image to a View object and then play an animation. An ImageView object is added to the Activity animation in the following example, And the animation starts playing when the screen is touched:

AnimationDrawable rocketAnimation;

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

ImageView rocketImage = (ImageView) findViewById (R. id. rocket_image );

RocketImage. setBackgroundResource (R. drawable. rocket_thrust );

RocketAnimation = (AnimationDrawable) rocketImage. getBackground ();

}

Public boolean onTouchEvent (MotionEvent event ){

If (event. getAction () = MotionEvent. ACTION_DOWN ){

RocketAnimation. start ();

Return true;

}

Return super. onTouchEvent (event );

}

Note that the start () method on the AnimationDrawable object cannot be called during the onCreate () method execution of the Activity. This is crucial because the AnimationDrawable object is not bound to the window at this time. If you want to play an animation immediately without interaction, you can call the star () method and onWindowFocusChanged () method in the onWindowFocusChanged () callback method of the Activity () the method is called by the Android system when the focus is obtained in the window.

By FireOfStar
 

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.