我有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
时做了两件事 它帮助我恢复了此错误,但not longer
。所以有人建议我做什么?
答案 0 :(得分:0)
在每个活动结束时,通过在位图上调用bitmap.recycle()
(使用component.getDrawable().getBitmap()
获取它们)并将null
设置为对象来释放位图和大对象。
答案 1 :(得分:0)
我认为你的可绘图像太大了。你应该减少图像尺寸。如果图像尺寸应该是可绘制的 - 128 * 128,它会给出最好的结果。