Как остановить анимацию в android?
после нажатия на кнопку начинается анимация трёх imageView
по кнопке СТОП хочу остановить эти 3 анимации, однако она продолжает крутиться
Нашёл в гугле 2 аналогичных вопроса, опробовал их решение, но безрезультатно:
https://stackoverflow.com/questions/39910650/stop-animation-when-button-pressed\
https://stackoverflow.com/questions/4112599/how-to-stop-an-animation-cancel-does-not-work\
Код запуска анимации MainActivity:
start_btn.setOnClickListener {
if (Common.SCORE >= 0) {
up.visibility = View.GONE
down.visibility = View.VISIBLE
image.setValueRandom(
Random.nextInt(4),
Random.nextInt(15 - 5 + 1) + 5)
image2.setValueRandom(
Random.nextInt(4),
Random.nextInt(15 - 5 + 1) + 5)
image3.setValueRandom(
Random.nextInt(4),
Random.nextInt(15 - 5 + 1) + 5)
}
}
Метод setValueRandom реализован в отдельном классе, реализующий интерфейс IEventEnd с методом eventEnd:
import android.animation.Animator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.ImageView
import kotlinx.android.synthetic.main.activity_main.view.*
import kotlinx.android.synthetic.main.image_view_scrolling.view.*
import ru.examp.waterslotgame.R
class ImageViewScrolling: FrameLayout {
internal lateinit var eventEnd:IEventEnd
internal var last_result=0
internal var oldValue = 0
companion object {
private val ANIMATION_DURATION = 150
}
val value:Int
get() = Integer.parseInt(nextImage.tag.toString())
fun setEventEnd(eventEnd: IEventEnd)
{
this.eventEnd = eventEnd
}
constructor(context: Context):super(context){
init(context)
}
constructor(context: Context, attrs: AttributeSet):super(context, attrs) {
init(context)
}
private fun init(context: Context) {
LayoutInflater.from(context).inflate(R.layout.image_view_scrolling, this)
nextImage.translationY = height.toFloat()
}
fun setValueRandom(image:Int, num_rotate:Int)
{
currentImage.animate()
.translationY((-height).toFloat())
.setDuration(ANIMATION_DURATION.toLong()).start()
nextImage.translationY = nextImage.height.toFloat()
nextImage.animate().translationY(0f).setDuration(ANIMATION_DURATION.toLong())
.setListener(object: Animator.AnimatorListener {
override fun onAnimationStart(p0: Animator?) {
}
override fun onAnimationEnd(p0: Animator?) {
setImage(currentImage, oldValue%6)
currentImage.translationY=0f
if (oldValue != num_rotate)
{
setValueRandom(image, num_rotate)
oldValue++
}
else
{
last_result = 0
oldValue = 0
setImage(nextImage, image)
eventEnd.eventEnd(image%6, num_rotate)
}
}
override fun onAnimationCancel(p0: Animator?) {
}
override fun onAnimationRepeat(p0: Animator?) {
}
}).start()
}
private fun setImage(img: ImageView?, value: Int) {
if (value == Util.fish1)
img!!.setImageResource(R.drawable.fish1)
else if (value == Util.fish2)
img!!.setImageResource(R.drawable.fish2)
else if (value == Util.fish3)
img!!.setImageResource(R.drawable.fish3)
else if (value == Util.fish4)
img!!.setImageResource(R.drawable.fish4)
img!!.tag = value
last_result = value
}
}
Также есть отдельный файл Util:
object Util {
var fish1 = 0
var fish2 = 1
var fish3 = 2
var fish4 = 3
}
Он используется, чтобы при сравнении смотреть какая из рыб попалась
Вот моя реализация обработки кнопки стоп, однако анимация не прекратилась:
stop_btn.setOnClickListener {
up.visibility = View.VISIBLE
down.visibility = View.GONE
image.clearAnimation()
image.clearFocus()
image.animate().cancel()
image2.clearAnimation()
image.clearFocus()
image2.animate().cancel()
image3.clearAnimation()
image.clearFocus()
image3.animate().cancel()
}