質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Android Emulator

Android EmulatorはアンドロイドのOSで起動しているアンドロイドのデバイスの機能をシミュレートするソフトウェアです。Emulatorは開発者に複数の違う設定を持ったデバイスを必要とすることなくアプリケーションを開発しテストすることが可能になります。

Q&A

0回答

1121閲覧

Android Studioでのアニメーションの停止、再実行の方法

Ssanswer

総合スコア11

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Android Emulator

Android EmulatorはアンドロイドのOSで起動しているアンドロイドのデバイスの機能をシミュレートするソフトウェアです。Emulatorは開発者に複数の違う設定を持ったデバイスを必要とすることなくアプリケーションを開発しテストすることが可能になります。

0グッド

0クリップ

投稿2020/10/27 02:05

編集2020/10/29 07:39

前提・実現したいこと

スロットゲームのリール回転(開始、一時停止、再実行)をアニメーションで実現したい
また、上記のことを実現するための使用するべきクラスを知りたい
(主にandroidDrawableを使用しての一時停止と再実行の方法が知りたいです。)

発生している問題・エラーメッセージ

スロットゲームの2回目以降のスロットのリール回転のアニメーションが機能していない
Logcatに以下のエラー文が出ている

2020-10-29 16:16:05.294 13579-13579/sample.example.droidslot4 E/AndroidRuntime: FATAL EXCEPTION: main
Process: sample.example.droidslot4, PID: 13579
java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
at sample.example.droidslot4.DroidSlotActivity$5.onClick(DroidSlotActivity.java:222)
at android.view.View.performClick(View.java:5637)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

該当のソースコード

ソースコードは一部抜粋しています

java

1@Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_droid_slot); 5 6 final ImageView droidImage1 = (ImageView) this.findViewById(R.id.droidimageid1); 7 final ImageView droidImage2 = (ImageView) this.findViewById(R.id.droidimageid2); 8 final ImageView droidImage3 = (ImageView) this.findViewById(R.id.droidimageid3); 9 final ImageView droidImage4 = (ImageView) this.findViewById(R.id.droidimageid4); 10 final ImageView droidImage5 = (ImageView) this.findViewById(R.id.droidimageid5); 11 final ImageView droidImage6 = (ImageView) this.findViewById(R.id.droidimageid6); 12 final ImageView droidImage7 = (ImageView) this.findViewById(R.id.droidimageid7); 13 final ImageView droidImage8 = (ImageView) this.findViewById(R.id.droidimageid8); 14 final ImageView droidImage9 = (ImageView) this.findViewById(R.id.droidimageid9); 15 16 final AnimationDrawable animationDrawable = (AnimationDrawable) droidImage1.getDrawable(); 17 final AnimationDrawable animationDrawable2 = (AnimationDrawable) droidImage2.getDrawable(); 18 final AnimationDrawable animationDrawable3 = (AnimationDrawable) droidImage3.getDrawable(); 19 final AnimationDrawable animationDrawable4 = (AnimationDrawable) droidImage4.getDrawable(); 20 final AnimationDrawable animationDrawable5 = (AnimationDrawable) droidImage5.getDrawable(); 21 final AnimationDrawable animationDrawable6 = (AnimationDrawable) droidImage6.getDrawable(); 22 final AnimationDrawable animationDrawable7 = (AnimationDrawable) droidImage7.getDrawable(); 23 final AnimationDrawable animationDrawable8 = (AnimationDrawable) droidImage8.getDrawable(); 24 final AnimationDrawable animationDrawable9 = (AnimationDrawable) droidImage9.getDrawable(); 25 26s.setOnClickListener(new View.OnClickListener() { 27 @Override 28 public void onClick(View v) { 29 30 animationDrawable.start(); 31 animationDrawable2.start(); 32 animationDrawable3.start(); 33 animationDrawable4.start(); 34 animationDrawable5.start(); 35 animationDrawable6.start(); 36 animationDrawable7.start(); 37 animationDrawable8.start(); 38 animationDrawable9.start(); 39 } 40 }); 41 42b1.setOnClickListener(new View.OnClickListener() { 43 @Override 44 public void onClick(View v) { 45 46 animationDrawable.stop(); 47 animationDrawable2.stop(); 48 animationDrawable3.stop(); 49 50//以下同様にb2,b3が続きます

試したこと

animationDrawableクラスを使用したのですが1度のアニメーションのみ反映され
1度停止するとその後のアニメーションは動きませんでした。

animatorクラスも試してみましたがこちらはアプリが強制終了となり原因は不明のままです。
animatorクラスに関しては以下を参考しました。
https://akira-watson.com/android/animatorlisteneradapter.html

こちらのサイトを新たに参考にさせていただいたのですがBitMapをanimationdrawableにキャスト変換できないというエラーがおそらく出ていますが理由が分かりません。
http://yonayona.biz/yonayona/blog/archives/1059446970.html

補足情報(FW/ツールのバージョンなど)

Android Studio 4.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Wind

2020/10/27 03:30

ソースコードが抜粋なので9個の数字があるのか、1~9を回したいのかわかりませんが、 後者でしたら一つのanimationDrawableにすべきでは無いでしょうか? https://techbooster.org/android/ui/2192/
Ssanswer

2020/10/27 04:04

リールを3つ、ボタンを3つ用意していてb1(ボタン1)が押されたときにリール1にある画像が止まる仕組みでb2、b3も同様の処理をしたいのですがその場合も1つのanimationDrawableにすべきなのでしょうか? 説明分かりにくかったみたいですいません。
Ssanswer

2020/10/27 04:04

一応参考にさせていただきます。
Wind

2020/10/29 02:52

Start/Stop以外の方法として、例えばアニメーション速度を0にして止めて、再開する時にアニメーション速度に数値を入れる方法が想定出来ます。 またアニメーションパターンを利用する方法も想定出来ます。 方法は沢山ありますので、色々と試して目的に合った物選んでください。
Ssanswer

2020/10/29 06:26

コメントありがとうございます。 アニメーションの速度で判定する方法は盲点でした。 まだ解決できたわけではありませんがアプローチの方法をご教授いただきありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問