Animator 属性动画

针对某个控件的某个属性进行操作(通过改变属性的值来实现动画效果)
会改变属性值 控件真正移动了

适用情况:需要实现复杂的动画效果,或需要对视图的属性进行动态改变和交互。

ValueAnimator

ValueAnimator 是一个用于执行属性动画的类,可以用来改变数值属性的值并在一定时间内进行动画过渡。

ObjectAnimator 继承自 ValueAnimator
val animator = ObjectAnimator.ofFloat(view, "propertyName", startValue, endValue)
view :要执行动画的 View 对象。
“propertyName” :要执行动画的属性名称,如 “alpha”、”rotation”、”translationX”、”scaleX” 等。
startValue :动画起始值。
endValue :动画结束值。

ValueAnimator
这是一个动画,不断产生一系列的值

1
2
3
ValueAnimator.ofArgb()  //颜色的变化
ValueAnimator.ofFloat() //得到某区间的数(浮点数)
ValueAnimator.ofInt() //得到某区间的数(整数)

创建动画

步骤:

第一步 创建动画对象

ValueAnimator

  1. duration 持续时间

  2. repeatCount 重复次数

    • 0 不重复
    • ValueAnimator.INFINITE 无限次
  3. repeatMode 重复模式

    • ValueAnimator.RESTART 重新开始
    • ValueAnimator.REVERSE 反转
  4. interpolator 插值器(控制动画的速度变化)

    • LinearInterpolator() 匀速进行
    • AccelerateInterpolator() 在动画开始时加速
    • DecelerateInterpolator() 在动画结束时减速
    • BounceInterpolator() 回弹效果
  5. startDelay 延迟时间 指定在调用 start 方法后多久开始执行动画

  6. start()启动动画

第二步 监听 value 变化

addUpdateListener{} //会返回更新值之后的 ValueAnimator

  • animatedValue 是 ValueAnimator 的一个属性,表示当前动画的数值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//1. 创建动画对象
//创建 ValueAnimator 的对象,并设置动画的起始值和结束值
val angleAnimator = ValueAnimator.ofFloat(0f,360f)

angleAnimator.duration = 2000
angleAnimator.repeatCount = 2
angleAnimator.repeatMode = ValueAnimator.REVERSE
angleAnimator.interpolator = BounceInterpolator()
angleAnimator.startDelay = 500

angleAnimator.start() //启动动画


//2. 监听value变化
//监听value变化 这个值是不断在更新变化的
angleAnimator.addUpdateListener {
//获取当前的值 转化成Float类型
mSweepAngle = it.animatedValue as Float

//drawText数值随动画变化
val num = ((it.animatedValue / 360) * 100).toInt()
rateText = "$num%"

invalidate() //告诉View刷新更新的值
}

用apply修改

1
2
3
4
5
6
7
8
9
ValueAnimator.ofFloat(0f,360f).apply{
duration = 500
addUpdateListener{
mSweepAngle = it.animatedValue as Float //得到当前过程值
invalidate() //告诉View刷新更新的值
}
start()
}

AnimatorSet

AnimatorSet 是一个用于 管理多个动画一起播放或按顺序播放 的类。
可以使用 AnimatorSet 来组合多个属性动画,使它们按照指定的顺序或同时播放。

AnimatorSet() 管理所有动画的播放形式

  • playTogether(Animator…) 一起

  • playSequentially(Animator…) 顺序

  • setDuration(Long): 设置AnimatorSet的持续时间

  • start(): 启动AnimatorSet中的动画

  • pause():暂停

  • cancel(): 取消AnimatorSet中的动画

  • addListener(Animator.AnimatorListener): 添加动画监听器

  • duration

  • interpolator

1
2
3
4
5
6
7
8
9
10
11
12
13
AnimatorSet.playSequentially()


AnimatorSet().apply {
playSequentially(radiusAnim,moveAnim) //传入动画

//监听动画事件 改变画笔颜色
addListener( onEnd = {
mRoundRectPaint.color = Color.parseColor("#00BAAD")
invalidate()//颜色变了 再重新画一下
})
start()
}

这个也是属性动画:

1
2
3
4
5
6
7
binding.colorRecyclerView  
.animate()
.translationY(requireActivity().dp2px( dp: -78).toFloat()

binding.colorRecyclerView
.animate()
.rotation( value: 360f)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* 管理封面图片的旋转动画
* 开始
* 暂停
* 恢复
* 取消
*/
class AlbumRotateAnimation(val target: View) {
private var mAnimation:ObjectAnimator? = null

fun start(){
if (mAnimation!= null && mAnimation!!.isPaused){
resume()
return
}

if (mAnimation!= null && mAnimation!!.isStarted){
release()
}
mAnimation = ObjectAnimator.ofFloat(target,"rotation",0f,360f).apply {
duration = 5000
repeatMode = ValueAnimator.RESTART
repeatCount = ValueAnimator.INFINITE //无限
interpolator = LinearInterpolator()
start()
}
}
fun reset(){
release()
}

fun pause(){
mAnimation?.pause()
}
fun resume(){
mAnimation?.resume()
}
fun release(){
mAnimation?.cancel()
mAnimation = null
}
}

Animator

Animator 抽象类
常用方法:
void cancel (): 取消动画。
void end ():让动画到达最后一帧。
void start():开始动画。
void pause():暂停动画。
void resume():继续动画。
void reverse ():反向播放动画。
boolean isRunning():是否在运行中。
boolean isStarted():是否已经开始。
boolean isPaused()

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2023-2025 Annie
  • Visitors: | Views:

嘿嘿 请我吃小蛋糕吧~

支付宝
微信