Android load big picture into memory

Source: Internet
Author: User

Load large picture to memory step: Step One:

Obtain the width and height data of the product screen; [HD1]

Step Two:

Get the width height of the picture; [HD2]

Step Three:

Calculate the width and height ratio of screen and picture, and scale processing according to the biggest difference; [HD3]

Step Four:

Zoom processing of the image; [HD4]

[HD1] // get the form size of the phone's screen

WindowManager WindowManager = (windowmanager) getsystemservice (window_service);

Display display = Windowmanager.getdefaultdisplay ();

windowheight = display. GetHeight();

windowwidth = display. GetWidth();

[HD2] // class for picture resolution configuration

Options options = new options ();

// do not really do the picture parsing , just get the header information to the picture : width X height date, etc.

Options. Injustdecodebounds = true ;

// set the target file for the image resolution class

bitmapfactory. DecodeFile ("/sdcard/x.jpg", options);

// get the picture's height X width

int imageheight = options. Outheight ;

int imagewidth = options. Outwidth ;

System. out . println (" high of the picture :" + imageheight + " width of the picture :" + imagewidth);

[HD3] // Use the width height of the picture and the width of the screen to calculate , figure out the proportions

int ScaleX = imagewidth/windowwidth;

int ScaleY = imageheight/windowheight;

// Default scale

int scale = 1;

if (ScaleX > ScaleY & ScaleY >= 1) {

scale = ScaleX;

       }

if (ScaleY > ScaleX & ScaleX >= 1) {

scale = ScaleY;

       }

[HD4] // Set image parser to really parse the picture

Options. Injustdecodebounds = false ;

// Set the resolution's sample rate , Set the value to scale 1/scale

Options. insamplesize = scale;

// get the picture bitmap again and set the sample rate

Bitmap Bitmap = bitmapfactory. DecodeFile ("/sdcard/x.jpg", options);

iv.setimagebitmap (bitmap);

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.