Android asynchronous loading full resolution large map processing

Source: Internet
Author: User

Android asynchronous loading full resolution large map processingOne of the most important parts of asynchronous loading is the processing of images, which is why we have previously used asynchronous loading of images for illustrative purposes. On the one hand, because the image processing is not good words will be very memory, and easy to oom, on the other hand, the image is larger than the text, loading relatively slow. So, after explaining how to do multi-threaded, asynctask preempted, pause the next learning, to the image of the asynchronous processing to do some optimization work.
Why image ProcessingWhy to deal with the image, this is a very direct problem, a picture, whether you take a cell phone, camera, SLR or something to shoot out, it has a certain size, but in different terminals, the terminal also has a different size, such as an ultra-high please uncensored big picture, 10M size, in the web to look very cool, Full HD, Pores can be seen clearly. The same picture, If placed on a 4.7-inch phone, of course, is also a high-definition uncensored big picture, but this picture 10M, on the computer may not be anything, but on the phone, is already very big, and this picture on the phone, you look desperately, that is, even if the resolution is reduced by half, you still look similar. This is like the so-called Retina screen, 2k screen, 4k screen, in fact, has basically reached the limit of visual analysis, under normal circumstances, the difference is not big.However, although you look at the difference is not big, but for the system, the difference is very large, the phone's memory, like the use of your hidden money, every point to think twice and use. So, when we download high-resolution images, we can compress the image, although there is not much difference in the display, but help the system save a lot of money.
The insamplesize of BitmapfactorybitmapfactoryIt is an image parsing method provided by Android, and through some static methods, we can parse the image, such as parsing the--decodefile from the file, parsing the--decoderesource from the resource, parsing the--decodestream from the network, and so on.When we download the image from the network, we look at the situation, whether we need to compress the image, then how to get the image size and so on before the system does not load the image into memory? Seems very contradictory, but the system provides us with a simple solution. bitmapfactory offersThe bitmapfactory.options parameter, this parameter has a Injustdecodebounds property, when this property is true, we can prohibit the system to load the image into memory, but!!! At this time, the options parameters in the image width, type and other attributes have been assigned, so that we realized the empty glove white Wolf, oh, no, is not to use memory to get the properties of the image.
Bitmapfactory.options Options = new Bitmapfactory.options (); options.injustdecodebounds = true; Bitmapfactory.decoderesource (Getresources (), r.id.myimage, options);//Get property 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, we display the image of ImageView only 200 pixels, and our picture has a size of 800 pixels, then you put such a large picture in such a small imageview, what is the use? Wasted memory in vain. OK, so here we have to compress the image, the options parameter provides us with an attribute--insamplesize, which can set the scale of the image, such as a 1000-pixel image, set Insamplesize to 5, This means that the image is scaled to one-fifth, which is X 200. OK, here's a way to optimize the image, first, we need to create a method to get to a suitable insamplesize:
/** * Get the right insamplesize * @param options * @param targetwidth expected width * @param targetheight expected height * @return */public St atic int getinsamplesize (bitmapfactory.options Options,                                        int targetwidth, int targetheight) {    //the height and width of the original picture    final int height = options.outheight;    Final int width = options.outwidth;    int insamplesize = 1;    if (Height > Targetheight | | width > targetwidth) {        //calculates the ratio of the actual width height to the target width to the        final int heightrate = Math.Round ((fl OAT) height/(float) targetheight);        Final int widthrate = Math.Round ((float) width/(float) targetwidth);        Insamplesize = Heightrate < widthrate? heightrate:widthrate;    }    return insamplesize;}

The method is very simple, which is to get the scale by the desired length and width. Let's create a method to get the scaled image, here we just create a way to get the image from the resource file:
/** * Use Targetwidth, targetheight to get the right insamplesize * and use insamplesize to zoom to a suitable size image * @param res getresources () * @param res ID 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);    Loaded into memory    options.injustdecodebounds = false;    Return Bitmapfactory.decoderesource (res, resId, options);}

By calling Decodesuitablebitmap Such a method, we can compress the image very simply.


my github
My video mu-class network





Android asynchronous loading full resolution large map processing

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.