Skip to content Skip to sidebar Skip to footer

Imageview: Automatically Recycle Bitmap If Imageview Is Not Visible (within Scrollview)

So, I've been looking at the ImageView source for a while, but I haven't figured out a hook to do this yet. The problem: having, let's say, 30 400x800 images inside a ScrollView (

Solution 1:

Call this method when you want to recycle your bitmaps.

publicstaticvoidrecycleImagesFromView(View view) {
            if(view instanceof ImageView)
            {
                Drawabledrawable= ((ImageView)view).getDrawable();
                if(drawable instanceof BitmapDrawable)
                {
                    BitmapDrawablebitmapDrawable= (BitmapDrawable)drawable;
                    bitmapDrawable.getBitmap().recycle();
                }
            }

            if (view instanceof ViewGroup) {
                for (inti=0; i < ((ViewGroup) view).getChildCount(); i++) {
                    recycleImagesFromView(((ViewGroup) view).getChildAt(i));
                }
            }
        }

Solution 2:

Have a look at the android documentation about "How to draw bitmaps" Also the code example called bitmap fun.

If you use a ListView or a GridView the ImageView objects are getting recycled and the actual bitmap gets released to be cleaned up by the GC.

You also want to resize the original images to the screen size and cache them on disk and/or RAM. This will safe you a lot of space and the actual size should get down to a couple of hundert KB. You can also try to display the images in half the resolution of your display. The result should still be good while using much less RAM.

Solution 3:

What you really want is to use an ArrayAdapter for this. If, like your notes suggest, that is not possible (although i don't see why) take a look at the source for array adapter and rewrite one to your own needs.

Post a Comment for "Imageview: Automatically Recycle Bitmap If Imageview Is Not Visible (within Scrollview)"