Created
March 26, 2023 16:32
-
-
Save maliksaif/a2d3f105f38684a79bacac6c4bf0d08a to your computer and use it in GitHub Desktop.
Flipcard animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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