The Android platform offers two types of animations. One is the tween animation, that is, the object in the scene constantly changing the image to produce animation effects (rotation, translation, indentation and gradient). The second category is frame animation, which is the sequential playback of pre-made images, similar to the GIF picture principle.
Let's talk about frame Animation.
In fact, it is relatively simple to use, first need to create a Animationdrawable object, through the Addframe method to display each frame to add the content, and finally through the Start method to play the animation. At the same time, there are methods such as setting up cyclic setoneshot to be used.
Let's take a look at the official description of the Animationdrawable class:
An object used to the Create Frame-by-frame animations, defined by a series of drawable objects, which can used as a View o Bject ' s background.
The simplest-to-create a frame-by-frame animation is-Define the animation in an XML file, placed in the RES/DRAWABL E/folder, and set it as the background to a View object. Then, call to run() start the animation.
An animationdrawable defined in XML consists of a single <animation-list> element, and a series of nested <item> tags. Each item defines a frame of the animation. See the example below.
Spin_animation.xml file in Res/drawable/folder:
<!--Animation Frames is wheel0.png--wheel5.png files inside the Res/drawable/folder--<animation-list Android Oid:id= "Selected" android:oneshot= "false" > <item android:drawable= "@drawable/wheel0" android:duration= " "/> <item android:drawable=" @drawable/wheel1 "android:duration=" "/> <item android: drawable= "@drawable/wheel2" android:duration= "/> <item android:drawable=" @drawable/wheel3 "Android: duration= "/> <item android:drawable=" @drawable/wheel4 "android:duration="/> <item android:drawable= "@drawable/wheel5" android:duration= "/> </animation-list>
Here's the code to load and play this animation.
Load the ImageView that would host the animation and//set its background to our animationdrawable XML resource. ImageView img = (ImageView) Findviewbyid (r.id.spinning_wheel_image); Img.setbackgroundresource (r.drawable.spin_animation); Get the background, which have been compiled to an Animationdrawable object. Animationdrawable frameanimation = (animationdrawable) img.getbackground (); Start the animation (looped playback by default). Frameanimation.start ()
Below we use frame animation to do a loading effect.
Prepare the material first, as follows:
Then create a new Animationdrawable object and load the images in. Addframe The first parameter represents the content to be loaded, and the second parameter represents the duration.
Code:
[Java]View Plaincopyprint?
- Frameanimation = new animationdrawable ();
- for (int i = 0; i < i++) {
- int id = getresources (). Getidentifier ("load" + (i+1), "drawable", this.getcontext (). Getpackagename ());
- Frameanimation.addframe (Getresources (). getdrawable (ID), 100);
- }
- //Set loop playback false indicates loop true means no loop, play only once
- Frameanimation.setoneshot (false);
The Animationdrawable class is a subclass of the Drawable class, so you can view the effect by setting animationdrawable as the background.
Finally, just call Frameanimation.start (); You can start the frame animation.
Fill it up another other.
Then the above is the use of Java code to load the frame animation, can also be like tween animation, using pure XML local file to do. The API has already been explained, this side no longer repeat.
Look directly at the example:
[XHTML]View Plaincopyprint?
- <? XML version= "1.0" encoding="Utf-8"?>
- <animaltion-list xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="false">
- <item android:drawable= "@drawable/load1" android:duration= " />
- <item android:drawable= "@drawable/load2" android:duration= " />
- <item android:drawable= "@drawable/load3" android:duration= " />
- <item android:drawable= "@drawable/load4" android:duration= " />
- <item android:drawable= "@drawable/load5" android:duration= " />
- <item android:drawable= "@drawable/load6" android:duration= " />
- <item android:drawable= "@drawable/load7" android:duration= " />
- <item android:drawable= "@drawable/load8" android:duration= " />
- <item android:drawable= "@drawable/load9" android:duration= " />
- <item android:drawable= "@drawable/load10" android:duration= " />
- </animaltion-list>
There is no clear point, welcome message exchange.
Attached parameter detailed description (from: http://www.moandroid.com/?p=790)
Table I. |
| properties [Type] |
Function |
|
| Duration[long] |
property is animation duration |
The time is measured in milliseconds |
| Fillafter [Boolean] |
When set to True, the animation conversion is applied after the animation is finished |
| Fillbefore[boolean] |
When set to True, the animation conversion is applied before the animation starts |
Interpolator |
Specifies the insertion of an animation |
There are some common inserts Accelerate_decelerate_interpolator Acceleration-deceleration animation insertion device Accelerate_interpolator Acceleration-Animation insertion Device Decelerate_interpolator Deceleration-Animation insertion device Other animations that belong to a specific animation effect |
| Repeatcount[int] |
Number of repetitions of an animation |
|
| Repeatmode[int] |
Define repetitive behavior |
1: Re-start 2:plays backward |
| Startoffset[long] |
The interval between animations, starting with how much time the last animation stopped to perform the next animation |
| Zadjustment[int] |
Defines the change of the z order of the animation |
0: Keep Z order unchanged 1: Stay on top -1: Stay on the lowest level |
Android Frame Animation