This article summarizes the commonly used attribute methods, and so on, the detailed study can use the following Guo Lin great God article:
Android Property Animation complete parsing (top), basic usage of initial knowledge property animation
Advanced usage of Valueanimator and objectanimator for full parsing (medium) of Android property animations
Android Property Animation full parsing (bottom), Interpolator and Viewpropertyanimator usage
Views Animation (View animation)
The view animation is divided into two seed animations tween animation and frame animation, which describe their usage respectively below.
Frame animation of view Animation (frame Animation)
Create Res/anim/sd_anim.xml
<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false"><item android:drawable="@mipmap/img_1" android:duration="80" /><item android:drawable="@mipmap/img_2" android:duration="80" /><item android:drawable="@mipmap/img_3" android:duration="80" />...</animation-list>
In the XML layout
<ImageView android:id="@+id/img_show" android:layout_width="120dp" android:layout_height="120dp" android:background="@anim/miao_gif" />
Use
ImageView img_show = (ImageView) findViewById(R.id.img_show);AnimationDrawable anim = (AnimationDrawable) img_show.getBackground();anim.start();//开始帧动画anim.stop();//停止帧动画
Motion Tween (view Animation) animation (Tween Animation) alphaanimation (transparency gradient)
Anim_alpha.xml:
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromAlpha="1.0" android:toAlpha="0.1" android:duration="2000"/>
Attribute Explanation:
- Fromalpha: Starting Transparency
- Toalpha: Ending transparency
- The range of transparency is: 0-1, fully transparent-completely opaque
Scaleanimation (Scaling gradient)
Anim_scale.xml:
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromXScale="0.2" android:toXScale="1.5" android:fromYScale="0.2" android:toYScale="1.5" android:pivotX="50%" android:pivotY="50%" android:duration="2000"/>
Attribute Explanation:
- Fromxscale/fromyscale: The starting scale for scaling along the x-axis/y Axis
- Toxscale/toyscale: Scaled end scale along the x-axis/y Axis
- Pivotx/pivoty: The pivot point x/y coordinates of the scale, that is, the position of the left edge of the image, such as 50% is centered on the center of the Axis point
Translateanimation (Displacement gradient)
Anim_translate.xml:
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXDelta="0" android:toXDelta="320" android:fromYDelta="0" android:toYDelta="0" android:duration="2000"/>
Attribute Explanation:
- Fromxdelta/fromydelta: x/y coordinates of the starting position of the animation
- Toxdelta/toydelta: x/y coordinates of end position of animation
Rotateanimation (rotate gradient)
Anim_rotate.xml:
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0" android:toDegrees="360" android:duration="1000" android:repeatCount="1" android:repeatMode="reverse"/>
Attribute Explanation:
- Fromdegrees/todegrees: Start/end angle of rotation
- RepeatCount: The number of rotations, the default value is 0, represents one time, if it is another value, such as 3, then rotated 4 times in addition, a value of 1 or infinite indicates that the animation never stops
- Repeatmode: Set repeat mode, default restart, but only valid if RepeatCount is greater than 0 or infinite or-1 o'clock. It can also be set to reverse, which means the opposite movement is done when an even number of times the animation is displayed!
Animationset (combination gradient)
Anim_set.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" android:shareInterpolator="true" > <scale android:duration="2000" android:fromXScale="0.2" android:fromYScale="0.2" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.5" android:toYScale="1.5" /> <rotate android:duration="1000" android:fromDegrees="0" android:repeatCount="1" android:repeatMode="reverse" android:toDegrees="360" /> <translate android:duration="2000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="320" android:toYDelta="0" /> <alpha android:duration="2000" android:fromAlpha="1.0" android:toAlpha="0.1" /></set>
Android development will inevitably involve animation aspects of the effect, then you will encounter a problem, if the control animation start speed, and end speed. There are some other effects.
Control animation speed (animation differential)
Setting properties in XML
android:interpolator="@android:anim/accelerate_interpolator" 设置动画为加速动画(动画播放中越来越快)android:interpolator="@android:anim/decelerate_interpolator" 设置动画为减速动画(动画播放中越来越慢)android:interpolator="@android:anim/accelerate_decelerate_interpolator" 设置动画为先加速在减速(开始速度最快 逐渐减慢)android:interpolator="@android:anim/anticipate_interpolator" 先反向执行一段,然后再加速反向回来(相当于我们弹簧,先反向压缩一小段,然后在加速弹出)android:interpolator="@android:anim/anticipate_overshoot_interpolator" 同上先反向一段,然后加速反向回来,执行完毕自带回弹效果(更形象的弹簧效果)android:interpolator="@android:anim/bounce_interpolator" 执行完毕之后会回弹跳跃几段(相当于我们高空掉下一颗皮球,到地面是会跳动几下)android:interpolator="@android:anim/cycle_interpolator" 循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2* mCycles* Math.PI* input)android:interpolator="@android:anim/linear_interpolator" 线性均匀改变android:interpolator="@android:anim/overshoot_interpolator" 加速执行,结束之后回弹
Set in code with the same order effect
animation.setInterpolator(new AccelerateInterpolator());animation.setInterpolator(new DecelerateInterpolator());animation.setInterpolator(new AccelerateDecelerateInterpolator());animation.setInterpolator(new AnticipateInterpolator());animation.setInterpolator(new AnticipateOvershootInterpolator());animation.setInterpolator(new BounceInterpolator());animation.setInterpolator(new CycleInterpolator(2));animation.setInterpolator(new LinearInterpolator());animation.setInterpolator(new OvershootInterpolator());
The animation does not set the Interpolator property as the default value, constant speed
Interpolator properties can also be customized
Monitoring of animation state
- Setanimationlistener (New Animationlistener ()) method, override the following three methods:
- Onanimationstart (): Animation start
- Onanimationrepeat (): Animation repetition
- Onanimationend (): End of animation
Property animations
Animator ———— ———— AnimatorSet ———— ValueAnimator ———— ObjectAnimator ———— TimeAnimator
Animatorset:animator sub-class, used to compose animations
Objectanimator:valueanimator sub-class,
Valueanimator
Res/animator/***.xml defined in Valueanimator
Grammar
<animator android:duration="int" android:valueFrom="float | int | color" android:valueTo="float | int | color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["repeat" | "reverse"] android:valueType=["intType" | "floatType"]/>
Use
//使用xml属性动画 ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.valueanimator); //动画监听 valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { Integer animatedValue = (Integer) animation.getAnimatedValue(); view.scrollTo(animatedValue,0); } }); valueAnimator.start();
Introduction to Java Properties
Animations from 0 to 10 to 5 and then to 0 valueanimator valueanimator = valueanimator.offloat (0, 10, 5, 0); Valueanimator valueanimator = valueanimator.ofint (0, 10, 5, 0); /* Ofobject differs from offloat and Ofint, with typeevaluator parameters; Ofint and offloat use the system's default Intevaluator and Floatevaluator, The Ofobject parameter typeevaluator needs our custom *///valueanimator.ofobject (Typeevaluator evaluator, Object ... values), post-introduction//Animation Call Valueanimator.addupdatelistener (new Valueanimator.animatorupdatelistener () {@Override Public when each frame is updated void Onanimationupdate (Valueanimator animation) {Final float Animatedvalue = (float) animation.getanimatedval UE (); } }); The Listener Valueanimator.addlistener (new Animatorlisteneradapter () {///animation cancellation @Override public void OnA Nimationcancel (Animator animation) {Super.onanimationcancel (animation); }//End of animation @Override public void Onanimationend (Animator animation) {Super.onanimationend (a Nimation); }//animation repeats @Override public void Onanimationrepeat (Animator animation) {Super.onanima Tionrepeat (animation); }//Animation starts @Override public void Onanimationstart (Animator animation) {Super.onanimationsta RT (animation); }//Animation paused @Override public void Onanimationpause (Animator animation) {Super.onanimationpau SE (animation); }//Animation restart @Override public void Onanimationresume (Animator animation) {Super.onanimationre Sume (animation); } }); Animation execution Time Valueanimator.setduration (500); Animation repetition number Valueanimator.setrepeatcount (Integer.max_value); /* Animation loop mode, there are two values: Valueanimator.restart: Restart Valueanimator.reverse: Reverse restart/valueanimator.setrepeatmode (Va Lueanimator.reverse); Animation differential Valueanimator.setinterpolator (new Decelerateinterpolator ()); Set animation delay play Valueanimator.setstartdelay (1000); Start Play Animation valueAnimator.start ();
Typeevaluator
Objectanimator
Properties Introduction
Objectanimator use is different from the only
/** 第二个参数 propertyName 说明: alpha:透明度 rotation:旋转 rotationX:围绕x轴旋转 rotationY:围绕y轴旋转 translationX:在x轴平移 translationY:在y轴平移 scaleX:在x轴缩放 scaleY:在y轴缩放 backgroundColor:可以修改控件背景颜色 等等 */ ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1, 10, 10, 5, 1); alphaAnimator.setDuration(2000); alphaAnimator.setRepeatMode(ValueAnimator.REVERSE); alphaAnimator.setRepeatCount(10); alphaAnimator.setInterpolator(new DecelerateInterpolator()); alphaAnimator.start(); alphaAnimator.setStartDelay(1000);
Android Animation Summary