Android Bitmap Efficient loading

Source: Internet
Author: User

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

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.