Android API Guides: Animation and Graphics -- 4. Drawable Animation

Source: Internet
Author: User

Http://developer.android.com/guide/topics/graphics/drawable-animation.html

Drawable animation lets you load a series of Drawable resources one after another to create an animation. this is a traditional animation in the sense that it is created with a sequence of different images, played in order, like a roll of film. theAnimationDrawableClass is the basis for Drawable animations.

While you can define the frames of an animation in your code, usingAnimationDrawableClass API, it's more simply accomplished with a single XML file that lists the frames that compose the animation. The XML file for this kind of animation belongs inres/drawable/Directory of your Android project. In this case, the instructions are the order and duration for each frame of the animation.

The XML file consists of<animation-list>Element as the root node and a series of child<item>Nodes that each define a frame: a drawable resource for the frame and the frame duration. Here's an example XML file for a Drawable 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 runs for just three frames. By settingandroid:oneshotAttribute of the listTrue, It will cycle just once then stop and hold on the last frame. If it is setFalseThen the animation will loop. With this XML savedrocket_thrust.xmlInres/drawable/Directory of the project, it can be added as the background image to a View and then called to play. Here's an example Activity, in which the animation is added toImageViewAnd then animated 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);}

It's important to note thatstart()Method called on the AnimationDrawable cannot be called duringonCreate()Method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it fromonWindowFocusChanged()Method in your Activity, which will get called when Android brings your window into focus.

For more information on the XML syntax, available tags and attributes, see Animation Resources.


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.