実現したいこと
画面タップ(そのたびに発生するアニメーション)を十回行った瞬間、別の画像へ変更を行いたい。
以下のコードで十回タップすると画像変更は行われるが、なぜかアニメーションが発生しません。
kotlin
1class MainActivity : AppCompatActivity() { 2 override fun onCreate(savedInstanceState: Bundle?) { 3 super.onCreate(savedInstanceState) 4 setContentView(R.layout.tama) 5 6 7 fun yure(yurayura: View) { ←アニメーションのコードはこっから 8 9 val animatorList: MutableList<Animator> = ArrayList() 10 11 animatorList.add(ObjectAnimator.ofFloat(yurayura, "translationX", 0f, -100f).setDuration(10)) 12 animatorList.add(ObjectAnimator.ofFloat(yurayura, "translationX", -100f, 70f).setDuration(30)) 13 animatorList.add(ObjectAnimator.ofFloat(yurayura, "translationX", 70f, -50f).setDuration(60)) 14 animatorList.add(ObjectAnimator.ofFloat(yurayura, "translationX", -50f, 40f).setDuration(100)) 15 animatorList.add(ObjectAnimator.ofFloat(yurayura, "translationX", 40f, -30f).setDuration(150)) 16 animatorList.add(ObjectAnimator.ofFloat(yurayura, "translationX", -30f, 0f).setDuration(210)) 17 18 val set = AnimatorSet() 19 set.playSequentially(animatorList) 20 set.start() 21 } 22 yurayura.setOnClickListener { 23 val v: View = yurayura 24 yure(v) 25 26 }アニメーションのコードはここまで(発生しない……) 27 28 var taxtuti = 0←ここから十回タップで画像変更コード 29 yurayura.setOnClickListener{ 30 taxtuti++ 31 when(taxtuti){ 32 10 -> yurayura.setImageResource(R.drawable.pann) 33 } 34 } 35 } 36} ここまで
tama.xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:background="#F6F5F5" 8 tools:context=".MainActivity" > 9 10 <ImageView 11 android:id="@+id/yurayura" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:background="@color/white" 15 android:backgroundTint="@color/white" 16 app:layout_constraintBottom_toBottomOf="parent" 17 app:srcCompat="@drawable/siroitamago" 18 tools:layout_editor_absoluteX="0dp" /> 19</androidx.constraintlayout.widget.ConstraintLayout>
activity_subtamago.xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".subtamago"> 8 9 <ImageView 10 android:id="@+id/medama" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 app:srcCompat="@drawable/pann" 14 tools:layout_editor_absoluteX="0dp" 15 tools:layout_editor_absoluteY="0dp" /> 16</androidx.constraintlayout.widget.ConstraintLayout>
試したこと
どうにか自力でここまでコードを書き、十回タップで画像変更は起きるものの、タップするごとに起きるはずのアニメーションが発生しません。
あと一息なのですが、どこを直せば見当もつかないんです。
どうか、よろしくお願い致します。
アニメーションと画面遷移を分けて setOnClickListener してるからでは。
あなたの回答
tips
プレビュー