Android animation includes tween animation and Frame Animation. tween animation mainly includes operations such as image enlargement and downsize, rotation, transparency change, and movement. Frame Animation is much simpler, it means to continuously play a picture to produce an animated effect.
This section describes frame animation.Article.
Frame Animation is mainly implemented through the animationdrawable class. It has two important methods: Start () and stop () to start and stop the animation. Frame Animation is usually configured through an XML file, and an xml configuration file is created in the Res/anim directory of the project, the configuration file contains a <animation-List> root element and several <item> child elements.
Implement a frame animation for a person to dance. The six images are shown below:
1. Put the six images in the Res/drawable directory and name them p01.png, p02.png, p03.png, p04.png, p05.png, and p06.png respectively.
2. Create an xml configuration file under the Res/anim directory named dance. xml. The file content is as follows:
<? XML version = "1.0" encoding = "UTF-8" ?>
< Animation-list Xmlns: APK = "Http://schemas.android.com/apk/res/android" APK: oneshot = "False" >
< Item APK: drawable = "@ Drawable/p01" APK: Duration = "500" />
< Item APK: drawable = "@ Drawable/p02" APK: Duration = "500" />
< Item APK: drawable = "@ Drawable/P03" APK: Duration = "500" />
< Item APK: drawable = "@ Drawable/p04" APK: Duration = "500" />
< Item APK: drawable = "@ Drawable/p05" APK: Duration = "500" />
< Item APK: drawable = "@ Drawable/p06" APK: Duration = "500" />
</ Animation-list >
APK: oneshot indicates whether to run only once. If it is set to false, it means loop playback.
3. Create the layout configuration file dance. XML in the Res/layout directory. The file content is as follows:
<? XML version = "1.0" encoding = "UTF-8" ?>
< Linearlayout Xmlns: APK = "Http://schemas.android.com/apk/res/android" APK: Orientation = "Vertical" APK: layout_width = "Fill_parent" APK: layout_height = "Fill_parent" >
<! -- Frame animated images -->
< Imageview APK: ID = "@ + ID/imgdance" APK: layout_width = "Wrap_content" APK: layout_height = "Wrap_content" APK: Background = "@ Anim/dance" />
<! -- Animation control button -->
< Linearlayout APK: layout_width = "Fill_parent" APK: layout_height = "Wrap_content" APK: Orientation = "Horizontal" >
< Button APK: Text = "Start" APK: layout_width = "Wrap_content" APK: layout_height = "Wrap_content" APK: onclick = "Onstartdance" />
< Button APK: Text = "End" APK: layout_width = "Wrap_content" APK: layout_height = "Wrap_content" APK: onclick = "Onstopdance" />
</ Linearlayout >
</ Linearlayout >
APK: the background uses the animation above as the background, which means that to obtain the animation, you only need to obtain the background of the view. Of course, you canCodeBy setting the background;
APK: onclick indicates the action of the button. Of course, you can implement onclicklistener in the code.
4. Activity Code:
/*** Copyright (c) 2004-2011 All Rights Reserved. */package COM. aboy. android. study. animation; import android. app. activity; import android. graphics. drawable. animationdrawable; import android. OS. bundle; import android. view. view; import android. widget. imageview; import COM. aboy. android. study. r;/*** Frame Animation ** @ author obullxl@gmail.com * @ version $ ID: frameactivity. java, V 0.1 12:49:46 oldbulla exp $ */public class frameactivity extends activity {public static final string tag = "frameactivity"; // Private imageview imgdance, an animation component; // frame animation private animationdrawable animdance;/*** @ see android. app. activity # oncreate (Android. OS. bundle) */Public void oncreate (bundle cycle) {super. oncreate (cycle); super. setcontentview (R. layout. dance); // instantiate the component this. imgdance = (imageview) super. findviewbyid (R. id. imgdance); // obtain the background (6 animated images) This. animdance = (animationdrawable) This. imgdance. getbackground ();}/*** button: Start 'Dancing 'animation */Public void onstartdance (view) {This. animdance. start ();}/*** button: Stop the 'Dancing 'animation */Public void onstopdance (view) {This. animdance. stop ();}}
just a few lines of code. After running, click the start button and the Animation continues to play until you click STOP.