Android asynchronous loading full parsing for big image processing

Source: Internet
Author: User

Android asynchronous loading full parsing for big image processing
The processing of images is an important part of asynchronous loading of full resolution big Image Processing for Android. This is why we used asynchronous loading of images for demonstration. On the one hand, if the image processing is not good, it will occupy a lot of memory, and it is easy to OOM. On the other hand, the image is larger than the text, and the loading is slow. Therefore, after explaining how to perform multi-thread and AsyncTask multi-thread loading, We will pause the subsequent learning to optimize asynchronous image processing.
Why does Image Processing need to be processed? This is a very direct problem. Whether you take a cell phone, camera, slr, or something, it has a certain size, but on different terminals, the terminal also has a different size, for example, a super high please no code big picture, 10 m size, looks pretty nice in the web page, Full HD, the pores are clearly visible. If the same picture is placed on a 4.7-inch mobile phone, of course, it is still a big picture without a code in high definition. However, this picture is 10 M, which may be nothing on the computer, but on the mobile phone, the image is already very big, and you are desperately watching it on your mobile phone, that is, even if the resolution is reduced by half, you still look similar. This is like the so-called Retina screen, 2 k screen, 4 K screen, in fact, has basically reached the limit of visual analysis, under normal circumstances, the difference is not big. However, although you don't know much about it, the difference is very big for the system. The memory of the mobile phone should be used for every minute, just like using your private house money. Therefore, when downloading high-resolution images, we can compress the images. Although there is no big difference in the display, it helps the system save a lot of private money.
BitmapFactory-inSampleSizeBitmapFactory is an image resolution method provided by Android. through some of its static methods, we can parse the image, for example, parsing from a file -- decodeFile; parse from the resource -- decodeResource; parse from the Network -- decodeStream and so on. When we download images from the network, we can see whether we need to compress the images. How can we achieve this before the system does not load the images to the memory, what about getting the image size and other parameters? It seems very contradictory, but the system provides us with a simple solution. BitmapFactory provides the BitmapFactory. Options parameter. This parameter has an inJustDecodeBounds attribute. When this attribute is true, we can disable the system from loading images to the memory, !!! At this time, the image width and height, type and other attributes in the Options parameter have been assigned a value, so that we can implement the empty glove White Wolf, oh, no, is to obtain the image attributes without using the memory.

BitmapFactory. options options = new BitmapFactory. options (); options. inJustDecodeBounds = true; BitmapFactory. decodeResource (getResources (), R. id. myimage, options); // get the attribute value int imageHeight = options. outHeight; int imageWidth = options. outWidth; String imageType = options. outMimeType;

After obtaining the image parameters, we can process the image accordingly. For example, the ImageView of the image is only 200X200 pixels, our images are 800X800 pixels. How can we use a large image in such a small ImageView? Memory is wasted. OK, then we will compress the image. The Options parameter provides the inSampleSize attribute, which can be used to set the scaling ratio of the image, for example, if you set inSampleSize to 5 for a 1000X1000 pixel image, the image is scaled to 1/5, that is, 200X200. OK. Next we will use this method to optimize the image. First, we need to create a method to obtain an appropriate inSampleSize:
/*** Get the appropriate inSampleSize * @ param options * @ param targetWidth expect Width * @ param targetHeight expect Height * @ return */public static int getInSampleSize (BitmapFactory. options options, int targetWidth, int targetHeight) {// the height and width of the original image final int height = options. outHeight; final int width = options. outWidth; int inSampleSize = 1; if (height> targetHeight | width> targetWidth) {// calculate the final ratio of the actual width to the target width. Int heightRate = Math. round (float) height/(float) targetHeight); final int widthRate = Math. round (float) width/(float) targetWidth); inSampleSize = heightRate <widthRate? HeightRate: widthRate;} return inSampleSize ;}

The method is very simple, that is, to obtain the scaling ratio through the expected length and width. Next we will create a method to obtain the scaled image. Here we will only create a method to retrieve the image from the resource file for Demonstration:
/*** Use targetWidth and targetHeight to obtain the appropriate inSampleSize * and use inSampleSize to zoom in and out an image of the proper size * @ param res getResources () * @ param resId id * @ param targetWidth * @ param targetHeight * @ return */public static Bitmap decodeSuitableBitmap (Resources res, int resId, int targetWidth, int targetHeight) {// empty glove White Wolf final BitmapFactory. options options = new BitmapFactory. options (); options. inJustDecodeBounds = true; BitmapFactory. decodeResource (res, resId, options); // calculate the appropriate inSampleSize options. inSampleSize = getInSampleSize (options, targetWidth, targetHeight); // load it To the memory options. inJustDecodeBounds = false; return BitmapFactory. decodeResource (res, resId, options );}

By calling decodeSuitableBitmap, We can compress images very easily.


 




Related Article

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.