Android Animation series of Tween (Tween) animation detailed

Source: Internet
Author: User

Reprint please indicate source: http://blog.csdn.net/Airsaid/article/details/51591239
This article is from: Travel blog

    • Objective
    • Development environment
    • Properties of a tweened animation
      • Properties of the animation
      • Alpha Property
      • Rotate Property
      • Scale Property
      • Translate property
      • Animationset Property
    • Use of tweened animations
      • Using motion tweens in your code
      • The Tween animation resource is defined in XML animationdrawable
    • Tween Tween animation and Interpolator

Objective

The previous blog has written the use of frame-by-frames, which provides support for tweened (Tween) animations in addition to frame animation. tweened animation means that the developer only needs to specify "keyframes" such as the start and end of the animation, while the "middle frame" of the animation changes from the system. This is the word "tween".

The class that corresponds to the tweened animation is: Animation. Since animation is an abstract class, we need to use its subclasses when we use it, and the inheritance relationship of the tween animations is as follows:

Java class name XML Keyword Description Information
Scaleanimation <scale> Gradient Dimension Stretch animation effect
Translateanimation <translate> Move animation effect on picture transition position
Animationset <set> A container that holds alpah, scale, translate, rotate, or other set elements
Alphaanimation <alpha> Gradient Transparency Animation effect
Rotateanimation <rotate> Picture Transfer rotation animation effect

The following is a detailed introduction to the use of motion tweens.

Development environment
    • IDE Version: AndroidStudio2.0
    • Physical Machine Version: Win7 flagship edition (64-bit)
Properties of the animation property of a tweened animation
Java Methods XML Properties explain
Setdetachwallpaper (Boolean) Android:detachwallpaper Whether to run on wallpaper
Setduration (Long) Android:duration Set the duration of the animation in milliseconds
Setfillafter (Boolean) Android:fillafter Whether the control remains animated at the end of the control animation state
Setfillbefore (Boolean) Android:fillbefore Controls whether the control reverts to the state before the animation starts when the control animation ends
Setfillenable (Boolean) Android:fillenable (Boolean) Same as Android:fillbefore effect
Setinterpolator (Boolean) Android:interpolator Set the Interpolator (specified animation effects, such as: rebound, etc.)
Setrepeatcount (int) Android:repeatcount Number of repetitions
Setrepeatmode (int) Android:repeatmode Repeat type: Reverse reverse playback, restart play from the beginning
Setstartoffset (Long) Android:startoffset The time, in milliseconds, to wait for the open line to run after the start function is called
setzadjustment (int) Android:zadjustment Represents the position of the animated content when it is run on the z axis (top/bottom/normal), which defaults to normal

Since the animation class is its abstract parent class, we already have the above properties in either of the use of tweened animations.
The following are the specific properties of each tween animation:

