一、概念
补间动画 ViewAnimation(Tween):不改变view的位置和属性。 |
属性动画PeopertyAnimation:view的属性根据执行的动画发生真实的改变。 |
帧动画 DrawableAnimation(Frame): |
二、补间动画 ViewAnimation(Tween)
2.1 动画模式
透明度 | AlphaAnimation(float fromAlpha, float toAlpha) fromAlpha:起始透明度。 toAlpha:结束透明度,透明度的范围为0-1。 |
缩放 | ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) fromXScale/fromYScale:沿着X轴/Y轴缩放的起始比例。 toXScale/toYScale:沿着X轴/Y轴缩放的结束比例。 pivotX/pivotY:缩放的中轴点X/Y坐标,即距离自身左边缘的位置,比如50%就是以图像的中心为中轴点。 |
移动 | TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) fromXDelta/fromYDelta:动画起始位置的X/Y坐标。 toXDelta/toYDelta:动画结束位置的X/Y坐标。 |
旋转 | RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) fromDegrees/toDegrees:旋转的起始/结束角度。 |
动画集合 | AnimationSet(boolean shareInterpolator) 可同时播放以上动画。shareInterpolator:true都用一样的插值器,false用各自的插值器。通过调用addAnimation(Animation a) 将动画添加进集合。 |
2.2 动画配置
持续时间 | public void setDuration(long durationMillis) |
重复次数 | public void setRepeatCount(int repeatCount) 值为-1或者infinite时,表示动画永不停止。 |
重复模式 | public void setRepeatMode(int repeatMode) 默认restart,但只有当repeatCount大于0或者infinite或-1时 才有效。还可以设置成reverse,表示偶数次显示动画时会做方向相反的运动。 |
停留结果 | public void setFillAfter(boolean fillAfter) 设为 true 则动画结束后 view 会停留在当前效果。 |
开始动画 | public void startAnimation(Animation animation) 传入要播放的动画。 |
2.3 插值器 Interpolator
LinearInterpolator | 动画以均匀的速度改变。 |
AccelerateInterpolator | 在动画开始的地方改变速度较慢,然后开始加速。 |
AccelerateDecelerateInterpolator | 在动画开始、结束的地方改变速度较慢,中间时加速。 |
CycleInterpolator | 动画循环播放特定次数,变化速度按正弦曲线改变: Math.sin(2 * mCycles * Math.PI * input)。 |
DecelerateInterpolator | 在动画开始的地方改变速度较快,然后开始减速。 |
AnticipateInterpolator | 反向,先向相反方向改变一段再加速播放, |
AnticipateOvershootInterpolator | 开始的时候向后然后向前甩一定值后返回最后的值。 |
BounceInterpolator | 跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100。 |
OvershottInterpolator | 回弹,最后超出目的值然后缓慢改变到目的值。 |
2.4 调用方式
2.4.1 xml
res目录下新建anim文件夹,创建xml文件,可选节点有alpha、rotate、scale、translate、set。
<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"/>
AnimationUtils.loadAnimation(this, R.anim.translate) //加载Xml文件中的动画
imagineView.startAnimation(translate) //将动画设置到指定的View上
2.4.2 代码
val imagineView = binding.imagineView
val button = binding.button
//透明
val alphaAnimation = AlphaAnimation(1.0F, 0.2F)
//旋转
val rotateAnimation = RotateAnimation(0F, 360F, Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0.5F)
//缩放
val scaleAnimation = ScaleAnimation(1.0F, 5.0F, 1.0F, 5.0F, Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0.5F)
//平移
val translateAnimation = TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0F, Animation.RELATIVE_TO_PARENT, 0F,Animation.RELATIVE_TO_PARENT, 0F, Animation.RELATIVE_TO_PARENT, 0.4F)
//动画集合
val set = AnimationSet(true).apply {
addAnimation(alphaAnimation)
addAnimation(rotateAnimation)
addAnimation(scaleAnimation)
addAnimation(translateAnimation)
duration = 1 //持续时间
repeatCount = 10 //重复次数
repeatMode = Animation.RESTART //重复方式
fillAfter
}
button.setOnClickListener {
imagineView.startAnimation(set) //开始动画
}
三、属性动画PeopertyAnimation
3.1 ViewPropertyAnimator
view.animate().translationX(500)