Android Kotlin ImageView 属性动画平滑旋转:暂停和开始功能
要实现在 Android Kotlin 中给 ImageView 添加平滑旋转的属性动画,并且包含暂停和开始功能,可以按照以下步骤进行操作:
- 首先,在 XML 布局文件中添加 ImageView 组件,并设置其初始属性,例如旋转角度为 0 度:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_image"
android:rotation="0"/>
- 在 Kotlin 代码中,获取 ImageView 组件的实例,并创建一个属性动画对象,用于控制旋转动画:
val imageView: ImageView = findViewById(R.id.imageView)
val rotationAnimation = ObjectAnimator.ofFloat(imageView, 'rotation', 0f, 360f)
rotationAnimation.duration = 2000 // 设置旋转动画的持续时间,单位为毫秒
rotationAnimation.repeatCount = ValueAnimator.INFINITE // 设置旋转动画的重复次数,这里设置为无限次
- 添加点击事件,当点击 ImageView 时,判断动画是否正在运行,如果是,则暂停动画,如果否,则开始动画:
imageView.setOnClickListener {
if (rotationAnimation.isRunning) {
rotationAnimation.pause() // 暂停动画
} else {
rotationAnimation.resume() // 继续动画
}
}
- 最后,启动动画,使 ImageView 开始平滑旋转:
rotationAnimation.start()
通过上述步骤,就可以实现在 Android Kotlin 中给 ImageView 添加平滑旋转的属性动画,并且包含暂停和开始功能。
原文地址: https://www.cveoy.top/t/topic/quBi 著作权归作者所有。请勿转载和采集!