目录
普通动画和属性动画的区别:
这里说的普通动画名字为补间动画
1.用法更简便 2.改变了事件,也就是真正改变了属性,如view的位置
参数的说明
repeatCount说明
重复次数,ValueAnimator.INFINITE为无限次
repeatMode说明
重复类型有两个值,reverse表示倒序回放,restart表示从头播放
duration
持续时间,如果设置到组合动画中,则按照组合动画来平均分配
Interpolators相关类解释:
AccelerateDecelerateInterolator:先加速后减速。
AccelerateInterpolator:加速。
DecelerateInterpolator:减速。
AnticipateInterpolator:先向相反方向改变一段再加速播放。
AnticipateOvershootInterpolator:先向相反方向改变,再加速播放,会超出目标值然后缓慢移动至目标值,类似于弹簧回弹。
BounceInterpolator:快到目标值时值会跳跃。
CycleIinterpolator:动画循环一定次数,值的改变为一正弦函数:Math.sin(2 * mCycles * Math.PI * input)。
LinearInterpolator:线性均匀改变。
OvershottInterpolator:最后超出目标值然后缓慢改变到目标值。
TimeInterpolator:一个允许自定义Interpolator的接口,以上都实现了该接口。
startDelay
第一次动画延后多少ms开始播放,之后不调用
举例说明
透明度变化
1.普通动画
其中repeatCount为循环多少次,infinite为无限循环
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.1"
android:repeatCount="infinite"
android:duration="500"
android:toAlpha="1" />
</set>
在代码中调用
Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
animation.setDuration(1000);
image.startAnimation(animation);
2.属性动画
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(image, "alpha", 0f, 1.0f,0f);
objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
objectAnimator.setDuration(1000);
objectAnimator.start();
平移
1.普通动画
平移回来又回去,注意回去就已经变为负数,因为新的起点变为0
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:duration="1000"
android:toXDelta="500" />
<translate
android:fromXDelta="0"
android:duration="1000"
android:startOffset="1000"
android:toXDelta="-500"/>
</set>
Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.translation);
image.startAnimation(animation1);
2.属性动画
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(image, "translationX", 0, 300.0f, 0f);
objectAnimator1.setRepeatCount(2);
objectAnimator1.setDuration(1000);
objectAnimator1.start();
旋转
普通动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"/>
</set>
Animation animation3 = AnimationUtils.loadAnimation(this, R.anim.rotate);
animation3.setDuration(1000);
image.startAnimation(animation3);
属性动画,如果想设置沿X轴旋转,可以设置rotationX
ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(image, "rotation", 0f, 360f);
objectAnimator3.setDuration(1000);
objectAnimator3.start();
大小
1.普通动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="1"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"/>
</set>
Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.scale);
animation2.setDuration(1000);
image.startAnimation(animation2);
2.属性动画
scale不能用,要用组合动画将scaleX和scaleY结合起来使用
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(image, "scaleX", 0f, 1.0f);
ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(image, "scaleY", 0f, 1.0f);
//组合动画
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(1000);
animatorSet.play(objectAnimator2).with(objectAnimator4);
animatorSet.start();
组合动画
实现一个效果,就是先透明度变化并旋转一圈,然后平移一段距离又回来
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1" />
<rotate
android:duration="1000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
<translate
android:duration="1000"
android:fromXDelta="0"
android:startOffset="1000"
android:toXDelta="500" />
<translate
android:duration="1000"
android:fromXDelta="0"
android:startOffset="2000"
android:toXDelta="-500" />
</set>
Animation animation4 = AnimationUtils.loadAnimation(this, R.anim.combination);
image.startAnimation(animation4);
属性动画
ObjectAnimator anim_alpha = ObjectAnimator.ofFloat(image, "alpha", 0f, 1.0f);
ObjectAnimator anim_rotation = ObjectAnimator.ofFloat(image, "rotation", 0f, 360f);
ObjectAnimator anim_translation = ObjectAnimator.ofFloat(image, "translationX", 0f, 500f);
ObjectAnimator anim_translation1 = ObjectAnimator.ofFloat(image, "translationX", 500f, 0f);
//用了两个set
AnimatorSet animatorSet1 = new AnimatorSet();
animatorSet1.play(anim_alpha).with(anim_rotation);
animatorSet1.setDuration(1000);
animatorSet1.start();
AnimatorSet animatorSet2 = new AnimatorSet();
animatorSet2.play(anim_translation).before(anim_translation1);
animatorSet2.setDuration(1000).setStartDelay(1000);
animatorSet2.start();
设置每次动画的停顿
在两次动画之间因为没有停顿的方法,所以只能让其透明度不变来模拟停顿
INFINITE:无限次循环
RESTART:每次动画都是重新开始(注意从最后一个到第一个是不需要时间的,所以在第一个第二个之间设置间隔)
如果设置为REVERSE是正播放一次,反播放一下
LinearInterpoator():速度线性变化
//头像的透明度变化,每次都会停顿一下,每次停顿一秒,透明度转变是2秒
val objectAnimatorWoman: ObjectAnimator =
ofFloat(civ_portrait_woman, "alpha", 1f, 1f, 0.5f, 0f, 0f, 0.5f, 1f)
objectAnimatorWoman.repeatCount = ValueAnimator.INFINITE
objectAnimatorWoman.repeatMode = RESTART
objectAnimatorWoman.interpolator = LinearInterpolator()
val objectAnimatorMan: ObjectAnimator =
ofFloat(civ_portrait_man, "alpha", 0f, 0f, 0.5f, 1f, 1f, 0.5f, 0f)
objectAnimatorMan.repeatCount = ValueAnimator.INFINITE
objectAnimatorMan.repeatMode = RESTART
objectAnimatorMan.interpolator = LinearInterpolator()
animatorSet = AnimatorSet()
animatorSet.play(objectAnimatorMan).with(objectAnimatorWoman)
animatorSet.duration = 6000
animatorSet.start()
运行结果