The size of the picture in memory

Source: Internet
Author: User

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 Import android.os.Bundle; 2 Import android.os.Environment; 3 Import android.app.Activity; 4 Import Android.graphics.Bitmap; 5 Import Android.graphics.BitmapFactory; 6 Import android.graphics.BitmapFactory.Options; 7 Import Android.view.Menu; 8 Import Android.view.View; 9 Import android.view.windowmanager;10 import android.widget.button;11 import Android.widget.ImageView; public class Mainactivity extends Activity {private Button btn_loadimage;14 private ImageView iv_bigimage; 1 5 @Override16 protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 1 8 Setcontentview (R.layout.activity_main); Btn_loadimage = (Button) Findviewbyid (r.id.btn_loadimage), iv_bigimage = (ImageView) Findviewbyid (R. Id.iv_bigimage); Btn_loadimage.setonclicklistener (New View.onclicklistener () {@Override23 public V OID OnClick (View v) {Bitmap///Bitmap=bitmapfactoRy.decodefile ("/sdcard/a.jpg");//Iv_bigimage.setimagebitmap (bitmap);                 Bitmapfactory.options opts = new Options (); 27//Do not read the array of pixels into memory, only the information of the picture is read 28 Opts.injustdecodebounds = true;29 bitmapfactory.decodefile ("/sdcard/a.jpg", opts); 30// Obtain the resolution of the image from the options of int imageheight = opts.outheight;32 int imagewidth = opts.outwidth; 33//Get Android screen service WindowManager wm = (WindowManager) getsystemservice (window_service                 ); 35//Get screen resolution, getheight (), GetWidth has been discarded 36//should use GetSize (), but here for backwards compatibility so still use them 37 int windowheight = Wm.getdefaultdisplay (). getheight (); int windowwidth = Wm.getdefaultdisplay (). GetWidth (); 39//Calculate sample rate ScaleX int = imagewidth/windowwidth;41 int ScaleY = image height/windowheight;42 int Scale = 1;43//Sample rate according to the maximum direction of the IF (ScaleX > ScaleY && scaleY >= 1) {45 Scale = scalex;46}47 if (ScaleX < ScaleY && ScaleX >= 1) {4                 8 scale = scaley;49}//False indicates that the number of pixels in the image is read in memory, according to the set sampling rate of 51                 Opts.injustdecodebounds = false;52//Sample rate is Opts.insamplesize = scale;54 Bitmap Bitmap = Bitmapfactory.decodefile ("/sdcard/a.jpg", opts); Iv_bigimage.setimagebitmap (Bitmap) ;    56}57}); 58}59}60

 1 Import android.os.Bundle; 2 Import android.os.Environment; 3 Import android.app.Activity; 4 Import Android.graphics.Bitmap; 5 Import Android.graphics.BitmapFactory; 6 Import android.graphics.BitmapFactory.Options; 7 Import Android.view.Menu; 8 Import Android.view.View; 9 Import android.view.windowmanager;10 import android.widget.button;11 import Android.widget.ImageView; public class Mainactivity extends Activity {private Button btn_loadimage;14 private ImageView iv_bigimage; 1 5 @Override16 protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 1 8 Setcontentview (R.layout.activity_main); Btn_loadimage = (Button) Findviewbyid (r.id.btn_loadimage), iv_bigimage = (ImageView) Findviewbyid (R. Id.iv_bigimage); Btn_loadimage.setonclicklistener (New View.onclicklistener () {@Override23 public V OID OnClick (View v) {Bitmap///Bitmap=bitmapfactoRy.decodefile ("/sdcard/a.jpg");//Iv_bigimage.setimagebitmap (bitmap);                 Bitmapfactory.options opts = new Options (); 27//Do not read the array of pixels into memory, only the information of the picture is read 28 Opts.injustdecodebounds = true;29 bitmapfactory.decodefile ("/sdcard/a.jpg", opts); 30// Obtain the resolution of the image from the options of int imageheight = opts.outheight;32 int imagewidth = opts.outwidth; 33//Get Android screen service WindowManager wm = (WindowManager) getsystemservice (window_service                 ); 35//Get screen resolution, getheight (), GetWidth has been discarded 36//should use GetSize (), but here for backwards compatibility so still use them 37 int windowheight = Wm.getdefaultdisplay (). getheight (); int windowwidth = Wm.getdefaultdisplay (). GetWidth (); 39//Calculate sample rate ScaleX int = imagewidth/windowwidth;41 int ScaleY = image height/windowheight;42 int Scale = 1;43//Sample rate according to the maximum direction of the IF (ScaleX > ScaleY && scaleY >= 1) {45 Scale = scalex;46}47 if (ScaleX < ScaleY && ScaleX >= 1) {4                 8 scale = scaley;49}//False indicates that the number of pixels in the image is read in memory, according to the set sampling rate of 51                 Opts.injustdecodebounds = false;52//Sample rate is Opts.insamplesize = scale;54 Bitmap Bitmap = Bitmapfactory.decodefile ("/sdcard/a.jpg", opts); Iv_bigimage.setimagebitmap (Bitmap) ;    56}57}); 58}59}60

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.html 3 */4 public class Bitmaputils {5 private static int calculateinsamplesize (Bitmapfactory.options Options, 6 int reqwidth, int reqheight) { 7 Final int height = options.outheight; 8 final int width = options.outwidth; 9 int insamplesize = 1;10 if (height > Reqheight | | width > reqwidth) {one final int half Height = height/2;12 final int halfwidth = width/2;13 while (halfheight/insamplesize) > reqHeight14 && (halfwidth/insamplesize) > Reqwidth) {insamplesize *= 2      ;}17}18 return insamplesize;19}20 21//If the image is enlarged, filter determines whether it is smooth, if the image is reduced, filter has no effect 22  private static Bitmap Createscalebitmap (Bitmap src, int dstwidth,23 int dstheight) {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 27}28 return dst;29}30 31//Load picture from resources, public static Bitmap Decodesampledbitmapfromresource (RESOURC Es res,33 int resId, int reqwidth, int reqheight) {final bitmapfactory.options Options = new Bitmap Factory.options (); options.injustdecodebounds = true;36 Bitmapfactory.decoderesource (res, resId, Options ); Read pictures Long options.insamplesize = calculateinsamplesize (options, reqwidth,38 reqheight); Calculation InSampleSize39 options.injustdecodebounds = false;40 Bitmap src = bitmapfactory.decoderesource (res, R Esid, Options); Load a slightly larger thumbnail with the return createscalebitmap (SRC, reqwidth, reqheight);             Further get target size thumbnail 42}43 44//Load picture from SD card public static Bitmap DECODESAMPLEDBITMAPFROMFD (String pathname,46 int reqwidth, int reqheight) {final BiTmapfactory.options Options = new Bitmapfactory.options (); options.injustdecodebounds = true;49 BITMAPFA Ctory.decodefile (pathName, options); options.insamplesize = calculateinsamplesize (options, Reqwidth, reqheight);         Wuyi options.injustdecodebounds = false;52 Bitmap src = bitmapfactory.decodefile (pathName, options); 53 Return Createscalebitmap (SRC, reqwidth, reqheight); 54}55}

 1/** 2 * http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 3 */4 public class Bitmaputils {5 private static int calculateinsamplesize (Bitmapfactory.options Options, 6 int reqwidth, int reqheight) { 7 Final int height = options.outheight; 8 final int width = options.outwidth; 9 int insamplesize = 1;10 if (height > Reqheight | | width > reqwidth) {one final int half Height = height/2;12 final int halfwidth = width/2;13 while (halfheight/insamplesize) > reqHeight14 && (halfwidth/insamplesize) > Reqwidth) {insamplesize *= 2      ;}17}18 return insamplesize;19}20 21//If the image is enlarged, filter determines whether it is smooth, if the image is reduced, filter has no effect 22  private static Bitmap Createscalebitmap (Bitmap src, int dstwidth,23 int dstheight) {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 27}28 return dst;29}30 31//Load picture from resources, public static Bitmap Decodesampledbitmapfromresource (RESOURC Es res,33 int resId, int reqwidth, int reqheight) {final bitmapfactory.options Options = new Bitmap Factory.options (); options.injustdecodebounds = true;36 Bitmapfactory.decoderesource (res, resId, Options ); Read pictures Long options.insamplesize = calculateinsamplesize (options, reqwidth,38 reqheight); Calculation InSampleSize39 options.injustdecodebounds = false;40 Bitmap src = bitmapfactory.decoderesource (res, R Esid, Options); Load a slightly larger thumbnail with the return createscalebitmap (SRC, reqwidth, reqheight);             Further get target size thumbnail 42}43 44//Load picture from SD card public static Bitmap DECODESAMPLEDBITMAPFROMFD (String pathname,46 int reqwidth, int reqheight) {final BiTmapfactory.options Options = new Bitmapfactory.options (); options.injustdecodebounds = true;49 BITMAPFA Ctory.decodefile (pathName, options); options.insamplesize = calculateinsamplesize (options, Reqwidth, reqheight);         Wuyi options.injustdecodebounds = false;52 Bitmap src = bitmapfactory.decodefile (pathName, options); 53 Return Createscalebitmap (SRC, reqwidth, reqheight); 54}55}

The size of the picture in 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.