Android Frame Animation

Source: Internet
Author: User

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?
  1. Frameanimation = new animationdrawable ();
  2. for (int i = 0; i < i++) {
  3. int id = getresources (). Getidentifier ("load" + (i+1), "drawable", this.getcontext ().  Getpackagename ());
  4. Frameanimation.addframe (Getresources (). getdrawable (ID), 100);
  5. }
  6. //Set loop playback false indicates loop true means no loop, play only once
  7. 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?
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <animaltion-list xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:oneshot="false">
  4. <item android:drawable= "@drawable/load1" android:duration= " />
  5. <item android:drawable= "@drawable/load2" android:duration= " />
  6. <item android:drawable= "@drawable/load3" android:duration= " />
  7. <item android:drawable= "@drawable/load4" android:duration= " />
  8. <item android:drawable= "@drawable/load5" android:duration= " />
  9. <item android:drawable= "@drawable/load6" android:duration= " />
  10. <item android:drawable= "@drawable/load7" android:duration= " />
  11. <item android:drawable= "@drawable/load8" android:duration= " />
  12. <item android:drawable= "@drawable/load9" android:duration= " />
  13. <item android:drawable= "@drawable/load10" android:duration= " />
  14. </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

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.