AnimationDrawableが最後のフレームしか表示されない場合

AndroidのAnimationDrawableを再生するには

        ImageView imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setBackgroundResource(R.drawable.animation1);
        AnimationDrawable animation = (AnimationDrawable) imageView.getBackground();
        animation.setOneShot(true); // 一度だけ再生
        animation.start();

こんなコードで実行しますが、もう一度再生しようとstart()を実行すると、最後の1フレームしか再生されません。

        ImageView imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setBackgroundResource(R.drawable.animation1);
        AnimationDrawable animation = (AnimationDrawable) imageView.getBackground();
        animation.stop(); // 念のため一度ストップ
        animation.selectDrawable(0); // 0フレーム目に戻す
        animation.setOneShot(true);
        animation.start();

とすれば最初から再生できます。

DrawableContainer | Android Developers

selectDrawableってドキュメントに何の説明も書いてないんですね。


タグ: ,

新しく会社作りました!

コメントをどうぞ