memory occupied by a picture (BITMAP) = picture length * image width * number of bytes per pixel
|
Picture format (bitmap.config ) |
a picture of 100*100 takes up memory size |
|
Alpha_8 |
Picture length * Picture width |
100*100=10000 bytes |
|
argb_4444 |
Picture length * Picture width * |
100*100*2=20000 bytes |
|
argb_8888 |
Picture length * Picture width |
100*100*4=40000 bytes |
|
rgb_565 |
Picture length * Picture width * |
100*100*2=20000 bytes |
The way to reduce image memory is only to change the format of the image, reduce the length and width, change the sample rate:
1 Public StaticBitmap Getbitmapbypath (String path,intSetWidth,intsetheight) {2 //Create an empty bitmap3Bitmap Bitmap =NULL;4 //get the Options object5Bitmapfactory.options Options =Newbitmapfactory.options ();6 //set the Injustdecodebounds property to True when the Injustdecodebounds property is true, Bitmapfactory only reads the original width height7 //does not actually load the picture8Options.injustdecodebounds =true;9 //because the Injustdecodebounds property is set to true on the top, it will not actually load the image and will only read the original width and height .Ten bitmapfactory.decodefile (path, options); One //gets the original height of the picture A intHeight =Options.outheight; - //gets the original width of the picture - intwidth =Options.outwidth; the //declares an original sample rate - intGetinsamplesize = 1; - //if the original width height is greater than the target width - if(Height>setheight | | width>setwidth) { + //take the original wide-height 2/1 - intHalfheight = HEIGHT/2; + intHalfwidth = WIDTH/2; A //calculate sample rate based on conditions at while((halfheight/getinsamplesize) >= setheight && (halfwidth/getinsamplesize) >=setwidth) { -Getinsamplesize *=2; - } - } - //The calculated sample rate is paid to options.insamplesize using the calculated sample rate -Options.insamplesize =getinsamplesize; in //set the Injustdecodebounds property to False -Options.injustdecodebounds =false; to //because the Injustdecodebounds property is set to false on the top, this will actually load the picture +Bitmap =Bitmapfactory.decodefile (path,options); - returnbitmap; the}
Android Bitmap Efficient loading