Foreword: "Ann to Guangsha tens of thousands, the big scholars all the world Smile Solutions"--du fu. In the park to live in the friends are likely to encounter the problem of renting a house (local tyrants Please ignore), find a house is really definitely work, also time consuming, occupy my valuable blog time, no way, who let we have no money also want to live better, hard to earn money!!! The above to complain, now get to the point. The previous blog "Bitmap The memory occupancy calculation and loading considerations", wrote bitmap basic knowledge and the use of bitmap need to know the considerations, this blog I will write in the Android app bitmap created and loaded. 1, bitmapfactory use: When it comes to the loading of pictures must say bitmapfactory, look at the name to know his role, is a production bitmap factory, is its some factory method: You can see that bitmapfactory can use arrays that store bitmap data, bitmap resources Id,bitmap files, and so on as data sources to create bitmap objects, depending on which data source is available in your program. Each of these methods provides two methods for each data source, here you need to pay attention to the Bitmapfacotry.options parameter, it is the inner class of bitmapfactory, there are some member variable meaning need to remember, the following is said. 2, bitmapfacotry.options the injustdecodebounds parameter use: In order to save memory, in many cases the original image will be scaled processing, according to the size of the control to deal with the corresponding size of the picture, At this point, you use Bitmapfactory to create the bitmap, and in many cases you will use the following code:
Bitmapfactory.options Options = new Bitmapfactory.options (); Options.injustdecodebounds =true; Bitmapfactory.decoderesource (Getresources (), r.id.myimage, options); int imageheight = Options.outheight;int ImageWidth = Options.outwidth; String imageType = Options.outmimetype;
Notice the injustdecodebounds parameter of the options.injustdecodebounds =true above, in order to avoid the inaccuracy of my translation I'm going to put it out here first. Google's original text: If set to True, the Decoder would return null (no bitmap), but the ... fields would still be set, allowing the caller to query the bitmap wit Hout has to allocate the memory for its pixels. In my words, the memory is not allocated to this bitmap pixel area in decode, except for the other information bitmap the difference. This makes a lot of sense, you have not consumed the memory and get the information of the picture, for your next picture processing to provide help. 3, bitmapfacotry.options insamplesize parameters using: Previous step you have obtained the original size of the picture, the next step is to zoom the original image to the size you want, Can be set by the Insamplesize parameter, Google's original explanation is: If set to a value > 1, requests the decoder to subsample the original image, Returni ng a smaller image to save memory. The sample size is the number of pixels in either dimension this correspond to a single pixel in the decoded bitmap. For example, insamplesize = = 4 Returns an image which is the width/height of the original, and 1/16 the number of pixel S. Any value <= 1 is treated the same as 1. Note:the decoder would try to fulfill this request, but the resulting bitmap mAy had different dimensions that precisely what had been requested. Also, powers of 2 is often faster/easier for the decoder to honor. (Whether you do not read English documents I still want to paste the original Google, my English is bad, translation is not necessarily accurate), probably mean this Parameters can adjust the memory you need in the decode original image, a bit like the sampling rate, will throw away some pixels, the value is greater than 1 number, the power of 2 is more advantageous to the operation. For example: when insamplesize = = 4 Returns a size (length and width) is the original size 1/4, the pixel is the original 1/16 picture. How is this value calculated?
public static int calculateinsamplesize ( bitmapfactory.options options,int reqwidth,int reqheight) { //Raw Height and width of image finalint height = options.outheight; Finalint width = options.outwidth; int insamplesize =1; if (Height > Reqheight | | width > reqwidth) { Finalint halfheight = HEIGHT/2; Finalint halfwidth = WIDTH/2; Calculate the largest insamplesize value is a power of 2 and keeps both //height and width larger than the RE quested height and width. while ((halfheight/insamplesize) > Reqheight && (halfwidth/insamplesize) > Reqwidth) { Insamplesize *=2; } } return insamplesize;}
In the decode when the first set Options.injustdecodebounds =true, get to the picture parameter and then set to False, this is the skill of decode, the following is the complete code, can be used as a tool method:
public static Bitmap Decodesampledbitmapfromresource (Resources res,int resId, int reqwidth,int reqheight) { // First decode with injustdecodebounds=true to check dimensions finalbitmapfactory.options Options = Newbitmapfactory.options (); Options.injustdecodebounds =true; Bitmapfactory.decoderesource (res, resId, options); Calculate insamplesize options.insamplesize = calculateinsamplesize (options, Reqwidth, reqheight); Decode bitmap with Insamplesize set options.injustdecodebounds =false; Returnbitmapfactory.decoderesource (res, resId, options);}
The above method from the Google website, no need to make changes, this is the programmer's take doctrine bar, the key is to know why so write. Here is a method that I wrote myself to use directly as a tool.
/** * Compress the picture, mainly to solve the problem that the control displays too large a picture to occupy memory, and generally compressed image size should be similar to the size of the control used to show it. * * @param Context context * @param resId picture Resource ID * @param reqwidth expected compression width * @param reqheight Desired Compressed Height * @r Eturn Compressed picture */public static Bitmap Compressbitmapfromresourse (context context, int resId, int reqwidth, int reqhe ight) {final bitmapfactory.options Options = new Bitmapfactory.options (); /* * When parsing for the first time, Injustdecodebounds is set to TRUE, * Disables allocating memory for bitmap, although the bitmap return value is empty, the picture size can be obtained */OPTIONS.I Njustdecodebounds = true; Bitmapfactory.decoderesource (Context.getresources (), resId, options); Final int height = options.outheight; Final int width = options.outwidth; int insamplesize = 1; if (Height > Reqheight | | width > reqwidth) {final int heightratio = Math.Round (float) height/(float) Reqheight); Final int widthRatio = Math.Round ((float) width/(float) reqwidth); Insamplesize = HeightRatio < WidthRatio? Heightratio:widthratio; } options.insamplesize = Insamplesize; Parse the picture again using the computed insamplesize value options.injustdecodebounds = false; Return Bitmapfactory.decoderesource (Context.getresources (), resId, options); }
The above is the bitmap in the Android loaded into the memory of some of the small tricks, we are not in the future can be very good application, to avoid the loading of images caused by the problem of Oom? If you have a better way to give me a message or add my public number:Coder_online。 We all study together, make progress together, making money no longer worrying about the house. You can easily scan the following QR code to add:
Android Advanced Programming Summary: Bitmap memory optimization