我有 5 个 FrameLayout,在所有屏幕中,我都在使用AnimationDrawable
类进行一些动画,并且当 Activity 进入另一个活动或更远时。
编辑 1 以获取我正在使用的代码片段:
iView_cow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try {
startCowBlinking.stop();
iView_cow.setBackgroundResource(R.drawable.cow_turn_movement);
startCowAnimation = (AnimationDrawable) iView_cow.getBackground();
startCowAnimation.start();
mHandler.postDelayed(r, 4000);
} catch (Exception e) {
// TODO: handle exception
}
}
});
mHandler = new Handler();
r = new Runnable() {
@Override
public void run() {
try {
if(startCowAnimation.isRunning()){
startCowAnimation.stop();
iView_cow.setBackgroundResource(R.drawable.cow_blink);
startCowBlinking = (AnimationDrawable) iView_cow.getBackground();
startCowBlinking.start();
}
} catch (Exception e) {
// TODO: handle exception
}
}
};
像这个动画一样,我在每个活动上使用 3-4 个动画。应用程序运行良好,但一段时间后我收到此错误:
dalvikvm-heap: Out of memory on a 4840016-byte allocation.
和
10-26 00:57:58.601: E/AndroidRuntime(12952): FATAL EXCEPTION: main
10-26 00:57:58.601: E/AndroidRuntime(12952): java.lang.OutOfMemoryError
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:577)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:775)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.content.res.Resources.loadDrawable(Resources.java:1998)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.content.res.Resources.getDrawable(Resources.java:707)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:280)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:869)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:806)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.content.res.Resources.loadDrawable(Resources.java:1983)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.content.res.Resources.getDrawable(Resources.java:707)
对于解决方案,我在 Activity Switch 时做了两件事
- 通过finish()完成当前活动;
- 使用stop() 完成所有 AnimationDrawable;
它帮助我恢复了这个错误,但是not longer
。所以有人建议我做什么?