Loading images into memory under Android

Source: Internet
Author: User

Android's standard default memory allocated for each application is 16M. So it is very valuable, in the creation of the application to save as much memory as possible, but when loading some large files, for example, the picture is quite memory, a 1.3M picture, the resolution is 2560x1920 (width × high) picture when loaded into the phone memory will request a 19M of memory, This is far beyond the system's own memory space, this time the application will be hung up, so we want to do the image zoom processing, the following I will take you to create a picture to zoom the application;

Applications such as the following:



Implementation of the core code:

Package Com.examp.loadpicture;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.bitmapfactory.options;import Android.os.Bundle;import Android.view.display;import Android.view.view;import Android.view.windowmanager;import Android.widget.ImageView; public class Mainactivity extends Activity {private ImageView iv;private int windowheight;private int windowwidth; @Overri deprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); iv = (ImageView) Findviewbyid (R.ID.IV);//Gets the window size of the phone's screen windowmanager WindowManager = ( WindowManager) Getsystemservice (window_service);D isplay Display = Windowmanager.getdefaultdisplay (); windowHeight = Display.getheight (); windowwidth = Display.getwidth ();} public void LoadPicture (view view) {//Bitmap factory class//Bitmap Bitmap = Bitmapfactory.decodefile ("/sdcard/x.jpg");//encode the specified file, Convert to Bitmap//Iv.setimagebitmap (bitmap);//Image resolution configuration of the class options options= new Options ();//Do not really do picture parsing, just get to the image of the header information: Width x High date options.injustdecodebounds = true;// Set the target file of 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 + "picture width:" + imagewidth);//Use the width of the picture and the width of the screen to calculate the ratio int ScaleX = Imagewidth/window Width;int ScaleY = imageheight/windowheight;//default proportional int scale = 1;IF (ScaleX > ScaleY & ScaleY >= 1) {scales = ScaleX;} if (ScaleY > ScaleX & ScaleX >= 1) {scale = ScaleY;} Set the image parser really to parse the picture Options.injustdecodebounds = false;//Set the rate of resolution, set the value is the scale 1/scaleoptions.insamplesize = scale;// Get the picture bitmap again and set the sample rate bitmap bitmap = bitmapfactory.decodefile ("/sdcard/x.jpg", options); Iv.setimagebitmap (bitmap);}}

Demo Download:

http://download.csdn.net/detail/u011936142/7448597




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.