Android in-depth UI (i) image

Source: Internet
Author: User

(i) Create the type of picture bitmap

Bitmapfactory.options represents the way bitmap is stored

Alpha_8 Only alpha value, accounting for 1 bytes
argb_4444 Not recommended, ARGB each accounted for 4bits, total 2 bytes
argb_8888 Default Save method, Argb 8bits, total 4 bytes
rgb_565 No alpha value, total 2 bytes


(ii) bitmap occupied memory size

The memory used by a picture (BITMAP) in Android is mainly related to the following factors: image length, image width, number of bytes per pixel.
Memory occupied by a picture (BitMap) = picture Length * Image width * Number of bytes per pixel
Note: The image length and image width are in pixels.

For example, a picture of 400*560, argb_8888 display, takes up memory as 400*560*4=869000 bytes.

Bitmap can also be obtained by the program to occupy memory

protected int bitmapsizeof (Bitmap data) {        if (Build.VERSION.SDK_INT < build.version_codes. HONEYCOMB_MR1) {            return data.getrowbytes () * Data.getheight ();        }        else {            return data.getbytecount ();        }    }


(3) Bitmap treatment Skills

1. Do not load bitmap get picture information

Bitmapfactory.options bmoptions = new Bitmapfactory.options ();                Bmoptions.injustdecodebounds = true;                Bitmapfactory.decodefile (path, bmoptions);                int Photow = bmoptions.outwidth;                int Photoh = Bmoptions.outheight;
With the Injustdecodebounds parameter, the setting does not load the picture, can get to the picture width height

2. Image compression Processing

InputStream is = This.getresources (). Openrawresource (R.drawable.pic); Bitmapfactory.options Options = new Bitmapfactory.options (); options.injustdecodebounds = False;options.insamplesize = 2; Bitmap BTP =bitmapfactory.decodestream (is,null,options);
Read the thumbnail image under drawable named pic, with a length and width of 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.

3. Image format conversion, reduce memory consumption

Bitmapfactory.options opt = new bitmapfactory.options ();     Opt.inpreferredconfig = Bitmap.Config.RGB_565;     Opt.inpurgeable = true;     Opt.ininputshareable = true;     Get the resource picture     InputStream is = Context.getresources (). Openrawresource (resId);     Return Bitmapfactory.decodestream (is,null,opt);  
Memory consumption is reduced by half compared to the argb_8888 load mode.
4. Images do not need to continue to use instant recycle

First determine if the IF (bitmap! = null &&!bitmap.isrecycled ()) is already recycled {         //recycle and set to null        bitmap.recycle ();         bitmap = null; } System.GC ();
The construction method of the bitmap class is private, so the developer cannot directly new out a bitmap object, only through the various static methods of Bitmapfactory class to instantiate a bitmap, the generation bitmap object is eventually implemented by the JNI call method. So, after loading bitmap into memory, it contains two parts of the memory area, part of the Java part, and part C. Therefore, the recycle () method needs to be called to release the memory of part C.





Android in-depth UI (i) image

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.