2

次のコードでサンプル プログラムをアニメーション化しようとしています。

AnimationDrawable animation;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loading);

    ImageView animatedImage = (ImageView) findViewById(R.id.animation);
    animatedImage.setBackgroundResource(R.drawable.animate_bag);
    animation = (AnimationDrawable) animatedImage.getBackground();
}


public void startAnimate(View v) 
{
    if (animation != null)
        animation.start();          
} //eof OnClick

XML ファイルは次のとおりです。

    <Button android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Animate"
        android:onClick="startAnimate" />
     <ImageView
        android:id="@+id/animation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/animate_bag" />


</LinearLayout>

私が抱えている問題は、animateImage.getBackground() が null を返すことです。

ヒントをいただければ幸いです:-)

ありがとう、サイモン

4

2 に答える 2

5

getBackground() が null を返す場合は、getDrawable() メソッドを使用します。

//animation = (AnimationDrawable) animatedImage.getBackground();
animation = (AnimationDrawable) animatedImage.getDrawable();
于 2014-12-04T07:38:59.707 に答える
0

これがあなたを助けることを願っています;)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loading);

    final ImageView animatedImage = (ImageView) findViewById(R.id.animation);
    animatedImage.setBackgroundResource(R.drawable.animate_bag);


    animatedImage.post(new Runnable() {

        @Override
        public void run() {
            AnimationDrawable frameAnimation = (AnimationDrawable) animatedImage.getDrawable();
            if (frameAnimation != null) {
                frameAnimation.start();
            }
        }
    });
}
于 2013-05-31T08:12:59.453 に答える