Alpha Property
Java Methods XML Properties explain
Alphaanimation (float Fromalpha,...) Android:fromalpha The transparency at the beginning of the animation (0.0 to 1.0,0.0 is full transparent, 1.0 is opaque)
Alphaanimation (..., float toalpha) Androdi:toalpha Transparency of the end of the animation (IBID.)
Rotate Property
Java Methods XML Properties explain
Rotateanimation (float fromdegrees, ...) Android:fromdegress Rotation start angle, which is the clockwise degree, negative for counterclockwise
Rotateanimation (..., float todegress) Android:todegress Rotation End angle (IBID.)
Rotateanimation (..., float pivotx, ...) Android:pivotx The zoom start x coordinate (number, percent, percent p, for example, 50 indicates the initial point of the current view's upper-left coordinate plus 50px, 50% indicates the current view's upper-left corner plus the current view width of 50% as the initial point, 50% P indicates that the top left corner of the current view plus 50% of the parent control's height is the initial point)
Rotateanimation (...,........, Pivoty) Android:pivoty Zoom start y coordinate (IBID.)
Scale Property
Java Methods XML Properties explain
Scaleanimation (float FromX, ...) Android:fromxscale Initial x-axis scale, 1.0 means no change
Scaleanimation (..., float ToX) Android:toxscale End x-axis scale
Scaleanimation (..., float fromY, ...) Androd:fromyscale Initial y-axis scale
Scaleanimation (..., ..., float ToY) Android:toyscale End Y-axis scale
Scaleanimation (..., float pivotx, ...) Android:pivotx Zoom start x axis coordinates (IBID.)
Scaleanimation (..., float pivoty) Android:pivoty Zoom start y coordinate (IBID.)
Translate property
Java Methods XML Properties explain
Translateanimation (float Fromxdelta, ...) Android:fromxdelta Translation start point x axis coordinates
Translateanimation (..., float toxdelta) Android:toxdelta Translation end point x-axis coordinates
Translateanimation (..., float fromydelta, ...) Android:fromydelta Translation start point y-axis coordinates
Translateanimation (..., float toydelta) Android:toydelta Translation end point y-axis coordinates
Animationset Property

The Animationset class is special because the class is a combined container management class of 4 classes, without its own unique property, whose properties inherit from its parent class: Animation.
We need to be aware when we use it that when we set properties on the set tag, all the controls under its label will have an effect.

Use of tweened animations

We can use motion tweens in code as well as in XML, and it is recommended in the official Android document that we use XML files to define motion tweens because it is more readable, reusable, and supports swappable than hard-coding animations.

Using motion tweens in your code

The XML layout defines five buttons, respectively, for the following click events:

 Public voidAlpha (View v) {alphaanimation Anim =NewAlphaanimation (0.0f,1.0f); Anim.setduration ( +); V.startanimation (ANIM);} Public voidRotate (View v) {rotateanimation Anim =NewRotateanimation (0.0f, -360f, V.getwidth ()/2, V.getheight ()/2); Anim.setduration ( +); V.startanimation (ANIM);} Public voidScale (View v) {scaleanimation Anim =NewScaleanimation (1.0f,0.0f,0.0f,1.0f, V.getwidth ()/2, V.getheight ()/2); Anim.setduration ( +); V.startanimation (ANIM);} Public voidTranslate (View v) {translateanimation Anim =NewTranslateanimation (0.0f,10.0f,100f,200f); Anim.setduration ( +); V.startanimation (ANIM);} Public void Set(View v) {Animationset Anim =NewAnimationset ( This, null); Alphaanimation Alpha =NewAlphaanimation (0.0f,1.0f); Anim.setduration ( +); Rotateanimation rotate =NewRotateanimation (0.0f, -360f, V.getwidth ()/2, V.getheight ()/2); Anim.setduration ( +);    Anim.addanimation (Alpha);    Anim.addanimation (rotate); V.startanimation (ANIM);}

Operating effect:

Tween animation resources defined in XML (animationdrawable)

The XML resource that defines the tweened animation should be placed under the/res/anim/path, and when we create a good project, the default is not to have anim this directory, create one on your own.
The syntax format for setting up motion tweens is as follows:

<?xml version= "1.0" encoding= "Utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"android: Duration="duration"android:interpolator="@[package:]anim/interpolator_resource" android:shareinterpolator="boolean">                <Alphaandroid:fromalpha="float"android:toalpha="float" / >                    <scaleAndroid:fromxscale="float"android:fromyscale="float"  android:pivotx="float"android:pivoty="float"android: Toxscale="float"android:toyscale="float" />                                                    <translateandroid:fromxdelta="float"android:fromydelta="float"  Android:toxdelta="float"android:toydelta="float" />                                    <rotateandroid:fromdegrees="float"android:pivotx=" float"android:pivoty=" float"android:todegrees=" float " />                                 </Set>

The above syntax contains a lot of fromxx. Toxx. Properties, which define the start and end states of a view, and when scaling (scale), rotation transformations (rotate) are performed, you also specify the Pivotx, Pivoty two properties that specify the center point of the transformation, which is the point at which the scaling, rotation is based. Duration the duration of the specified animation.

In addition, the above <alpha../>、<scale../>、<transslate../>、<rotate../> can specify a Android:interpolator property, which specifies the speed at which the animation changes, which can be achieved by constant velocity, positive acceleration, negative acceleration, irregular acceleration, etc. The R.anim class of the Android system contains a number of constants that define different animation speeds, including:
* Linear_interpolator: Constant speed conversion.
* Accelerate_interpolater: Accelerated transformation.
* Decelerate_interpolater: deceleration transformation.
The following is a detailed explanation of the use of interpolator.

Tweening (Tween) animations and interpolator

Interpolator in the school when the translation to: "Interpolator." It is specifically designed to control how many frames need to be dynamically "filled in" during the animation, specifically when the animation is running.
Interpolator calculates the density and position of the dynamically inserted frames required for the entire animation based on the specified algorithm. In short, interpolator is responsible for controlling the speed of animation changes, which makes the basic animation effects (Alpha, scale, Translate, Rotate) change at a constant speed, acceleration, deceleration, parabolic, and many other speeds.

Interpolator is an interface that defines the methods that all interpolator must implement: float Getinterpolator (float input), Developers can completely control the speed of animation by implementing Interpolator. Android provides the following implementation classes for Interpolator, which are used to achieve different speed of animation changes, respectively.

    • Linearinterpolator: The animation changes at a constant speed.
    • Accelerateinterpolator: Changes slowly at the beginning of the animation, and then starts to speed up.
    • Acceleratedecelerateinterpolator: At the beginning of the animation, where the end of the slow speed, the middle of the time to accelerate.
    • Cycleinterpolator: The animation loop plays the specified number of times and the speed of change is changed by sinusoidal.
    • Decelerateinterpolator: Change speed at the beginning of the animation and start slowing down.

The above Interpolator Demo animation resource file:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="3000"    android:interpolator="@android:anim/linear_interpolator">    <translate        android:fromYDelta="0"        android:toYDelta="1000" /></set>

The animation resource file is loaded in code with the Animationutils.loadanimation () method:

Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_interpolator);mImage.startAnimation(animation);

Continue next: Customizing motion Tweens

Android Animation series of Tween (Tween) animation detailed

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.