Manipulate the picture itself. Try not to use Setimagebitmap, Setimageresource, Bitmapfactory.decoderesource to set a large image, because these methods are finished decode, and eventually through the Java layer CreateBitmap to complete, need to consume more memory. Therefore, instead of using the Bitmapfactory.decodestream method first to create a bitmap, and then set it to ImageView Source,decodestream the biggest secret is its direct call jni>> Nativedecodeasset () to complete the decode, eliminating the need to use the Java layer CreateBitmap, thus saving the Java layer of space. If the config parameter is added at the time of reading, it can more effectively reduce the loaded memory, thus more effectively prevent throwing memory exception. In addition, Decodestream directly take the picture to read the bytecode, not according to the machine's various resolutions to automatically adapt, after using the Decodestream, you need to configure the corresponding image resources in hdpi and mdpi,ldpi, Otherwise the same size (number of pixels) is the same on different resolution machines, and the displayed size is wrong.
The code is as follows:
Inputstreamis=this.getresources (). Openrawresource (R.DRAWABLE.PIC1);
Bitmapfactory.optionsoptions=newbitmapfactory.options ();
Options.injustdecodebounds=false;
Options.insamplesize=10;//width,hight is set to the original very
Bitmapbtp=bitmapfactory.decodestream (is,null,options);
The code is as follows:
if (!bmp.isrecycle ()) {
Bmp.recycle ()//Recover the memory occupied by the picture
System.GC ()//Alarm system timely recovery
}
The code is as follows:
/**
* Read pictures of local resources in the most memory-saving way
* @paramcontext
* @paramresId
* @return
*/
Publicstaticbitmapreadbitmap (CONTEXTCONTEXT,INTRESID) {
Bitmapfactory.optionsopt=newbitmapfactory.options ();
opt.inpreferredconfig=bitmap.config.rgb_565;
Opt.inpurgeable=true;
Opt.ininputshareable=true;
Get a picture of a resource
Inputstreamis=context.getresources (). Openrawresource (ResId);
Returnbitmapfactory.decodestream (is,null,opt);
}
The value in option refers to the scale at which the picture is scaled, and the recommended value in the SDK is 2 of the exponential value, and the larger the value, the more the picture is not clear. The length and width are only 1/2 of the original picture. The image size is reduced, and the memory used naturally becomes smaller. The disadvantage of this is that the picture quality is poor, the larger the value of the insamplesize, the picture quality is worse. Scaling pictures on different phones may be different depending on the algorithm used by each handset maker to scale pictures. I have encountered the Moto mobile phone scale after the quality can be accepted, Samsung phones on the same scale, the quality is much worse.
There are four types of Android, namely:
Alpha_8: Consumes 1byte of memory per pixel
argb_4444: Consumes 2byte of memory per pixel
argb_8888: Consumes 4byte of memory per pixel
rgb_565: Consumes 2byte of memory per pixel
Android default color mode for argb_8888, this color mode color is the most delicate, showing the highest quality. But the same, the memory is also the largest.
The above code is to read the 1.png in argb_4444 mode. Memory reduction is not as obvious as the first method, but for most pictures, there is no difference between the argb_8888 pattern. However, when you read a picture with a gradient effect, a color bar may appear. In addition, it will affect the effect of the image processing.
Optimizes heap memory allocation for Dalvik virtual machines. For the Android platform, the DALVIKJAVAVM used by its hosting layer can be optimized for processing from the present performance, such as we may consider manual interference with GC processing in the development of some large-scale games or resource-consuming applications. Using the Settargetheaputilization method provided by the Dalvik.system.VMRuntime class can enhance the processing efficiency of the program heap memory. How to use:
The code is as follows:
privatefinalstaticfloattarget_heap_utilization=0.75f;
Vmruntime.getruntime (). Settargetheaputilization (target_heap_utilization);
Can.
There is also the ability to define the size of heap memory.
The code is as follows:
privatefinalstaticintcwj_heap_size=6*1024*1024; Vmruntime.getruntime (). Setminimumheapsize (cwj_heap_size);//Set minimum HEAP memory to 6MB size
Call the Recycle () method of the picture:
This is not really a way to actually reduce the image memory. The main purpose is to mark the image object so that the local data of the image object is easily recycled. The local data of a picture object occupies the largest amount of memory and is calculated separately from the memory in the Java portion of the program. So often appear javaheap enough to use, while the picture takes place outofmemoryerror situation. Calling this method when the picture is not in use can effectively reduce the peak value of local data in the image, thus reducing the probability of outofmemoryerror. However, a picture object called recycle () is in an "obsolete" state, which causes a program error when invoked. Therefore, it is not recommended to use this method if it is not guaranteed that the picture object will never be called again. In particular, it is important to note that picture objects that have been assigned to the control using the Setimagebitmap (bitmapimg) method may be called by the System class library, causing a program error.
How to change the color mode of a picture enlarged with the matrix object:
While zooming in on a picture with a matrix object will certainly consume more memory, but sometimes you have to. The enlarged image uses the argb_8888 color mode, even if the original picture is argb_4444 color mode, and there is no way to specify the color mode directly when zoomed in. You can change the picture color mode in the following ways.
The code is as follows
The code is as follows:
Matrixmatrix=newmatrix ();
floatnewwidth=200;//the width of the image after enlargement
floatnewheight=300;//the length of the image after enlargement
Matrix.postscale (Newwidth/img.getwidth (), Newheight/img.getheight ());
Bitmapimg1=bitmap.createbitmap (Img,0,0,img.getwidth (), Img.getheight (), matrix,true);//Get enlarged picture
Img2=img1.copy (Bitmap.config.argb_4444,false);//Get a picture of argb_4444 color mode
Img=null;
Img1=null;
This creates an additional picture object IMG1 than the original image. But the system will automatically recycle IMG1, so the actual memory is still reduced.
Image optimization for Android memory optimization