1.Activity中Bitmap的回收
public void recycleAllImages () {
Logger.d("---------recycleView=====::" + getClass().getSimpleName());
View decorView = getWindow().getDecorView();
recycleViewImages(decorView);
}
2.View中Bitmap的回收
if (view == null) {
return ;
}
Logger.d("---------recycleView=====:" + view.getClass().getSimpleName());
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null && bgDrawable instanceof BitmapDrawable) {
BitmapDrawable bd = (BitmapDrawable) bgDrawable;
if (bd != null) {
if (bd.getBitmap() != null) {
bd.getBitmap().recycle();
}
}
}
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i=0; i<group.getChildCount(); i++) {
recycleViewImages(group.getChildAt(i));
}
} else {
if (view instanceof ImageView || view instanceof ImageButton) {
ImageView iv = (ImageView) view;
Drawable drawable = iv.getDrawable();
if (drawable != null && drawable instanceof BitmapDrawable) {
BitmapDrawable bd = (BitmapDrawable) drawable;
if (bd != null) {
if (bd.getBitmap() != null) {
bd.getBitmap().recycle();
}
}
}
}
}
}
本文探讨了在Activity中如何有效地回收Bitmap资源,包括通过`recycleAllImages`方法进行全局回收,以及针对View中Bitmap的特定回收策略,旨在解决内存泄漏问题,优化应用性能。
408

被折叠的 条评论
为什么被折叠?



