Ati*_*der 6 android android-image android-layout android-memory
我使用图像设置为我所有活动的背景但它导致内存溢出问题并使应用程序崩溃.现在我在我的活动中解除暂停()和Destroy()上的drawables,现在按下后退按钮显示空白屏幕.那么如何在不使用额外内存的情况下避免这种情况
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();
}
Run Code Online (Sandbox Code Playgroud)
最初我使用android:background ="@ drawable /"来夸大我的布局,这总是导致内存溢出错误,说VM不会让我们分配10MB(app.)现在我从那个drawable得到一个位图而不缩小和绑定它在运行时.现在它说VM不会让我们分配5MB(app.)而不使用unbindDrawables(..)显然,显示的背景图像的质量已经下降但是我无法理解如果我使用的是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();
}
}
Run Code Online (Sandbox Code Playgroud)
我已经找到解决这个问题的方法。现在,我在运行时将位图缩放到非常小的尺寸,然后将它们存储在内部存储中。程序在运行时从存储中调用缩放后的位图,如果存储中不存在该位图,则会从可绘制文件夹中调用它,缩放它,将其写入存储中,然后将其绑定到视图。通过这种方式,无需在任何时间点调用 unbindDrawables 方法,并且应用程序始终保持响应。我现在唯一关心的是位图的质量,我认为我需要调整缩放尺寸以找出具有最高质量的最小可能尺寸。