0

Android の AnimationDrawable クラスを使用してアニメーションを行う簡単なデモ プロジェクトを 1 つ作成しました。ここに私のJavaコードがあります:

public class TestAnimationActivity extends Activity {
    /** Called when the activity is first created. */

    ImageView imgCircleWhite,imgCircleYellow;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imgCircleYellow = (ImageView)findViewById(R.id.imgCircleYellow);

        animateState(true, imgCircleYellow);

    }

    public void animateState(boolean flag,ImageView imageView)
    {
        imgCircleYellow.setBackgroundResource(R.drawable.animate_circle_sensor_1);
        AnimationDrawable yourAnimation = (AnimationDrawable) imageView.getBackground(); 
        if(!flag)
        {   
            //imageView.getAnimation().reset();
            imageView.setBackgroundResource(R.drawable.circle_label_white_1);           
            yourAnimation.stop();           
        }
        else
        {
            imageView.setBackgroundResource(R.drawable.animate_circle_sensor_1); 
            yourAnimation = (AnimationDrawable) imageView.getBackground(); 
            yourAnimation.start();
        }
    }
}

これが私のmain.xmlファイルです:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <ImageView android:id="@+id/imgCircleYellow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle_label_yellow_1"/>

</LinearLayout>

以下は私の animate_sensor_circle_1.xml で、使用するために drawable フォルダーに入れました:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >

    <item
        android:drawable="@drawable/circle_label_white_2"
        android:duration="50"/>
    <item
        android:drawable="@drawable/circle_label_yellow_1"
        android:duration="50"/>
</animation-list>

アプリを実行すると、上記の xml ファイルの最初の項目のみが表示されますが、アニメーションは正常に繰り返されません。誰かが私が間違っている場所を教えてもらえますか?

4

1 に答える 1

0

を呼び出すのに適切な時期ではありませんAnimationDrawable.start()。これは、ビューの初期化が完了した後に行う必要があります。これを試して:

final ImageView imgCircleYellow = (ImageView)findViewById(R.id.imgCircleYellow);
imgCircleYellow.setBackgroundResource(R.drawable.animate_circle_sensor_1);
imgCircleYellow.post(new Runnable() {
    @Override
    public void run() {
        animateState(true, imgCircleYellow);
    }

詳細については、これを確認してください: https://stackoverflow.com/a/5490922/813135

于 2012-10-31T08:06:50.720 に答える