import
android.os.Bundle
import
android.view.animation.Animation
import
android.view.animation.AnimationUtils
import
android.widget.Button
import
android.widget.ImageView
import
androidx.appcompat.app.AppCompatActivity
class
MainActivity : AppCompatActivity(), Animation.AnimationListener {
private
lateinit var animation1: Animation
private
lateinit var animation2: Animation
private
var isFrontOfCardShowing =
true
private
lateinit var image: ImageView
private
lateinit var clickBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super
.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
animation1 = AnimationUtils.loadAnimation(
this
, R.anim.to_middle)
animation1.setAnimationListener(
this
)
animation2 = AnimationUtils.loadAnimation(
this
, R.anim.from_middle)
animation2.setAnimationListener(
this
)
clickBtn = findViewById(R.id.button1)
image = findViewById(R.id.imageView1)
clickBtn.setOnClickListener {
it.isEnabled =
false
image.clearAnimation()
image.animation = animation1
image.startAnimation(animation1)
}
}
override fun onAnimationEnd(animation: Animation) {
if
(animation === animation1) {
if
(isFrontOfCardShowing) {
image.setImageResource(R.drawable.card_back)
}
else
{
image.setImageResource(R.drawable.card_front)
}
image.clearAnimation()
image.animation = animation2
image.startAnimation(animation2)
}
else
{
isFrontOfCardShowing = !isFrontOfCardShowing
clickBtn.isEnabled =
true
}
}
override fun onAnimationRepeat(animation: Animation?) {
}
override fun onAnimationStart(animation: Animation?) {
}
}