/** * Crazyandcoder * Contact: * QQ : 275137657 * Email: [Email protected] * Reprint please indicate the source! */
Specific explanations for Android animation properties
The animation properties in Android are divided into four main categories: alpha, scale, translate, rotate. We are now going to know exactly what each type means. Then define your own animations based on the requirements of the project.
- Animated effect of alpha transparency gradient
- Scale size shrink gradient animation effect
- Animation effect of translate image conversion position
- Rotate picture Transfer rotation animation effect
Loading animations There are two ways we can do it, one is the way XML is laid out, and the other is pure Java code. Both approaches have advantages. Depending on the need to be able to choose. First we look at the way XML defines the animation properties, you need to create a new folder under the Res folder Anim, the detailed path is Res/anim, inside a new Img_anim.xml file.
<?xml version= "1.0" encoding= "Utf-8"?><set> <alpha/> <rotate/> <scale/> <translate/></set>
Next we understand the contents of each of these attributes:
animated effect of Alpha transparency gradient
- Fromalpha the transparency at the beginning of the animation, the number of float types between 0.0~1.0, 0.0 for total transparency, and 1.0 for opaque
- Toalpha transparency at the end of the animation, IBID.
- Interpolator animation display speed, can be accelerated, constant speed and other states.
- Duration duration of animation
scale size shrink gradient animation effect
- The Fromyscale represents the scale relative to itself in the Y direction of the starting point. 0.5 zoom in half, 1.0 no change, 2.0 magnification one times
- The Fromxscale represents the scale relative to itself in the x direction of the beginning.
- Toxscale and Toyscale are relative to the above. Represents the end of the X-, y-direction of the scale
- Pivotx,pivoty represents the center point X, y position of the zoom,
Let's take a simple example: starting from the center point of the image, the initial image is scaled in the xy direction to half the original, then magnified to twice times the original. The scaling process lasts for 5 seconds. The code is:
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <scale android:duration= "android:fromxscale=" 0.1 " android:fromyscale=" 0.1 " android:pivotx= "0.5" android:pivoty= "0.5" android:repeatcount= "" " android:toxscale=" 2.0 " android:toyscale= "2.0"/></set>
Can see that only the dog has a small to large change process, because it is in the mobile phone simulation animation, so the scaling process is not very smooth, it is recommended to test the real machine, the effect is better.
Rotate Picture Transfer rotation animation effect
- Fromdegrees starts from the initial angle, the unit is "°"
- The degree at the end of the Todegrees
We can simulate the animation of loading progress, just need a picture, and then rotate it with its center point. We can.
<?XML version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <rotate android:duration= "android:fromdegrees=" 0 " android:pivotx=" 50% " android: Interpolator= "@android: Anim/linear_interpolator" android:pivoty= "50%" android:repeatcount= "-1" Android:todegrees= "/></set>"
Main class file code:
Package Com.example.animationsdemo;import Android.app.activity;import Android.os.bundle;import android.view.Menu; Import Android.view.menuitem;import Android.view.animation.animation;import android.view.animation.AnimationUtils Import Android.widget.imageview;public class Mainactivity extends Activity {private ImageView mimganim;private Animation manimation; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate ); Setcontentview (R.layout.activity_main); mimganim= (ImageView) Findviewbyid (R.id.img_anim); manimation= Animationutils.loadanimation (this, r.anim.img_anim); Mimganim.startanimation (manimation);}}
Implementation Results Demo:
Because it is the emulator, plus the conversion to GIF format so the middle of a bit of a phenomenon. Direct real machine test is recommended.
Animation effect of Translate image conversion position
- Fromxdelta represents the position of x in the starting direction
- Fromydelta represents the position of y in the starting direction
- Toxdelta represents the position of x at the end of the direction
- Toydelta represents the position of Y at the end of the direction
Let's implement a horizontally moving animation
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <translate android:duration= "android:fromxdelta=" 0% " android:fromydelta=" 0% " android:repeatcount= "-1" android:toxdelta= "200%" android:toydelta= "0%"/></set>
The effect is:
Public properties:
- RepeatCount the number of times the animation repeats, 1 represents the wireless loop
- The Fillafter hypothesis is set to True to stop the animation when it ends and to keep the state at the end of the animation.
- Duration duration of animation
Android Animation XML properties specific explanation