Animation use & mdash; View Animation, Animation viewanimation

Source: Internet
Author: User

Animation use-View Animation, Animation viewanimation

View Animation defines the following four Animation effects:

Scale, translation, rotation, and alpha)

 

Scaling Animation:

ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)

See the constructor of ScaleAnimation. The meaning of each parameter is clear.

FromX: The width before scaling the object.

ToX: The size of object x to be scaled

The other two parameters with Y are similar.

 

Displacement Animation:

TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

FromXDelte: X coordinate before the object executes the animation

ToXDelte: The x coordinate of the moving target when the object is stopped.

 

Rotation Animation:

RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)

FromDegrees: angle before Rotation

ToDegrees: How many degrees the object should be rotated

The last two parameters are used to control the rotation center.

 

Transparent Animation:

AlphaAnimation(float fromAlpha, float toAlpha)

The values of the two parameters can only be between 0 and 1, 0 is completely transparent, 1 and not transparent

 

Instance code:

Package com. whathecode. viewanimation; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. view. view; import android. view. animation. alphaAnimation; import android. view. animation. animationSet; import android. view. animation. cycleInterpolator; import android. view. animation. rotateAnimation; import android. view. animation. scaleAnimation; import android. view. animation. translateAnimation; import android. widget. imageView; public class MainActivity extends ActionBarActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void onclickMove (View view) {ImageView img = (ImageView) findViewById (R. id. image); TranslateAnimation ta = new TranslateAnimation (0, 10, 0, 0); ta. setInterpolator (new CycleInterpolator (7f); ta. setDuration (1, 1000); img. startAnimation (ta);} public void onclickScale (View view) {ImageView img = (ImageView) findViewById (R. id. image);/*** set the zooming center value */int zooming Tx = img. getWidth ()/2; int equalty = img. getHeight ()/2; // initialize the scaling object ScaleAnimation sa = new ScaleAnimation (1f, 0.5f, 1f, 0.5f, 0000tx, 0000ty); // set the interpolation device, it is used to control the animation behavior. Here, it controls the number of animation repetitions 3 sa. setInterpolator (new CycleInterpolator (3f); // The animation speed sa. setDuration (1, 1500); img. startAnimation (sa);} public void onclickRotate (View view) {ImageView img = (ImageView) findViewById (R. id. image); int destination Tx = img. getWidth ()/2; int equalty = img. getHeight ()/2; RotateAnimation ra = new RotateAnimation (0, 360f, 0000tx, 0000ty); ra. setDuration (1, 1000); img. startAnimation (ra);} public void onclickAlpha (View view) {ImageView img = (ImageView) findViewById (R. id. image); AlphaAnimation aa = new AlphaAnimation (1f, 0.5f); aa. setDuration (1, 1500); img. startAnimation (aa);} public void startAnimSet (View view) {ImageView img = (ImageView) findViewById (R. id. image); int destination Tx = img. getWidth ()/2; int equalty = img. getHeight ()/2; RotateAnimation ra = new RotateAnimation (0, 360f, 0000tx, 0000ty); ra. setDuration (1000); AlphaAnimation aa = new AlphaAnimation (1f, 0.5f); aa. setDuration (1500); // Initialize an empty animation set AnimationSet as = new AnimationSet (false); // Add an animation as to the set. addAnimation (ra);. addAnimation (aa); img. startAnimation ();}}

Effect:

<? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android" android: interpolator = "@ [package:] anim/interpolator_resource" android: extends interpolator = ["true" | "false"]> <alpha android: fromAlpha = "float" android: toAlpha = "float"/> <scale android: fromXScale = "float" android: toXScale = "float" android: fromYScale = "float" android: toYScale = "float" android: paitx = "float" android: required ty = "float"/> <translate android: fromXDelta = "float" android: toXDelta = "float" android: fromYDelta = "float" android: toYDelta = "float"/> <rotate android: fromDegrees = "float" android: toDegrees = "float" android: effectx = "float" android: ty = "float"/> <set>... </set>

Instance code:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="false">    <translate         android:fromXDelta="0"        android:toXDelta="200"        android:duration="1500"/>        <alpha         android:fromAlpha="1"        android:toAlpha="0.5"        android:duration="1500"        /></set>

How to load resources:

Public void loadAnimRes (View view) {// use the loadAnimationUtils class to load the animation resource AnimationSet as = (AnimationSet) AnimationUtils. loadAnimation (this, R. anim. animset); img. startAnimation ();}

Effect:

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.