我已经准备了可绘制动画的数量。当应用程序启动时,第一个动画将启动。我有两个按钮(下一个和上一个)具有相同的活动。当我单击下一个按钮时,我遇到了异常,例如,
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
我的代码是,
为了从drawable获取drawable动画,
private void getAnimationForLetters(int mAnim) {
// TODO Auto-generated method stub
String anim = "capital_anim_" + new Integer(mAnim).toString();
final int resID = getApplicationContext().getResources().getIdentifier(
anim, "drawable", "my-package-name");
mImgLetter.clearAnimation();
mImgLetter.setBackgroundResource(resID);
mImgLetter.post(new Runnable() {
public void run() {
loadingAnimation = (AnimationDrawable) mImgLetter
.getBackground();
loadingAnimation.start();
}
});
}
我的下一个按钮代码是,
case R.id.btnNext_:
unbindDrawables(findViewById(R.id.imgLetter));
mLetterNum=mLetterNum+1;
getAnimationForLetters(mLetterNum);
我的undind可绘制方法代码是,
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
最后我得到了例外java.lang.OutOfMemoryError: bitmap size exceeds VM budget
这里显示异常mImgLetter.setBackgroundResource(resID);
请帮我。
我添加了以下代码来清除,
loadingAnimation.stop();
for (int i = 0; i < loadingAnimation.getNumberOfFrames(); ++i){
Drawable frame = loadingAnimation.getFrame(i);
if (frame instanceof BitmapDrawable) {
((BitmapDrawable)frame).getBitmap().recycle();
}
frame.setCallback(null);
}
loadingAnimation.setCallback(null);
它仅适用于下一个或上一个。第一次单击下一个动画移动到第二个动画,第二次如果我单击上一个按钮,我会遇到异常,
Canvas: trying to use a recycled bitmap android.graphics.Bitmap
请帮我。