Android asynchronous load full resolution large map processing

Source: Internet
Author: User

Android asynchronous load full resolution large map processingAn important part of asynchronous loading is the processing of images, which is why we used to load images asynchronously earlier.

On the one hand, due to bad image processing will occupy memory, and Easyoom, and on the one hand, the image is larger than the text, loading is slower. Therefore, in the explanation of how to multi-threaded, Asynctask multi-threaded loading, the first pause to learn later. To perform some optimization work on the asynchronous processing of images.


Why image ProcessingIt is a straightforward question as to why the image should be processed. A single image. Whether you take a cell phone, camera, SLR or something to shoot it, it has a certain size, but on different terminals, the terminal also has a different size. For example, a super-high please uncensored big picture, 10M size, on the Web page is very cool. Full HD, pores can be seen clearly.

Same picture. Suppose on a 4.7-inch phone, of course, the same or 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 desperately look. That is, even if the resolution is reduced by half. You still look almost the same.

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 very big.

However, although you look at the difference is not big, but for the system. The difference is very big, the memory of the phone. Like using your stash of money, every point has to think twice.

So. When we download high-resolution images. The image can be compressed, although the display is not much different, but it helps the system to save a lot of money.


The insamplesize of BitmapfactorybitmapfactoryIs the method of parsing the image provided in Android. Through some of its static methods. We are able to parse the image. such as parsing--decodefile from a file, parsing--decoderesource from a resource. Parsing--decodestream from the network, and so on.When we download the image from the network, to see the situation, whether it is necessary to compress the image, then how to get the image size and other parameters before the system does not load the image into memory? It seems contradictory, but the system provides us with a simple solution. bitmapfactory offersThe bitmapfactory.options number of references. This parameter has a injustdecodebounds attribute. When this property is true, we are able to prevent the system from loading images into memory. But.!!

In this case, the image width, type, and other attributes in the options parameters have been assigned, so. We realized the empty glove white Wolf, oh, not correct, is to get the properties of the image without using memory.


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 we have obtained the parameters of the image, we are able to handle the image accordingly. For example, we show the image of the ImageView only a few 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 of it? Wasted memory in vain. OK, so we're going to compress the image below, and the options parameter gives us an attribute--insamplesize, which sets the scale of the image, such as a 1000-pixel image. Set Insamplesize to 5. This means that the image is scaled to one of five points. That 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 easy. is to get the scale by the desired length and width. Here we create a method to get the scaled image. To illustrate, we just create a way to get an image from a 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);    Load into memory    options.injustdecodebounds = false;    Return Bitmapfactory.decoderesource (res, resId, options);}

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


My GitHub
My video mu-class network





Android asynchronous load 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.