我有精靈動畫,如奔跑,睡眠,走路,跳躍等。這些動畫中的每一個都可以自行工作,但是當我嘗試使用setBackgroundResource()將動畫更改爲另一組動畫時,在前一個動畫處於活動狀態時,我遇到了內存問題。我相信以前的動畫集沒有從內存中清除。可以做些什麼來避免內存問題?如何在無法運行到OOM的情況下無縫更改精靈動作動畫?
內存錯誤:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.debug, PID: 5668
java.lang.OutOfMemoryError: Failed to allocate a 5644812 byte allocation with 1290256 free bytes and 1260KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:4215)
at android.content.res.Resources.loadDrawable(Resources.java:4089)
at android.content.res.Resources.loadDrawable(Resources.java:3939)
at android.content.res.TypedArray.getDrawable(TypedArray.java:886)
at android.graphics.drawable.AnimationDrawable.inflateChildElements(AnimationDrawable.java:324)
at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:294)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:2549)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:2320)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:4210)
at android.content.res.Resources.loadDrawable(Resources.java:4089)
at android.content.res.Resources.getDrawable(Resources.java:2005)
at android.content.res.Resources.getDrawable(Resources.java:1987)
at android.content.Context.getDrawable(Context.java:464)
at android.view.View.setBackgroundResource(View.java:18537)
我在繪製對象的文件夾不同的動畫文件。這是我的運行xml設置。
anim_sprite_run.xml
<?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/run0001" android:duration="50" />
<item android:drawable="@drawable/run0002" android:duration="50" />
<item android:drawable="@drawable/run0003" android:duration="50" />
<item android:drawable="@drawable/run0004" android:duration="50" />
<item android:drawable="@drawable/run0005" android:duration="50" />
<item android:drawable="@drawable/run0006" android:duration="50" />
<item android:drawable="@drawable/run0007" android:duration="50" />
<item android:drawable="@drawable/run0008" android:duration="50" />
<item android:drawable="@drawable/run0009" android:duration="50" />
<item android:drawable="@drawable/run0010" android:duration="50" />
<item android:drawable="@drawable/run0011" android:duration="50" />
<item android:drawable="@drawable/run0012" android:duration="50" />
<item android:drawable="@drawable/run0013" android:duration="50" />
<item android:drawable="@drawable/run0014" android:duration="50" />
<item android:drawable="@drawable/run0015" android:duration="50" />
<item android:drawable="@drawable/run0016" android:duration="50" />
<item android:drawable="@drawable/run0017" android:duration="50" />
<item android:drawable="@drawable/run0018" android:duration="50" />
<item android:drawable="@drawable/run0019" android:duration="50" />
<item android:drawable="@drawable/run0020" android:duration="50" />
<item android:drawable="@drawable/run0021" android:duration="50" />
<item android:drawable="@drawable/run0022" android:duration="50" />
<item android:drawable="@drawable/run0023" android:duration="50" />
<item android:drawable="@drawable/run0024" android:duration="50" />
<item android:drawable="@drawable/run0025" android:duration="50" />
</animation-list>
在代碼中,我開始跑步動畫這樣
ivSprite.setBackgroundResource(R.drawable.anim_sprite_run);
ivSprite.post(new Runnable() {
@Override
public void run() {
mAnimationDrawable = (AnimationDrawable) ivSprite.getBackground();
mAnimationDrawable.start();
}
});
10秒後,我打電話的代碼片段另一個引用睡眠動畫。這是它崩潰由於OOM
ivSprite.setBackgroundResource(R.drawable.anim_sprite_sleep);
ivSprite.post(new Runnable() {
@Override
public void run() {
mAnimationDrawable = (AnimationDrawable) ivSprite.getBackground();
mAnimationDrawable.start();
}
});
在調用睡眠動畫我曾嘗試以下:
ivSprite.setBackgroundResource(0);
ivSprite.clearAnimation();
mAnimationDrawable.stop();
我嘗試將背景設置爲空,以清除先前的喧鬧。然後我調用clearAnimation()來獲得額外的保證。最後我停止了以前的動畫。這些都不能防止OOM錯誤,並且我沒有想法。
在此先感謝您的幫助。