我使用的图像设置为背景为我所有的活动,但它是造成内存溢出问题和系统崩溃的应用程序。 现在我解除绑定暂停我可绘制()和destroy()方法在我的活动,现在,它显示了按后退按钮空白屏幕。 那么,如何避免这种情况,而无需使用额外的内存。
protected void onPause(){
super.onPause();
unbindDrawables(findViewById(R.id.login_root));
}
protected void onDestroy() {
unbindDrawables(findViewById(R.id.login_root));
super.onDestroy();
}
private void unbindDrawables(View view) {
System.gc();
Runtime.getRuntime().gc();
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();
}
起初,我在不断膨胀采用了android我的布局:背景=“@绘制/”总是造成内存溢出错误,指出VM不会让我们分配10MB现在我正在从绘制一个位图没有缩小和有约束力的(应用)。它runtime.Now它说VM不会让我们没有使用unbindDrawables(..)显然,这显示已经下降的背景图像质量分配5MB(应用程序),但我不能够明白,如果我使用的是png格式13KB的文件,如何JVM需要5或10MB的空间来处理请求?
我从的onCreate改变我的布局语句()来的onResume()方法,但应用程序再次运行的内存上按后退按钮。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
protected void onResume(){
setContentView(R.layout.home);
Bitmap bmp;
ImageView background = (ImageView)findViewById(R.id.iv_home_background);
InputStream is = getResources().openRawResource(R.drawable.background);
bmp = BitmapFactory.decodeStream(is);
background.setImageBitmap(bmp);
super.onResume();
}
protected void onPause(){
super.onPause();
unbindDrawables(findViewById(R.id.home_root));
}
private void unbindDrawables(View view) {
System.gc();
Runtime.getRuntime().gc();
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();
}
}