一、动画集合
类:
- AnimationSet
功能较弱不能做到把集合中的动画按一定顺序进行组织然后在执行的定制。 - AnimatorSet
两者都没有设置重复的函数。。。
1、AnimationSet
使用:
class MainActivity : AppCompatActivity() {
var TAG = "Animation"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.btn)
val animationSet = AnimationSet(true) //true—让这些动画共用AnimationSet设置插值器
val alphaAnimation = AlphaAnimation(0f, 1f)
alphaAnimation.duration = 3000
val rotateAnimation = RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f)
rotateAnimation.duration = 3000
alphaAnimation.repeatCount = Animation.INFINITE
rotateAnimation.repeatCount = Animation.INFINITE
animationSet.addAnimation(alphaAnimation)
animationSet.addAnimation(rotateAnimation)
animationSet.interpolator = AccelerateDecelerateInterpolator()
button.startAnimation(animationSet)
}
}
注意一下,不能在AnimationSet设置循环,因为设了也不起作用。
2、AnimatorSet
有两种方法可以将动画添加到AnimatorSet:
- 可以调用AnimatorSet的playTogether(Animator[])或AnimatorSet的playSequentially(Animator[])方法来一次添加一组动画
- 也可以将AnimatorSet的play(Animator)方法与AnimatorSet.Builder类中的方法结合使用以逐个添加动画。
依然没办法在AnimatorSet设置循环
来吧:
playTogether(Animator[])
仅仅让动画一起开始,但过程和结果由各个动画自己定义
class MainActivity : AppCompatActivity() {
var TAG = "Animation"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun click(view: View) {
val animatorSet = AnimatorSet()
val button = findViewById<Button>(R.id.btn)
val backgroundColorAnimator = ObjectAnimator.ofInt(button, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
val rotation= ObjectAnimator.ofFloat(button, "Rotation", 0f, 360f)
animatorSet.playTogether(backgroundColorAnimator, rotation)
animatorSet.duration = 3000
animatorSet.start()
}
fun click2(view: View) {
val animatorSet = AnimatorSet()
val button2 = findViewById<Button>(R.id.btn2)
val backgroundColorAnimator = ObjectAnimator.ofArgb(button2, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
val rotation= ObjectAnimator.ofFloat(button2, "Rotation", 0f, 360f)
animatorSet.playTogether(backgroundColorAnimator, rotation)
backgroundColorAnimator.duration = 3000
rotation.duration = 6000
animatorSet.start()
}
}
我们第在二个button每个动画设置了不同执行时间
这里顺带来了解以下ofInt设置颜色和ofArgb的区别
- ofInt是通过从起始颜色一点点累加到目标颜色。
- ofArgb可以理解为设置平时我们用ps设置的渐变效果。
playSequentially(Animator[])
顺序执行
class MainActivity : AppCompatActivity() {
var TAG = "Animation"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun click(view: View) {
val animatorSet = AnimatorSet()
val button = findViewById<Button>(R.id.btn)
val backgroundColorAnimator = ObjectAnimator.ofInt(button, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
val rotation= ObjectAnimator.ofFloat(button, "Rotation", 0f, 360f)
animatorSet.playSequentially(backgroundColorAnimator, rotation)
animatorSet.duration = 3000
animatorSet.start()
}
fun click2(view: View) {
val animatorSet = AnimatorSet()
val button2 = findViewById<Button>(R.id.btn2)
val backgroundColorAnimator = ObjectAnimator.ofArgb(button2, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
val rotation= ObjectAnimator.ofFloat(button2, "Rotation", 0f, 360f)
animatorSet.playSequentially(backgroundColorAnimator, rotation)
backgroundColorAnimator.duration = 3000
rotation.duration = 6000
animatorSet.start()
}
}
通过两个按钮效果我们发现
在AnimatorSet设置duration时,每个动画都会进行duration个时间长度,总时长:n*duration
playSequentially循环问题,以上面这个为例子如果你在第一个动画设置无限循环,那第二个动画就永远执行不到了,在Animation设置duration也无效。除非你手动cancel掉
AnimatorSet.Builder
建造者
三个常用函数:
- 通常用法:
animatorSet.play().with().before().after()
- with 动画同行
- before 先行
- after 后行
- play 返回一个AnimatorSet.Bulider对象
那就究竟时哪个动画先后呢?记一下:
- A after B A在B执行后再执行
- A before B A在B执行前执行
验证一下:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun click(view: View) {
val animatorSet = AnimatorSet()
val button = findViewById<Button>(R.id.btn)
val backgroundColorAnimator = ObjectAnimator.ofInt(button, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
val rotation = ObjectAnimator.ofFloat(button, "Rotation", 0f, 360f)
animatorSet.duration = 3000
animatorSet.play(backgroundColorAnimator).before(rotation)
animatorSet.start()
}
fun click2(view: View) {
val animatorSet = AnimatorSet()
val button2 = findViewById<Button>(R.id.btn2)
val backgroundColorAnimator = ObjectAnimator.ofArgb(button2, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
val rotation = ObjectAnimator.ofFloat(button2, "Rotation", 0f, 360f)
backgroundColorAnimator.duration = 3000
rotation.duration = 6000
animatorSet.play(backgroundColorAnimator).after(rotation)
animatorSet.start()
}
}
那这些复杂的动画顺序又怎么实现重复播放呢?看下面代码:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun click2(view: View) {
val animatorSet = AnimatorSet()
val button2 = findViewById<Button>(R.id.btn2)
val backgroundColorAnimator = ObjectAnimator.ofArgb(button2, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
val rotation = ObjectAnimator.ofFloat(button2, "Rotation", 0f, 360f)
backgroundColorAnimator.duration = 3000
rotation.duration = 6000
animatorSet.play(backgroundColorAnimator).after(rotation)
animatorSet.addListener(object : Animator.AnimatorListener{
override fun onAnimationRepeat(animation: Animator?) {
}
override fun onAnimationEnd(animation: Animator?) {
animation?.start()
}
override fun onAnimationCancel(animation: Animator?) {
}
override fun onAnimationStart(animation: Animator?) {
}
})
animatorSet.start()
}
}
动画执行结束重开呗
注意哦,这里onAnimationEnd虽然传进来了个animation,但并不表示每一个动画,而是人家用每个动画合成好的大动画,所以并不会乱哦。
下节进入插值器估值器,还有ofObject()用法
希望能和大家相互学习哦。