OutOfMemory错误android

时间:2013-07-10 11:21:35

标签: android android-animation out-of-memory

我正在开发一款安卓游戏。它几乎已经完成并尝试了10.1平板电脑,nexus 7,nexus 4,HTC Senstation,它对所有这些都有效。但是,当我在Galaxy s3上尝试它时,当我在帧动画xml中调用pics时,我得到了OutofMemory错误。现在我通过调整xhdpi文件夹中的图片来解决这个问题,但结果并不令人满意。有没有足够的方法可以用来重新缩放以动画形式显示的图片以进行帧动画。

以下是代码:

public class Logo extends Activity {

    private ImageView iv;
    Thread timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_logo);

        iv = (ImageView) findViewById(R.id.imageView1);
        iv.setBackgroundResource(R.anim.anime);

        // thread for animating the logo and moving to next activity after
        // display.
        timer = new Thread() {

            @Override
            public void run() {
                try {
                    sleep(7000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    //Intent i = new Intent();
                    //i.setClass(Logo.this, StartPoint.class);
                    //startActivity(i);
                }
            }
        };
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        AnimationDrawable anime = (AnimationDrawable) iv.getBackground();
        if (hasFocus) {
            anime.start();
            timer.start();
        } else {
            anime.stop();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.logo, menu);
        return true;
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        finish(); // to remove this activity.
    }
}

这是anime.xml:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android
android:oneshot="true">
<item android:drawable="@drawable/a1" android:duration="60" />
<item android:drawable="@drawable/a2" android:duration="60" />
<item android:drawable="@drawable/a3" android:duration="60" />
<item android:drawable="@drawable/a4" android:duration="60" />
<item android:drawable="@drawable/a5" android:duration="60" />
<item android:drawable="@drawable/a6" android:duration="60" />
<item android:drawable="@drawable/a7" android:duration="60" />
<item android:drawable="@drawable/a8" android:duration="60" />
<item android:drawable="@drawable/a9" android:duration="60" />
<item android:drawable="@drawable/a10" android:duration="60" />
<item android:drawable="@drawable/a11" android:duration="60" />
<item android:drawable="@drawable/a12" android:duration="60" />
<item android:drawable="@drawable/a13" android:duration="60" />
<item android:drawable="@drawable/a14" android:duration="60" />
<item android:drawable="@drawable/a15" android:duration="60" />
<item android:drawable="@drawable/a16" android:duration="60" />
<item android:drawable="@drawable/a17" android:duration="60" />
<item android:drawable="@drawable/a18" android:duration="60" />
<item android:drawable="@drawable/a19" android:duration="60" />
<item android:drawable="@drawable/a20" android:duration="60" />
<item android:drawable="@drawable/a21" android:duration="60" />
<item android:drawable="@drawable/a22" android:duration="60" />
<item android:drawable="@drawable/a23" android:duration="60" />
<item android:drawable="@drawable/a24" android:duration="60" />
<item android:drawable="@drawable/a25" android:duration="60" />
<item android:drawable="@drawable/a26" android:duration="60" />
<item android:drawable="@drawable/a27" android:duration="60" />

</animation-list>

现在我看到很多答案说明了bitmapFactory.options 但是如何在xml文件中应用这些选项,如果不可能的话。有没有其他方法我可以使用而不是调整图片的大小。 先感谢您。

这里我尝试应用程序代码,仍然遇到同样的错误:

