Handling of bitmap in Android technology 6:android

Source: Internet
Author: User

Many Android applications run slowly, are not fluent, and so affect user experience issues, which is partly related to the picture. When Android applications need to load larger photos, they are often slow to react, even due to oom, which causes the system to collapse. The following will provide a workable solution.

1. Get the picture's width and Height properties

1 bitmapfactory.options options=new  bitmapfactory.options (); 2 options.injustdecodebounds=true; 3 bitmapfactory.decodefile (filepath, options);

Injustdecodebounds is true; The bitmap returned by DecodeFile is null, and the picture is not loaded into memory, but the height and width of the file and other properties can be obtained.

2. Set the Insamplesize value

The Insamplesize value is one of the options properties, mainly in the resolution of bitmap when the length and width of pixels to the original 1/insamplesize, the corresponding occupied space reduced to the original 1/(insamplesize^2). All insamplesize values are the nearest power of 2, and the default value is 1, which is not scaled.

1     //Get Insamplesize2     Private Static intGetinsamplesize (Bitmapfactory.options Options,intWidthintheight) {3         //get the width and height of the original4         intMwidth=Options.outwidth;5         intmheight=Options.outheight;6         //The initial insamplesize=1 is not scaled if n (power of 2) and the 1/n are reduced to the original 1/n^27         intInsamplesize=1;8         if(Mheight>height | | mwidth>width) {9             intHwidth=mwidth/2;Ten             intHheight=mheight/2; One              while(hwidth/insamplesize) >width && (hheight/insamplesize) >height) { AInsamplesize=insamplesize*2; -             } -         }         the         returninsamplesize; -}

3. Parse out bitmap according to Insamplesize

1 // set Injustdecodebounds to False before decoding gets bitmap; 2 options.injustdecodebounds=false; 3 Bitmap bitmap=bitmapfactory.decodefile (filepath, options);

4. Specify bitmap width and height according to the ImageView control

1 true);  

5. Setting the ImageView Property

1 image=(ImageView) Findviewbyid (r.id.image); 2 image.setimagebitmap (bitmaptools.getsamplebitmap (path, 100, 100));

The above is the basic steps for picture processing, compress the picture to a certain scale, reduce the memory. This process is very effective when designing a large number of images in Android.

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.