How to destroy the ImageView pulled out inside if we do not need it?

This question is related to. Do we need to explicitly process a bitmap image if we do not need it?

There is an ImageView with the ability to draw, when the user clicks the button, he will assign a new image to ImageView.

Do we need to destroy the old drawable owned by ImageView, and how?

Drawable oriDrawable = imageView.getDrawable() // set callback to null oriDrawable.setCallback(null); // get the bitmap and recycle it ((BitmapDrawable)oriDrawable).getBitmap().recycle(); 

Is the code correct? What is the best solution?

+4
source share
2 answers

Is this just a general question or is your memory running out? I would not do this optimization until you really have a problem.

In general, if you are loading bitmaps from drop-down folders that are small (large, like megabytes), you should not run into a problem.

First you need to make sure that the resources you load are optimal for where you display them, for example, it makes no sense to install ImageView on an image of size 1024 x 1024 if the area in which you display the image is 64x64.

An interruption in the budget of a bitmap image is usually caused by loading images with an unknown size, or simply simply displaying image sizes incorrectly, as described above. Typically, ImageView file sharing usually does not give you problems with optimal sized images.

Android Training has a great article that optimizes the loading of bitmaps http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Hope that helps

+3
source

You can try using something like:

 Drawable drawable = imageView.getDrawable(); if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; Bitmap bitmap = bitmapDrawable.getBitmap(); bitmap.recycle(); } 

Where imageView is your imageView .

The original answer is here .

+11
source

Source: https://habr.com/ru/post/1435537/


All Articles