1. Requirement description
The project needs to realize the flipping effect of Tarot cards. After clicking on the picture, the picture will be turned over, and during the turning process, it will be replaced with image resources, similar to the turning over of playing cards.
2. Implementation ideas
The picture achieves a flip effect, and the picture is replaced when the animation is halfway through (just when it reaches the back of the picture)
(Tarot gallery: viewpager, image flip: ObjectAnimator)
3. Implement the code
Core code:
val flipAnimator = ObjectAnimator.ofFloat(imageView, "rotationY", 0f, 180f)
flipAnimator.duration = 1000
flipAnimator.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
super.onAnimationEnd(animation)
}
})
flipAnimator.start()
MainScope().launch{
delay(500)
imageView.setImageResource(imageList[position].imageResId)
imageView.rotationY = 0f
}
4. Code logic
First, create a new flip animation with an animation time of 1000 milliseconds, bind the imageView, and add a listener to the animation, because some requirements require some operations after the animation ends. The code below is delayed by 500 milliseconds because the total animation is 1000 seconds and the flip is 180 degrees. At 500 milliseconds, you just start to see the back side, so it gives the user a smooth card flip effect.