Skip to content

Instantly share code, notes, and snippets.

@maliksaif
Created March 26, 2023 16:32
Show Gist options
  • Select an option

  • Save maliksaif/a2d3f105f38684a79bacac6c4bf0d08a to your computer and use it in GitHub Desktop.

Select an option

Save maliksaif/a2d3f105f38684a79bacac6c4bf0d08a to your computer and use it in GitHub Desktop.
Flipcard animation
private lateinit var binding: ActivityMainBinding
lateinit var front_animation: AnimatorSet
lateinit var back_animation: AnimatorSet
var isFront = false
override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
// Now Create Animator Object
// For this we add animator folder inside res
// Now we will add the animator to our card
// we now need to modify the camera scale
val scale = applicationContext.resources.displayMetrics.density
binding.cardFront.cameraDistance = 8000 * scale
binding.cardBack.cameraDistance = 8000 * scale
// Now we will set the front animation
front_animation =
AnimatorInflater.loadAnimator(applicationContext, R.animator.flip_in) as AnimatorSet
back_animation =
AnimatorInflater.loadAnimator(applicationContext, R.animator.flip_out) as AnimatorSet
binding.flipButton.setOnClickListener {
flipCard()
}
back_animation.doOnEnd { binding.flipButton.isClickable = true }
}
private fun flipCard() {
binding.flipButton.isClickable = false
if (isFront) {
back_animation.setTarget(binding.cardBack)
front_animation.setTarget(binding.cardFront)
back_animation.start()
front_animation.start()
} else {
back_animation.setTarget(binding.cardFront)
front_animation.setTarget(binding.cardBack)
front_animation.start()
back_animation.start()
}
isFront = !isFront
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment