Android animation Learning

Source: Internet
Author: User

With a deep understanding of drewable, we found that drawable is more powerful: Display animation. Android SDK introduces two types of Animation:

  • Tween animation: animated effects are generated by constantly performing image transformations (translation, scaling, and rotation) on objects in the scenario.
  • Frame Animation: plays images prepared in advance in sequence, similar to movies.

Before using animation, we should first learn how to define animation, which will be of great help for us to use animation. Animation is defined in XML format. The defined XML file is stored in RES/anim. Since the definitions and usage of tween animation and frame animation are quite different, we will introduce them separately. This article will introduce the definition and use of tween animation, frame Animation will be detailed later. Based on the structure of the XML document [parent node, child node, and attribute], we will introduce tween animation, which consists of four types:

  • ALPHA: gradient transparency animation effect
  • Scale: gradient scaling animation effect
  • Translate: animation effect for moving the position of the screen
  • Rotate: animation effect for moving the position of the screen

Before introducing the above four types, we will first introduce the common node attributes of tween animation. For the node naming principles, please read the androidmanifest. xml file structure description.

Table 1

Attribute [type] Function  
Duration [long] Attribute is animation duration Time in milliseconds
Fillafter [Boolean] If it is set to true, the animation will be applied after the animation ends.
Fillbefore [Boolean] If this parameter is set to true, the animation is applied before the animation starts.

Interpolator

Specifies an animation insertor. There are some common plug-ins
Accelerate_decelerate_interpolator
Accelerator-slowdown animation inserter
Accelerate_interpolator
Acceleration-animation insertor
Decelerate_interpolator
Slowdown-animation inserter
Other animation Effects
Repeatcount [int] Number of animation repetitions  
Repeatmode [int] Define repeated Behaviors 1: Start again 2: plays backward
Startoffset [long] The time interval between the animation, starting from the time when the animation was stopped
Zadjustment [int] Changing the Z order of an animation 0: Keep Z order unchanged
1: Keep at the top
-1: Keep at the bottom layer

After reading the above nodes, do you want to start defining animations. Next we will introduce the unique node elements of the four types based on specific examples.

Table 2

XML Node Function Description
Alpha Gradient transparency animation effect
<Alpha
Android: fromalpha = "0.1 ″
Android: toalpha = "1.0 ″
Android: Duration = "3000"/>
Fromalpha

Transparency when attribute is animation start

0.0 indicates completely transparent
1.0 indicates completely opaque
The above value is a float data type number between 0.0 and 1.0.
Toalpha The property is the transparency when the animation ends.

Table 3

Scale Gradient scaling animation effect
<Scale
Android: interpolator = "@ Android: anim/accelerate_decelerate_interpolator"
Android: fromxscale = "0.0 ″
Android: toxscale = "1.4 ″
Android: fromyscale = "0.0 ″
Android: toyscale = "1.4 ″
Android: Required Tx = "50%"
Android: Required ty = "50%"
Android: fillafter = "false"
Android: startoffset = "700"
Android: Duration = "700 ″
Android: repeatcount = "10"/>
Fromxscale [float] fromyscale [float] It is the scaling size on the X and Y coordinates when the animation starts. 0.0 indicates shrinking to none
1.0 indicates normal scaling-free
A value smaller than 1.0 indicates contraction.
Value greater than 1.0 indicates Amplification
Toxscale [float]
Toyscale [float]
It is the scaling size on the X and Y coordinates when the animation ends.
Pivotx [float]
Ty [float]
It is the starting position of the animation relative to the X and Y coordinates of the object. Attribute value description: from 0%-100%, 50% is the midpoint position on the X or Y coordinate of the object.
       

Table 4

Translate Animation Effect
<Translate
Android: fromxdelta = "30 ″
Android: toxdelta = "-80 ″
Android: fromydelta = "30 ″
Android: toydelta = "300 ″
Android: Duration = "2000"/>
Fromxdelta
Toxdelta
It is the position on the X coordinate at the animation end and start.  
Fromydelta
Toydelta
It is the position on the Y coordinate of the animation and the start point of the animation.  
       

Table 5

Rotate Animation effects of screen transfer and Rotation
<Rotate
Android: interpolator = "@ Android: anim/accelerate_decelerate_interpolator"
Android: fromdegrees = "0 ″
Android: todegrees = "+ 350 ″
Android: Required Tx = "50%"
Android: Required ty = "50%"
Android: Duration = "3000"/>
Fromdegrees It is the angle of the object when the animation starts. Description
If the angle is negative, it indicates clockwise rotation.
If the angle is positive, it indicates clockwise rotation.
(Negative from -- to positive: clockwise rotation)
(Negative from -- to negative: counter-clockwise rotation)
(Positive from -- to positive: clockwise rotation)
(Positive from -- to negative: clockwise rotation)
Todegrees The Rotation Angle of an object can be greater than 360 degrees when the animation ends.
Pivotx
Ty
It is the starting position of the animation relative to the X and Y coordinates of the object. Note: The preceding two attribute values are from 0% to 100%.
50% indicates the point position on the X or Y coordinate of the object.
       

After reading the above example, I think you may also want to see what the effects of these defined animations are. The following shows the animation in the scale example. The interface changes are as follows:

     

After learning the definition of tween animation according to the above description, I have a detailed understanding of tween animation, and then I will learn about the android SDK's animation package (Android. view. it provides all the classes used to operate the tween animation.

The android SDK provides a base class: animation, which contains a large number of set/getxxxx () functions to set and read the attributes of animation, that is, the attributes displayed in the previous table. Tween animation consists of four types: Alpha, scale, translate, and roate. Corresponding classes are provided in the android SDK, the animation class derives alphaanimation, scaleanimation, translateanimation, and rotateanimation animations that implement translation, rotation, and Alpha value change. each subclass adds its own unique attributes on the basis of its parent class. Let's take a look at the constructors of these classes. Are they exactly the same as the attributes defined in Table 2, table 3, table 4, and table 5.

After learning about the definition of tween animation and having some basic knowledge about Android. View. animation, I will introduce how to use tween animation. The android SDK provides two methods: Read the animation directly from the XML resource, and use the constructor of the animation subclass to initialize the animation object, the second method knows how to use it after reading the descriptions of various classes in the android SDK. The following briefly describes the process of reading animation from XML resources and developing apps, the following describes the entire process of use:

  1. Create an android project;
  2. Import an image resource;
  3. Replace textview in res/layout/Main. XML with imageview;
  4. Create a new folder anim under Res and define the animation XML file under this folder;
  5. Modify the code in oncreate () to display the animation resources;

The key code is parsed as follows:
// Imageview in Main. xml
Imageview spaceshipimage = (imageview) findviewbyid (R. Id. spaceshipimage );
// Load the animation
Animation hyperspacejumpanimation =
Animationutils. loadanimation (this, R. anim. hyperspace_jump );
// Use imageview to display animations
Spaceshipimage. startanimation (hyperspacejumpanimation );
The source code of the project is provided for download. The brief analysis here is as follows:

  • Animationutils provides the animation loading function. In addition to the loadanimation () function, go to the android SDK for details;
  • The so-called animation is to make a graphical transformation to the View content;
Summary

After reading this long article, I have a deep understanding of the definition and usage of tween animation. Due to the limited space, I have omitted a lot of content in the android SDK, for example, interpolator needs to be carefully read in the android SDK.

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.