Android Tween Animations

Source: Internet
Author: User

The advantage of motion tweens is that you can save space. Motion tweens are inherently different from frame-by-frames animations, which simulate the effect of animation by continuously playing a picture, while motion tweens are achieved by adding a gradient animation between two keyframes. There are 5 types of motion tweens supported by the Android application framework at this time. Specifically implemented in the Android.view.animation class library.

(1) Alphaanimation:

Transparency (Alpha) gradient effect, corresponding to <alpha/> label.

(2) Translateanimation:

Displacement gradient, you need to specify the starting and ending coordinates of the moving point, corresponding to the <translate/> tag.

(3) Scaleanimation:

Zoom gradient, you can specify the reference point of the zoom, corresponding to the <scale/> label.

(4) Rotateanimation:

Rotate the gradient to specify a reference point for the rotation, corresponding to the <rotate/> label.

(5) Animationset:

Combination of gradients, support for combining various gradient effects, corresponding <set/> tags.

The effects of motion tweens can also be defined using an XML file, which is placed in the res/anim/directory of the Android project.

Instance:

 Public classMainactivityextendsActivity {PrivateImageView IV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);            Setcontentview (R.layout.activity_main); IV=(ImageView) Findviewbyid (R.ID.IV); }         Public voidClick1 (View v) {//how-javacode the gradient effect is implementedAlphaanimation ani =NewAlphaanimation (0.0f, 1.0f); Ani.setduration (2000); Ani.setrepeatcount (2);            Ani.setrepeatmode (Animation.reverse);        Iv.startanimation (ANI); }         Public voidclick11 (View v) {//how-xml the gradient effect is implementedAnimation ani = animationutils.loadanimation ( This, R.anim.alpha_anim);        Iv.startanimation (ANI); }         Public voidClick2 (View v) {//Scaling Gradient-javacode Implementation methodScaleanimation ani =NewScaleanimation (0.0f, 2.0f, 0.0f, 2.0f, Animation.relative_to_self,0.5f, Animation.relative_to_self,0.5f); Ani.setduration (2000); Ani.setrepeatcount (2);            Ani.setrepeatmode (Animation.reverse);        Iv.startanimation (ANI); }         Public voidclick22 (View v) {//Scaling Gradient-xml Implementation methodAnimation ani= Animationutils.loadanimation ( This, R.anim.scale_ani);        Iv.startanimation (ANI); }         Public voidClick3 (View v) {//-javacode Implementation of rotation gradient effectRotateanimation ani =NewRotateanimation (0, 360, Animation.relative_to_self,0.5f, Animation.relative_to_self,0.5f); Ani.setduration (2000); Ani.setrepeatcount (2);            Ani.setrepeatmode (Animation.reverse);        Iv.startanimation (ANI); }         Public voidclick33 (View v) {//-xml Implementation of rotation gradient effectAnimation ani = animationutils.loadanimation ( This, R.anim.rotate_ani);        Iv.startanimation (ANI); }         Public voidClick4 (View v) {//-javacode Realization of displacement gradient effectTranslateanimation ani =Newtranslateanimation (animation.relative_to_parent,0.0f, Animation.relative_to_parent,1.0f, Animation.relative_to_parent,0.0f, Animation.relative_to_parent,1.0f); Ani.setduration (2000); Ani.setrepeatcount (2);            Ani.setrepeatmode (Animation.reverse);        Iv.startanimation (ANI); }         Public voidclick44 (View v) {//-xml Realization of displacement gradient effectAnimation ani = animationutils.loadanimation ( This, r.anim.translate);        Iv.startanimation (ANI); }    }
View Code

The corresponding XML animation effect file

Alpha_anim.xml

<? XML version= "1.0" encoding= "Utf-8" ?> <  xmlns:android= "http://schemas.android.com/apk/res/android"     android: Fromalpha= "1.0"    android:toalpha= "0.5"    Android:fillafter  = "true"    android:duration= "$"></ Alpha >

Code Description:

Transparency Controls Animation effect Alpha

Floating-point values:

The Fromalpha property is the transparency at the beginning of the animation

Toalpha property is the transparency at the end of the animation

Description

0.0 indicates full transparency

1.0 means completely opaque

The above values take a number of float data types between 0.0-1.0

Long Integer value:

The Duration property is the duration of the animation

Description

The time is measured in milliseconds

Rotate_ani.xml

<?XML version= "1.0" encoding= "Utf-8"?><Rotatexmlns:android= "Http://schemas.android.com/apk/res/android"android:fromdegrees= "0"android:todegrees= " the"Android:pivotx= "50%"Android:pivoty= "50%"android:duration= "$" ></Rotate>

Code Description:

Fromdegrees: Indicates the starting angle of rotation

Todegrees: Indicates the end angle of the rotation

RepeatCount: Number of rotations The default value is 0 for rotation 1 times if the value is repeatcount=4 rotated 5 times, with a value of 1 or infinite, the motion tween will never stop

Repeatmode set the repeating mode. The default is restart. Valid when the value of RepeatCount is greater than 0 or infinite.

Repeatcount=-1 or infinite loops can also be set to reverse, which means that an even number of times the animation is animated in a direction opposite to the direction defined by the animation file.

Scale_ani.xml

<?XML version= "1.0" encoding= "Utf-8"?>< Scalexmlns:android= "Http://schemas.android.com/apk/res/android"Android:fromxscale= "0.2"Android:toxscale= "2.0"Android:fromyscale= "0.2"Android:toyscale= "2.0"Android:fillafter= "true"android:duration= "$" ></ Scale>

Code Description:

Fromxscale: Represents the starting scale scaled along the x-axis

Toxscale: Represents the end scale scaled along the x-axis

Fromyscale: Represents the starting scale scaled along the y-axis

Toyscale: Represents the end scale scaled along the y-axis

Picture Center point:

android:pivotx= "50%"

Android:pivoty= "50%"

Translate.xml

<?XML version= "1.0" encoding= "Utf-8"?><Translatexmlns:android= "Http://schemas.android.com/apk/res/android"Android:fromxdelta= "20%p"Android:toxdelta= "50%p"Android:fromydelta= "0"Android:toydelta= "50%p"android:duration= "$"Android:repeatcount= "2"Android:repeatmode= "Reverse" ></Translate>

Code description

Renderer for Android:interpolator animations

Accelerate_interpolator (animation accelerator) makes the animation slowest at the beginning and then gradually accelerates

Decelerate_interpolator (animation reducer) makes the animation the fastest at the beginning, and then gradually slows down

Accelerate_decelerate_interpolator (animation acceleration reducer)

Intermediate position layering: make the animation slowest at the beginning and then gradually accelerate

Make the animation the fastest at the beginning, and then gradually slow down the end of the slowest position

The horizontal axis of the Fromxdelta animation starting position

Toxdelta the horizontal axis of the ending position of the animation

Fromydelta the ordinate of the starting position of the animation

Toydelta the ordinate of the end position of the animation

Duration duration of animation

Reference Address:

http://songfantasy.iteye.com/blog/1258980

Http://www.cnblogs.com/yydcdut/p/3829860.html

Related Article

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.