Thumbnail loading in Android-don't waste a bit of extra memory

Source: Internet
Author: User

This article links http://blog.csdn.net/xiaodongrush/article/details/29355651

1. Why do you want to load the thumbnail image?

Sometimes you don't need to show the original, just show the thumbnail of the picture and save the memory. For example: NetEase news in the picture browsing, the left to show the Little lion Picture is a thumbnail, click on this image, will show the original.

2. How, what do you do?

Http://developer.android.com/training/displaying-bitmaps/load-bitmap.html gives a way to load a thumbnail image of a picture.
Bitmapfactory#decodefile (String pathName, Bitmapfactory.options opts), opts can specify two parameters for Injustdecodebounds and insamplesize. When Injustdecodebounds is specified, only the length and width of the image are resolved, not the picture is loaded. When Insamplesize is specified, a thumbnail is loaded according to Insamplesize. For example insamplesize=4, the loaded thumbnail is 1/4 of the size of the original image.
How much do you want to set insamplesize? Assuming the original image is 1500x700, the space we set aside for the thumbnail is 100x100. So Insamplesize=min (1500/100, 700/100) = 7. The thumbnail we can get is 1/7 of the original image. Here if you want to ask 15:7 of the pictures how to show in the area of 1:1, please go to see the ScaleType property of ImageView.
But the fact is not so perfect, although set insamplesize=7, but the resulting thumbnail is 1/4 of the original, because Insamplesize can only be 2 of the whole number of power, if not, down to achieve the maximum of 2 of the whole number of power, 7 down looking for 2 of the whole power, is 4.
How can we not waste memory? Big Bear classmate found a method: Bitmap#createscaledbitmap (Bitmap src, int dstwidth, int dstheight, Boolean filter), This method can generate a bitmap of a specified size bitmap, which can enlarge the image or reduce the image.

Final thumbnail loading process:

1. Use Injustdecodebounds to read the length and width of the bitmap.
2. Calculate the size of the insamplesize according to the length and width of the bitmap and the target thumbnail.
3. Using Insamplesize, load a larger thumbnail a
4. Using Createscasebitmap, use thumbnail A to generate the thumbnail b we need.
5. Shrink-back sketch A.

3. Notice, things to be aware of

Createscasebitmap if the original and target thumbnail size is the same, then a new bitmap will not be generated to directly return bitmap, so when recycling, to determine whether thumbnail A is thumbnail b, if so, do not recycle.

4. Code

/** * http://developer.android.com/training/displaying-bitmaps/load-bitmap.html */public class Bitmaputils {private S tatic int calculateinsamplesize (bitmapfactory.options Options, int reqwidth, int reqheight) {Final int        Height = options.outheight;        Final int width = options.outwidth;        int insamplesize = 1;            if (Height > Reqheight | | width > reqwidth) {final int halfheight = HEIGHT/2;            Final int halfwidth = WIDTH/2; while ((halfheight/insamplesize) > Reqheight && (halfwidth/insamplesize) > Reqwidth)            {insamplesize *= 2;    }} return insamplesize;            }//If the image is enlarged, filter determines whether it is smooth, if the image is scaled down, the filter has no effect on the private static Bitmap Createscalebitmap (Bitmap src, int dstwidth,        int dstheight) {Bitmap dst = bitmap.createscaledbitmap (src, dstwidth, dstheight, false); if (src! = DST) {//If not scaled, then do not recycle SRC.RECYCLE ();    Releases the bitmap native pixel group} return DST; }//Load pictures from resources public static Bitmap Decodesampledbitmapfromresource (resources res, int resId, int r        eqwidth, int reqheight) {final bitmapfactory.options Options = new Bitmapfactory.options ();        Options.injustdecodebounds = true; Bitmapfactory.decoderesource (res, resId, options); Read pictures Long options.insamplesize = calculateinsamplesize (options, Reqwidth, reqheight);        Calculate Insamplesize options.injustdecodebounds = false; Bitmap src = bitmapfactory.decoderesource (res, resId, options); Load a slightly larger thumbnail return createscalebitmap (src, reqwidth, reqheight); Further get the target size thumbnail}//load picture from SD card public static Bitmap Decodesampledbitmapfromfd (String pathName, int re        qwidth, int reqheight) {final bitmapfactory.options Options = new Bitmapfactory.options ();        Options.injustdecodebounds = true; Bitmapfactory.decodefile (PathName, Options);        Options.insamplesize = calculateinsamplesize (options, Reqwidth, reqheight);        Options.injustdecodebounds = false;        Bitmap src = bitmapfactory.decodefile (pathName, Options);    Return Createscalebitmap (SRC, reqwidth, reqheight); }}

5. Test code

Code Download LINK http://download.csdn.net/detail/u011267546/7485045

In the drawable-xxhdpi directory, a total of four large figures, each average 5-6mb, altogether 22.9MB. If the original loading is easy, it crashes. If loaded using the method above, only 234KB is loaded. Here's how 234KB came, 4 ImageView, target size is 75dip*50dip, on my phone galaxynexus above, converted to PX is 150px*100px=15000 pixels, each pixel using argb_ 8888-way Storage, 4 bytes required. A picture needs 15000*4=60000 bytes, a total of 4 bitmap, then is 60000*4=240000 byte =234.375 KB.

How much memory is needed if it is covered with a screen? My phone is 768*1280 pixels, argb_8888 way storage, 4 bytes per pixel, then is 768*1280*4=3840KB=3.75MB. So, to cache 3 screen images, it's about 11MB. When doing the internal cache, it is necessary to consider this problem.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.