Images in Android memory

Source: Internet
Author: User

The size of the picture in memory

There is an inner class Bitmap.config class in the Android.graphics.Bitmap class, CreateBitmap (intwidth, int height, bitmap.config Config) in the Bitmap class Method will be used, open a look at this class


Enumeration variables
public static final Bitmap.config Alpha_8
public static final Bitmap.config argb_4444
public static final Bitmap.config argb_8888
public static final Bitmap.config rgb_565

In plain alpha_8, Alpha is made up of 8 bits.
The argb_4444 is made up of 4 4-bit, or 16-bit,
The argb_8888 is made up of 4 8-bit, or 32-bit,
Rgb_565 is R is 5 bits, G is 6 bits, B is 5 bits total 16 bits

Thus:
Alpha_8 represents a 8-bit alpha bitmap
argb_4444 represents a 16-bit ARGB bitmap
argb_8888 represents a 32-bit ARGB bitmap
rgb_565 represents a 8-bit RGB bitmap

The higher the number of bitmap bits, the more color information they can store, and the more realistic the image will be.

The resolution of the image is 3776 * 2520, each point is made up of ARGB color, each pigment accounted for 4 byte, so this picture loaded into memory needs to consume the memory:
3776 * 2520 * 4byte = 38062080byte, can cause memory overflow, then how to load the big picture?

How to load a large resolution picture

Sometimes we do need to load some large-resolution images, but for mobile devices, even if loading a successful memory is also a waste (screen resolution limit), so you need to find a way to compress the picture at a certain rate, so that the resolution is reduced so that it does not need to spend a lot of heap memory space, You can also maximize the screen resolution of the device to display pictures. A Bitmapfactory.options object is used here, which is described below.
Bitmapfactory.options is an internal class for bitmapfactory, which is used primarily to set up some information about loading images with bitmapfactory. Here are the attributes that you need to use in the options:
Injustdecodebounds: If set to true, the image's pixel array will not be loaded into memory, loading only some extra data into the options.
Outheight: The height of the picture.
Outwidth: The width of the picture.
Insamplesize: If set, the picture will be loaded based on this sample rate and cannot be set to a number less than 1. For example, set to 4, the resolution width and height will be the original 1/4, this time the total memory will be the original 1/16.

Example code:

1 ImportAndroid.os.Bundle;2 Importandroid.os.Environment;3 Importandroid.app.Activity;4 ImportAndroid.graphics.Bitmap;5 Importandroid.graphics.BitmapFactory;6 Importandroid.graphics.BitmapFactory.Options;7 ImportAndroid.view.Menu;8 ImportAndroid.view.View;9 ImportAndroid.view.WindowManager;Ten ImportAndroid.widget.Button; One ImportAndroid.widget.ImageView; A  Public classMainactivityextendsActivity { -     PrivateButton btn_loadimage; -     PrivateImageView iv_bigimage; the @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); +Btn_loadimage =(Button) Findviewbyid (r.id.btn_loadimage); -Iv_bigimage =(ImageView) Findviewbyid (r.id.iv_bigimage); +Btn_loadimage.setonclicklistener (NewView.onclicklistener () { A @Override at              Public voidOnClick (View v) { -                 //Bitmap bitmap=bitmapfactory.decodefile ("/sdcard/a.jpg"); -                 //Iv_bigimage.setimagebitmap (bitmap); -Bitmapfactory.options opts =NewOptions (); -                 //do not read the array of pixels into memory, only the information that reads the picture -Opts.injustdecodebounds =true; inBitmapfactory.decodefile ("/sdcard/a.jpg", opts); -                 //get the resolution of the image from the options to                 intImageHeight =Opts.outheight; +                 intImageWidth =Opts.outwidth; -                  //Get the Android screen service theWindowManager WM =(WindowManager) Getsystemservice (window_service); *                 //get the screen resolution, GetHeight (), GetWidth has been discarded $                 //GetSize () should be used, but they are still used for backwards compatibility.Panax Notoginseng                 intWindowHeight =Wm.getdefaultdisplay (). GetHeight (); -                 intWindowWidth =Wm.getdefaultdisplay (). getwidth (); the                  //Calculate sample Rate +                 intScaleX = imagewidth/WindowWidth; A                 intScaleY = imageheight/WindowHeight; the                 intScale = 1; +                 //sample Rate According to the maximum direction -                 if(ScaleX > ScaleY && scaleY >= 1) { $Scale =ScaleX; $                 } -                 if(ScaleX < ScaleY && ScaleX >= 1) { -Scale =ScaleY; the                 }  -                  //False indicates that the number of pixels in the image is read into memory, according to the set sampling rateWuyiOpts.injustdecodebounds =false; the                 //Sample Rate -Opts.insamplesize =Scale ; WuBitmap Bitmap = Bitmapfactory.decodefile ("/sdcard/a.jpg", opts); - Iv_bigimage.setimagebitmap (bitmap); About              } $         }); -     } - } -   
View Code


Loading thumbnail images

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.

Things to watch out for

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.

1 /**2  * http://developer.android.com/training/displaying-bitmaps/load-bitmap.html3  */4  Public classBitmaputils {5     Private Static intcalculateinsamplesize (bitmapfactory.options Options,6             intReqwidth,intreqheight) {7         Final intHeight =Options.outheight;8         Final intwidth =Options.outwidth;9         intInsamplesize = 1;Ten         if(Height > Reqheight | | width >reqwidth) { One             Final intHalfheight = HEIGHT/2; A             Final intHalfwidth = WIDTH/2; -              while((halfheight/insamplesize) >Reqheight -&& (halfwidth/insamplesize) >reqwidth) { theInsamplesize *= 2; -             } -         } -         returninsamplesize; +     } -   +     //if the image is enlarged, filter determines whether it is smooth, and if the image is reduced, filter has no effect A     Private StaticBitmap Createscalebitmap (Bitmap src,intDstwidth, at             intdstheight) { -Bitmap DST = bitmap.createscaledbitmap (src, dstwidth, dstheight,false); -         if(src! = DST) {//If there is no scaling, then do not recycle -Src.recycle ();//release bitmap native array of pixels -         } -         returnDST; in     } -   to     //loading pictures from resources +      Public StaticBitmap Decodesampledbitmapfromresource (Resources res, -             intResId,intReqwidth,intreqheight) { the         FinalBitmapfactory.options Options =Newbitmapfactory.options (); *Options.injustdecodebounds =true; $Bitmapfactory.decoderesource (res, resId, options);//Read Pictures longPanax NotoginsengOptions.insamplesize =calculateinsamplesize (Options, Reqwidth, -Reqheight);//Calculate Insamplesize theOptions.injustdecodebounds =false; +Bitmap src = bitmapfactory.decoderesource (res, resId, options);//load a slightly larger thumbnail image A         returnCreatescalebitmap (SRC, reqwidth, reqheight);//get further thumbnail images of the target size the     } +   -     //loading pictures from SD card $      Public StaticBitmap decodesampledbitmapfromfd (String pathName, $             intReqwidth,intreqheight) { -         FinalBitmapfactory.options Options =Newbitmapfactory.options (); -Options.injustdecodebounds =true; the bitmapfactory.decodefile (pathName, options); -Options.insamplesize =calculateinsamplesize (Options, Reqwidth, reqheight);WuyiOptions.injustdecodebounds =false; theBitmap src =bitmapfactory.decodefile (pathName, options); -         returncreatescalebitmap (SRC, reqwidth, reqheight); Wu     } -}
View Code

Images in Android memory

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.