将ImageView源设置为AnimationDrawable时,应用程序崩溃

时间:2014-08-20 11:05:07

标签: android

我试图将ImageView的源设置为AnimationDrawable,但无论我采用何种方式,应用程序始终会崩溃。继承动画资源:

<?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/f11" android:duration="200" />
<item android:drawable="@drawable/f2" android:duration="200" />
<item android:drawable="@drawable/f3" android:duration="200" />
<item android:drawable="@drawable/f4" android:duration="200" />
<item android:drawable="@drawable/f5" android:duration="200" />
<item android:drawable="@drawable/f6" android:duration="200" />
<item android:drawable="@drawable/f7" android:duration="200" />
<item android:drawable="@drawable/f8" android:duration="200" />
<item android:drawable="@drawable/f9" android:duration="200" />
<item android:drawable="@drawable/f10" android:duration="200" />
<item android:drawable="@drawable/f11" android:duration="200" />
<item android:drawable="@drawable/f12" android:duration="200" />
<item android:drawable="@drawable/f13" android:duration="200" />
<item android:drawable="@drawable/f14" android:duration="200" />
</animation-list>

在我的xml布局中:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
<ImageView
        android:id="@+id/banim"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:src="@anim/anim"
        />
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
          android:gravity="center">
<ImageView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:id="@+id/play"
        android:src="@drawable/play"
        />
<ImageView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:id="@+id/achievements"
        android:src="@drawable/achievements"
        />

</LinearLayout>
</FrameLayout>

继承我的活动:

public class Menu extends Activity {

AnimationDrawable animationDrawable;

public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);

    ImageView gif = (ImageView)findViewById(R.id.banim);
    animationDrawable = (AnimationDrawable)gif.getBackground();
    animationDrawable.start();

    final Intent intent = new Intent(this, MyActivity.class);
    final Intent intent1 = new Intent(this, Achievements.class);

    final ImageView bplay = (ImageView)findViewById(R.id.play);
    final ImageView bachieve = (ImageView)findViewById(R.id.achievements);

    final View.OnClickListener buttonClickListener;
    buttonClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (view == bplay){
                startActivity(intent);
            }else if (view == bachieve){
                startActivity(intent1);
            }

        }
    };

    bplay.setOnClickListener(buttonClickListener);
    bachieve.setOnClickListener(buttonClickListener);
}

}

我已尝试使用

以编程方式设置它
gif.setImageResource(R.anim.anim);
那时它崩溃了。我尝试将动画移动到drawable文件夹但仍然没有运气。 有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您必须从主线程以外的其他线程运行可绘制的动画,然后像这段代码片段一样更新您的代码,它将起作用。

在您的活动中,请执行以下操作:

public class Menu extends Activity {

    AnimationDrawable animationDrawable;
    ImageView gif ;

        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.menu);

            // the fix is here
            gif = (ImageView)findViewById(R.id.banim);
            gif.setImageResource(R.anim.anim);

            Starter starter = new Starter();
            gif.post(starter);

            final Intent intent = new Intent(this, MyActivity.class);
            final Intent intent1 = new Intent(this, Achievements.class);

            final ImageView bplay = (ImageView)findViewById(R.id.play);
            final ImageView bachieve = (ImageView)findViewById(R.id.achievements);

            final View.OnClickListener buttonClickListener;
            buttonClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (view == bplay){
                        startActivity(intent);
                    }else if (view == bachieve){
                        startActivity(intent1);
                    }

                }
            };

            bplay.setOnClickListener(buttonClickListener);
            bachieve.setOnClickListener(buttonClickListener);
        }

            class Starter implements Runnable {
                public void run() {

                    // Get the background, which has been compiled to an
                    // AnimationDrawable
                    // object.
                    AnimationDrawable frameAnimation = (AnimationDrawable) gif
                            .getBackground();

                    frameAnimation.setOneShot(false);

                    // Start the animation (looped playback by default).
                    frameAnimation.start();
                }
            }
}