How Android can display the larger bitmaps_android efficiently

Source: Internet
Author: User

One, high efficient loading of large bitmaps

Pictures have a variety of shapes and sizes, and in many cases, the actual size of the picture is much larger than the image shown in the application, such as the Gallery application shown in the Android system, the actual resolution is usually much higher than the density of the cell phone device.

Considering that we are developing a limited application of memory usage, ideally, we just want to load a low-resolution version of the bitmap into memory, which generally corresponds to the size that the UI component actually needs to display. A high-resolution picture does not give us any obvious benefits, but consumes valuable memory resources and creates additional performance overhead

Second, to obtain the size and type of bitmap

The Bitmapfactory class provides us with several decoding methods (Decodebytearray (), DecodeFile (), Decoderesource (), etc) to create Bitmap from different sources, How to choose the most appropriate decode method depends on your image data source, these methods will try to apply for memory to build bitmap objects, all can easily lead to a outofmemory exception, each type of decode method has an additional signature to allow you to pass Bitmapfactory.options class to specify the decoding option, when we decoding the Injustdecodebounds property is set to True to avoid requesting memory, although a null bitmap object is returned, But we set the value of Outwidth, Outheight, and outmimetype for our incoming Bitmapfactory.options object, which allows you to know the size and type of the bitmap object prior to building it.

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; 

To avoid java.lang.OutOfMemory anomalies, you need to check bitmap size and type before decoding bitmap, unless you really know the size of decoding you want to bitmap, And this size to fit the current application memory environment

Third, load the ' reduced version ' of the bitmap to memory

Now that we know the size of the bitmap, which will help us decide whether to load the whole bitmap or load the ' shrink ' bitmap, here are some factors to consider:

1, load the entire picture expected to use how much memory

2, in consideration of other aspects of memory needs, you want to put the amount of memory to bitmap use:

3. The size of the ImageView control or other UI component that is used to display bitmap

4, the current device screen size and density

For example, it's not worth loading a 1024x768 pixel picture into memory, but it's finally displayed on a ImageView control that 128x96 pixel size

We should tell decoder that the image needs to be sampled to load a smaller bitmap into memory, setting the Insamplesize property of the Bitmapfactory.options object to True. For example, a picture with a resolution of 2048x1536 pixels, if the insamplesize is set to 4 when decode, the resulting final picture is approximately 512x384 in size, and the load is 0.75M instead of 12M when the whole sheet is loaded (assuming that the bitmap is configured as argb_8888), here's a way to compute insamplesize on a high and wide basis

public static int calculateinsamplesize ( 
   bitmapfactory.options Options, int reqwidth, int reqheight) { 
 //Raw Hei Ght and width of image 
 final int height = options.outheight; 
 Final int width = options.outwidth; 
 int insamplesize = 1; 
 
 if (Height > reqheight | | | width > reqwidth) { 
  if (width > height) { 
   insamplesize = Math.Round (float) Heig HT/(float) reqheight); 
  else { 
   insamplesize = Math.Round ((float) width/(float) reqwidth); 
  } 
 return insamplesize; 
} 

Note: The insamplesize value is a power of 2, which is faster and more efficient for decoder. However, if you want to cache resized bitmaps to memory or hard disks, it still makes sense decoding the most appropriate bitmap size, which helps save memory or save hard disk space

Here's a way to get a bitmap

public static Bitmap Decodesampledbitmapfromresource (resources res, int resid, 
  int reqwidth, int reqheight) { 
 
 // The Decode with injustdecodebounds=true to check dimensions 
 final bitmapfactory.options Options = new Bitmapfactory . 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; 
 Return Bitmapfactory.decoderesource (res, resid, options); 
 

This method makes it easy to load a bitmap in any UI element that displays size.

Mimageview.setimagebitmap ( 
 Decodesampledbitmapfromresource (Getresources (), R.id.myimage, 100, 100)); 

The techniques for acquiring bitmaps above other bitmapfactory.decode* series of decode methods are also needed.

This is the entire content of this article, I hope to learn more about Android software programming help.

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.