解除绑定可绘制的onPause()导致反应迟钝后退导航,以及跳过这一步的原因内存溢出迟钝、绑定、跳过、反应

2023-09-07 17:46:11 作者:悲伤撕不开止不住的流逝

我使用的图像设置为背景为我所有的活动,但它是导致内存溢出问题和系统崩溃的应用程序。现在我解除绑定我在暂停可绘制()和destroy()方法在我的活动,现在它显示了pressing后退按钮空白屏幕。那么,如何避免这种情况,而无需使用额外的内存。

 保护无效的onPause(){    super.onPause();    unbindDrawables(findViewById(R.id.login_root));}保护无效的onDestroy(){        unbindDrawables(findViewById(R.id.login_root));        super.onDestroy();      }私人无效unbindDrawables(查看视图){    System.gc()的;    调用Runtime.getRuntime()GC()。    如果(view.getBackground()!= NULL){    view.getBackground()setCallback(空)。    }    如果(查看的instanceof的ViewGroup){        的for(int i = 0;我≤((ViewGroup中)视图).getChildCount();我++){        unbindDrawables(((ViewGroup中)视图).getChildAt(一));        }    ((ViewGroup中)视图).removeAllViews();    } 

起初我是充气采用了android我的布局:(APP)背景=@绘制/它总是导致内存溢出错误,指出VM不会让我们分配10MB现在我从绘制得到一个位图无缩放下来并绑定在runtime.Now它说,虚拟机不会让我们没有使用unbindDrawables分配5MB(APP)(..)显然,这是所表现出的背景图像质量有所下降,但我无法理解,如果我使用13KB的PNG文件,请问JVM需要5或10MB的空间来处理请求?

我从的onCreate()来onResume()方法,但再次申请改变我的布局语句耗尽内存在pressing后退按钮。

 公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);    }  保护无效onResume(){        的setContentView(R.layout.home);        BMP位图;        ImageView的背景=(ImageView的)findViewById(R.id.iv_home_background);        InputStream为= getResources()openRawResource(R.drawable.background)。        BMP = BitmapFactory.de codeStream(是);        background.setImageBitmap(BMP);         super.onResume();    } 保护无效的onPause(){        super.onPause();        unbindDrawables(findViewById(R.id.home_root));    } 私人无效unbindDrawables(查看视图){        System.gc()的;        调用Runtime.getRuntime()GC()。        如果(view.getBackground()!= NULL){        view.getBackground()setCallback(空)。        }        如果(查看的instanceof的ViewGroup){            的for(int i = 0;我≤((ViewGroup中)视图).getChildCount();我++){            unbindDrawables(((ViewGroup中)视图).getChildAt(一));            }        ((ViewGroup中)视图).removeAllViews();        }    } 

解决方案

我已经找到了工作,这个问题的解决。现在,我在运行时缩放我的位图一个非常小的尺寸,然后将它们存储在内部存储。该程序在运行时调用从存储的缩放位图和如果它不是present那里,它从绘制文件夹的话来说,它的规模,它写入存储,然后将其绑定到视图。 以这种方式,不需要调用unbindDrawables方法在任何时间点和应用程序保持整个响应。  我唯一​​担心的,现在是位图的品质,我想我需要发挥与周围的​​缩放大小,找出最大质量尽可能少的大小。

华硕A43E求重装系统步骤

I am using an image to set as the background for all my activities but it was causing a memory overflow issue and crash the app. Now I am unbinding my drawables on pause() and on Destroy() in my activity and now it shows blank screen on pressing back button. So how can I avoid this without using extra memory.

    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();
    }

Initially I was inflating my layout using android:background="@drawable/" which always caused memory overflow error saying that VM won't let us allocate 10MB (app.) Now I am getting a bitmap from that drawable without scaling down and binding it on runtime.Now it says VM won't let us allocate 5MB (app.) without using unbindDrawables(..) Obviously the quality of background image which is shown has decreased but I am not able to understand that if I am using a png file of 13KB, how does JVM requires 5 or 10MB space to process the request ?

I have shift my layout statements from onCreate() to onResume() method but the application again runs out of memory on pressing back button.

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();
        }
    }

解决方案

I have found a work around for this problem. Now I am scaling my bitmaps at runtime to a very small size and then storing them in internal storage. The program calls the scaled bitmaps from the storage at runtime and if its not present there, it calls it from drawable folder, scale it, write it to storage and then bind it to the view. In this way there is no need to call unbindDrawables method at any point of time and the application remains responsive throughout. My only concern right now is the quality of bitmaps, I think I need to play around with the scaling size to find out the least possible size with maximum quality.