Android:imageView.setImageResource(AnimationDrawable)导致内存错误

时间:2013-02-12 11:41:20

标签: android image animation memory

这就是我正在做的事情

ImageView image2 = (ImageView) findViewById(R.id.imageView3);
image2.getDrawable().setCallback(null);
image2.setImageResource(R.drawable.fire);

其中fire是一个提出动画可绘制的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/fire1" android:duration="50" />
<item android:drawable="@drawable/fire2" android:duration="50" />
<item android:drawable="@drawable/fire3" android:duration="50" />
<item android:drawable="@drawable/fire4" android:duration="50" />
<item android:drawable="@drawable/fire5" android:duration="50" />
...
<item android:drawable="@drawable/fire16" android:duration="50" />

我有这个错误

02-12 11:37:20.919: E/dalvikvm-heap(3193): 1535664-byte external allocation too large for this process.
02-12 11:37:20.959: E/GraphicsJNI(3193): VM won't let us allocate 1535664 bytes
02-12 11:37:20.959: W/dalvikvm(3193): threadid=1: thread exiting with uncaught exception (group=0x40018560)
02-12 11:37:20.979: E/AndroidRuntime(3193): FATAL EXCEPTION: main
02-12 11:37:20.979: E/AndroidRuntime(3193): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.content.res.Resources.loadDrawable(Resources.java:1709)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.content.res.Resources.getDrawable(Resources.java:581)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:267)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.content.res.Resources.getDrawable(Resources.java:581)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.widget.ImageView.resolveUri(ImageView.java:501)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.widget.ImageView.setImageResource(ImageView.java:280)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at com.example.kersplatt.GameActivity.onCreate(GameActivity.java:71)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.os.Looper.loop(Looper.java:130)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at java.lang.reflect.Method.invoke(Method.java:507)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
02-12 11:37:20.979: E/AndroidRuntime(3193):     at dalvik.system.NativeStart.main(Native Method)

我读了很多关于内存泄漏,内存错误(例如java.lang.OutOfMemoryError: bitmap size exceeds VM budget - Android)的线程但是我没有成功解决我的问题

当我image2.setImageResource(R.drawable.fire);重新image2.setImageResource(R.drawable.fire1);,其中fire1是一个简单的png,一切正常 我该怎么做才能解决我的问题

1 个答案:

答案 0 :(得分:1)

要删除OutOfMemory错误,您应该专注于缩放。您可以在http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

找到