KOTLIN代碼實現 A和B兩個VIEW,A放大到200%後打開B,B按200%來打開
时间: 2023-06-20 11:03:10 浏览: 41
下面是Kotlin代码实现A和B两个View的放大和打开功能:
```
// 获取A和B的View对象
val viewA = findViewById<View>(R.id.viewA)
val viewB = findViewById<View>(R.id.viewB)
// 定义放大的倍数
val scale = 2f
// 定义动画时长
val duration = 500L
// 定义A放大的动画
val scaleAnimateA = ScaleAnimation(1f, scale, 1f, scale, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f).apply {
this.duration = duration
}
// 定义B放大的动画
val scaleAnimateB = ScaleAnimation(1f, scale, 1f, scale, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f).apply {
this.duration = duration
// 设置放大动画结束后打开B的动画
this.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation?) {}
override fun onAnimationEnd(animation: Animation?) {
viewB.visibility = View.VISIBLE
viewB.startAnimation(ScaleAnimation(1f, scale, 1f, scale, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f).apply {
this.duration = duration
})
}
override fun onAnimationRepeat(animation: Animation?) {}
})
}
// 设置A的点击事件
viewA.setOnClickListener {
// 开始A的放大动画
viewA.startAnimation(scaleAnimateA)
// 隐藏B
viewB.visibility = View.GONE
}
// 设置B的点击事件
viewB.setOnClickListener {
// 开始B的放大动画
viewB.startAnimation(scaleAnimateB)
}
```
在代码中,我们首先获取了A和B的View对象,然后定义了放大倍数和动画时长。接着,分别定义了A和B的放大动画,其中B的放大动画结束后,设置了打开B的动画,并在动画结束后将B的可见性设置为可见。最后,分别为A和B设置点击事件,开始对应的动画即可。