//onCreate and thread then 
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);

    BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.a1);
    BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.a2);
    BitmapDrawable frame3 = (BitmapDrawable)getResources().getDrawable(R.drawable.a3);
    BitmapDrawable frame4 = (BitmapDrawable)getResources().getDrawable(R.drawable.a4);
    BitmapDrawable frame5 = (BitmapDrawable)getResources().getDrawable(R.drawable.a5);
    BitmapDrawable frame6 = (BitmapDrawable)getResources().getDrawable(R.drawable.a6);
    BitmapDrawable frame7 = (BitmapDrawable)getResources().getDrawable(R.drawable.a7);
    BitmapDrawable frame8 = (BitmapDrawable)getResources().getDrawable(R.drawable.a8);
    BitmapDrawable frame9 = (BitmapDrawable)getResources().getDrawable(R.drawable.a9);
    BitmapDrawable frame10 = (BitmapDrawable)getResources().getDrawable(R.drawable.a10);
    BitmapDrawable frame11 = (BitmapDrawable)getResources().getDrawable(R.drawable.a11);
    BitmapDrawable frame12 = (BitmapDrawable)getResources().getDrawable(R.drawable.a12);
    BitmapDrawable frame13 = (BitmapDrawable)getResources().getDrawable(R.drawable.a13);
    BitmapDrawable frame14 = (BitmapDrawable)getResources().getDrawable(R.drawable.a14);
    BitmapDrawable frame15 = (BitmapDrawable)getResources().getDrawable(R.drawable.a15);
    BitmapDrawable frame16 = (BitmapDrawable)getResources().getDrawable(R.drawable.a16);
    BitmapDrawable frame17 = (BitmapDrawable)getResources().getDrawable(R.drawable.a17);
    BitmapDrawable frame18 = (BitmapDrawable)getResources().getDrawable(R.drawable.a18);
    BitmapDrawable frame19 = (BitmapDrawable)getResources().getDrawable(R.drawable.a19);
    BitmapDrawable frame20 = (BitmapDrawable)getResources().getDrawable(R.drawable.a20);
    BitmapDrawable frame21 = (BitmapDrawable)getResources().getDrawable(R.drawable.a21);
    BitmapDrawable frame22 = (BitmapDrawable)getResources().getDrawable(R.drawable.a22);
    BitmapDrawable frame23 = (BitmapDrawable)getResources().getDrawable(R.drawable.a23);
    BitmapDrawable frame24 = (BitmapDrawable)getResources().getDrawable(R.drawable.a24);
    BitmapDrawable frame25 = (BitmapDrawable)getResources().getDrawable(R.drawable.a25);
    BitmapDrawable frame26 = (BitmapDrawable)getResources().getDrawable(R.drawable.a26);
    BitmapDrawable frame27 = (BitmapDrawable)getResources().getDrawable(R.drawable.a27);

    final AnimationDrawable anime = new AnimationDrawable();
    anime.setOneShot(true);
    anime.addFrame(frame1, 60);
    anime.addFrame(frame2, 60);
    anime.addFrame(frame3, 60);
    anime.addFrame(frame4, 60);
    anime.addFrame(frame5, 60);
    anime.addFrame(frame6, 60);
    anime.addFrame(frame7, 60);
    anime.addFrame(frame8, 60);
    anime.addFrame(frame9, 60);
    anime.addFrame(frame10, 60);
    anime.addFrame(frame11, 60);
    anime.addFrame(frame12, 60);
    anime.addFrame(frame13, 60);
    anime.addFrame(frame14, 60);
    anime.addFrame(frame15, 60);
    anime.addFrame(frame16, 60);
    anime.addFrame(frame17, 60);
    anime.addFrame(frame18, 60);
    anime.addFrame(frame19, 60);
    anime.addFrame(frame20, 60);
    anime.addFrame(frame21, 60);
    anime.addFrame(frame22, 60);
    anime.addFrame(frame23, 60);
    anime.addFrame(frame24, 60);
    anime.addFrame(frame25, 60);
    anime.addFrame(frame26, 60);
    anime.addFrame(frame27, 60);


    iv.setBackgroundDrawable(anime);
    if (hasFocus) {
        anime.start();
        timer.start();
    }

    else
        anime.stop();
}

1 个答案:

答案 0 :(得分:2)

实际上,Galaxy S3的规格相对较小,默认堆大小(32MB)。

您可以临时解决此问题,请求更大的堆并检查这是否可以解决您的问题。

<application android:largeHeap="true">
        . . .
</application>

然后我将研究帧资源的大小和类型。

干杯。