An overview of the

The Android SDK introduces two kinds of Animation:

  1. Tween Animation: It produces Animation effects by constantly transforming images of objects in the scene (translation, scaling, rotation), which is a kind of gradient Animation, or Tween Animation.
  2. Frame Animation: Play pre-made images sequentially. Frame Animation is a screen transition Animation, or Frame by Frame Animation.

The gradient of animation

4 gradient animations

Alpha Gradient transparency Animation Effect Scale Gradient size Zoom Animation Effect Translate screen shift position move animation effect Rotate screen shift rotate animation effectCopy the code

Steps to achieve animation:

1. Prepare an animation object, which can be thought of as an animation object that describes (encapsulates) the style of the animation. We can create these objects manually in code, corresponding to the four animaiton object classes:

AlphaAnimation Gradient Transparency Animation Effect ScaleAnimation Gradient Size Scaling animation effect TranslateAnimation transition position moving animation effect RotateAnimation transition rotation animation effectCopy the code

XML way

I could also write an XML file describing the animation and place it in the Anim folder of the resources file. Then, load the file with this description in the code:

int animationSrouceId = 0; / / resource file ID of the Animation ani1 = AnimationUtils. LoadAnimation (getApplicationContext (), animationSrouceId); return ani1;Copy the code

2. Specify the startAnimation for the view control and call startAnimation to do so.

Imageview_imageview1; _imageView1 = (ImageView)findViewById(R.id.imageView1); _imageView1.start Animation(ani1);Copy the code

The following is the DEMO screenshot I made, the animation style is difficult to screenshot. I’ll put the source code at the end of this article.

\

Below is an ANIMATION described by XML

Transparent Alpha effect code:

<? The XML version = "1.0" encoding = "utf-8"? > < set XMLNS: android = "http://schemas.android.com/apk/res/android" > < alpha android: fromAlpha = "0.3" android: toAlpha = "1.0"  android:duration="2000" /> <! Alpha floating point value: fromAlpha property is the transparency at the beginning of the animation toAlpha property is the transparency at the end of the animation </set> </set> </set> </set>Copy the code

Rotation (rotate)

<? The XML version = "1.0" encoding = "utf-8"? > <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0" android:toDegrees="+350"  android:pivotX="50%" android:pivotY="50%" android:duration="3000" /> <! -- rotate Animation effect property: interpolator Specifies an animation interpolator In my experiments, I found three animation interpolators using resources in Android.res.anim: Accelerate_interpolator Accelerate_interpolator animation interpolator. Decelerate_interpolator Animation interpolator The fromDegrees property is the Angle of the object at the beginning of the animation. The toDegrees property is the Angle of the object at the end of the animation can be greater than 360 degrees. When the Angle is negative -- indicates counterclockwise rotation when the Angle is positive -- indicates clockwise rotation (negative from -- to positive: clockwise rotation) (negative from -- to negative: counterclockwise rotation) (positive from -- to positive: clockwise rotation) The pivotX property is the starting position of the animation relative to the X coordinate of the object. The values range from 0% to 100%. 50% is the midpoint of the X or Y coordinates of the object.Copy the code

Scale

<? The XML version = "1.0" encoding = "utf-8"? > <set xmlns:android="http://schemas.android.com/apk/res/android"> <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 :pivotX="50%" Android :fillAfter="false" android:duration="700" /> </set> <! In my experiments, using resources in Android.res.anim, I have found three kinds of animation interpolators: Accelerate_interpolator Accelerate_interpolator animation interpolator. Decelerate_interpolator Animation interpolator Other floating-point values belong to specific animation effects: The fromXScale property is the scaling size in X coordinates at the beginning of the animation. The toXScale property is the scaling size in X coordinates at the end of the animation. The fromYScale property is the scaling size in Y coordinates at the beginning of the animation. Values of the above four attributes: 0.0 indicates that the animation has shrunk to no. 1.0 indicates that the animation is normal. A value smaller than 1.0 indicates that the animation has shrunk to a value greater than 1.0 indicates that the animation has been enlarged. Values range from 0% to 100%. 50% is the midpoint of the X or Y coordinates of the object. Long integer: duration Specifies the duration of the animation. FillAfter property When set to true, the animation conversion is applied after the animation ends -->Copy the code

Displacement (translate)

<? The XML version = "1.0" encoding = "utf-8"? > <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="50" android:fromYDelta="0" android:toYDelta="50" android:duration="2000" android:fillAfter="true" /> <! Translate position transfer animation Integer values: The fromXDelta property is the position on the x-coordinate at the start of the animation. The toXDelta property is the position on the x-coordinate at the end of the animation. The fromYDelta property is the position on the y-coordinate at the start of the animation. If fromXType toXType fromYType toYType is not specified, the default is to use itself as the relative reference. Long integer value: duration specifies the animation duration.Copy the code

Frame Animation

Frame-by-frame animation is the process of showing multiple images in sequence to create a dynamic effect. Effect demonstration:

\

1. Prepare several consecutive images and write an animation description file (create a new XML file under the Anim Resources folder).

<? The XML version = "1.0" encoding = "utf-8"? > <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> <item android:drawable="@drawable/c1" android:duration="200" /> <item android:drawable="@drawable/c2" android:duration="200" /> <item android:drawable="@drawable/c3" android:duration="200" /> <item android:drawable="@drawable/c4" android:duration="200" /> <item android:drawable="@drawable/c5" android:duration="200" /> <item android:drawable="@drawable/c6" android:duration="200" /> </animation-list>Copy the code

2. Place an ImageView control in the form and write it in code

_imageView1 = (ImageView)findViewById(R.id.imageView1); Put the ImageView controls / / / / set the animation background _imageView1. SetBackgroundResource (state Richard armitage nim. Animation_list); Animaition = (AnimationDrawable) _imageView1.getBackground();Copy the code

3. Start the animation

_animaition.setOneShot(false); // Start only once? If (_animaition.isrunning ())// isRunning? { _animaition.stop(); } _animaition.start(); / / startCopy the code

Gradient Animation – Source code Download Frame – Source code download

reference

www.cnblogs.com/feisky/arch… www.eoeandroid.com/forum.php?m…