From design to implementation, step-by-step to implement the Android-universal-imageloader-tool class

Source: Internet
Author: User

Reproduced please indicate the source, this articlefrom: Chaossss's blog

In the previous blog post, we analyzed the function and architecture design of the Auimgloader cache module, and today we might as well begin to analyze the Auimgloader tool class to pave the future for the analysis.

In the Utils package, there are tool classes that Auimgloader may use. Where Diskcacheutils, storageutils, memonrycacheutils we analyzed the Auimgloader cache function module has been explained, today will not repeat. In fact, the classes implemented as tools in Auimgloader are more than just classes in Utils packages, such as Filenamegenerator and downloads ... No more nonsense, go to the point:

Imagesizeutils

Provides calculations with image sizes, scales

Imagesizeutils class only more than 200 lines of code, the main function is the picture length and picture scale calculation, in the class mainly deal with ImageSize objects, then ImageSize object is what?

ImageSize
 Public  class ImageSize {    Private Static Final intTo_string_max_lenght =9;Private Static FinalString SEPARATOR ="x";Private Final intWidthPrivate Final intHeight Public ImageSize(intWidthintHeight) { This. width = width; This. height = height; } Public ImageSize(intWidthintHeightintRotation) {if(Rotation% the==0) { This. width = width; This. height = height; }Else{ This. width = height; This. height = width; }    } Public int getwidth() {returnWidth } Public int getheight() {returnHeight } PublicImageSizeScaledown(intSampleSize) {return NewImageSize (Width/samplesize, height/samplesize); } PublicImageSize Scale(floatScale) {return NewImageSize ((int) (Width * scale), (int) (height * scale)); }@Override     PublicStringtoString() {return NewStringBuilder (To_string_max_lenght). Append (width). Append (SEPARATOR). Append (height). toString (); }}

SEPARATORis a delimiter used to separate the length and width of a picture

TO_STRING_MAX_LENGHTRepresents the maximum length of a string that represents a picture size, for example: 9999x9999

The construction method of ImageSize requires that we pass in the length (height) and width (width), scaledown () and scale () methods to process the image's length, to reduce/enlarge the image size. In general, ImageSize is a simple class that represents the size of a picture.

Static methods in Imagesizeutils

Now that we know what ImageSize is, let's continue the analysis of Imagesizeutils:

First get imagesizeutils The following code must be executed:

static {        intnewint[1];        0);        int maxBitmapDimension = Math.max(maxTextureSize[0], DEFAULT_MAX_BITMAP_DIMENSION);        new ImageSize(maxBitmapDimension, maxBitmapDimension);    }

The main purpose of this code is to ensure that the image size does not exceed our defined maximum size, which is 2048x2048.

Definetargetsizeforview ()

In the Definetargetsizeforview () method, if Imageaware's wid and height are greater than 0, we will return the ImageSize object with the Imageaware width as the picture size, and if less than 0, return the MaxIma The ImageSize object that is constructed from the long-width value stored in the Gesize object.

publicstaticdefineTargetSizeForView(ImageAware imageAware, ImageSize maxImageSize) {        int width = imageAware.getWidth();        if0) width = maxImageSize.getWidth();        int height = imageAware.getHeight();        if0) height = maxImageSize.getHeight();        returnnew ImageSize(width, height);    }
Imageaware

What about the Imageaware object?

publicinterface ImageAware {    int getWidth();    int getHeight();    ViewScaleType getScaleType();    View getWrappedView();    boolean isCollected();    int getId();    boolean setImageDrawable(Drawable drawable);    boolean setImageBitmap(Bitmap bitmap);}

As we can see, Imageaware is an interface, from the author's definition of it we can know that in the process of using Imageloader, Imageaware provides all the properties required for image processing/display. In addition, through its callback method we can get all the view that obtains the picture through the Imageloader and then displays the picture.

Computeimagesamplesize () method

We continue to analyze, the Computeimagesamplesize () method looks a bit long, we look at 1.1 points:

computeImageSampleSize(ImageSize srcSize, ImageSize targetSize, ViewScaleType viewScaleType,            boolean powerOf2Scale)

First, the four parameters of this method represent:

    • srcsize-Initial (image) size
    • targetsize-target (view) size of the display image
    • viewscaletype-how pictures are displayed in View (Zoom/section display, etc...) )
    • powerof2scale-whether the picture size is a multiple of 2

Inside the method, we first get the initial size and the width of the target size, and then initialize the variable scale to represent the proportions of the picture to 1, and go to a switch SELECT statement to handle the scales variable:

switch (viewScaleType) {    case FIT_INSIDE:        ……        break;    case CROP:        ……        break;}

In the SELECT statement, there are two values for fit_inside and CROP, eh, these two values we have not seen, what exactly is ... Ctrl + mouse Click inside we found that it was both Viewscaletype enumeration values:

publicenum ViewScaleType {    FIT_INSIDE,    CROP;}

From the notes, when using fit_inside, the length and width of the picture will have at least one value that is less than or equal to the length or width of the view to ensure that the picture is fully displayed in view (but may be scaled), whereas when using CROP, the length and width of the picture will be greater than or equal to the length and width of the view. Causes the picture to appear incomplete because it will be cut.

To figure out the use of these two values, we can continue to analyze the pull down:

Switch(Viewscaletype) { CaseFit_inside:if(Powerof2scale) {Final intHalfwidth = srcwidth/2;Final intHalfheight = srcheight/2; while(Halfwidth/scale) > Targetwidth | | (Halfheight/scale) > Targetheight) {// ||Scale *=2; }        }Else{scale = Math.max (Srcwidth/targetwidth, srcheight/targetheight);//Max} Break; CaseCROP:if(Powerof2scale) {Final intHalfwidth = srcwidth/2;Final intHalfheight = srcheight/2; while(Halfwidth/scale) > Targetwidth && (Halfheight/scale) > Targetheight) {//&&Scale *=2; }        }Else{scale = Math.min (Srcwidth/targetwidth, srcheight/targetheight);//min} Break;}

Because the processing in the two judgment blocks is very similar, I only take the fit_inside to analyze it:

First check the value of the Powerof2scale, if the Powerof2scale is false, indicating that the picture size is not a multiple of 2, then directly with the initial value divided by the target value, the lowest proportion of the result is the scale value.

If Powerof2scale is true, the picture size is a multiple of 2, you first get the initial size of 1/2, and then through the loop continuously let the value of scale multiply by 2, thus continuously halfwidth and halfheight, until there is a greater than the target size, The end of the loop, at which point the value of scale is the final value.

Since the display of the picture must fill the entire View, after the scale is processed by the SELECT statement, you need to check the value of scale and, if it is less than 1, set the value of scale to 1. Then execute the considermaxtexturesize () method.

Considermaxtexturesize () method

We get the scale value after the completion of the Computeimagesamplesize () method, and at the end of the method we also need to process the scale value through the Considermaxtexturesize () method, then What did the Considermaxtexturesize () method do?

Private Static int considermaxtexturesize(intSrcwidth,intSrcheight,intScaleBooleanPOWEROF2) {Final intMaxWidth = Maxbitmapsize.getwidth ();Final intMaxHeight = Maxbitmapsize.getheight (); while(Srcwidth/scale) > MaxWidth | | (Srcheight/scale) > MaxHeight) {if(POWEROF2) {Scale *=2; }Else{scale++; }        }returnScale }

It turns out that we divide the initial size with the calculated scale value to determine whether the scaled length/width exceeds the maximum picture size we define, and if it exceeds, you need to increase the scale value to increase the scale.

Computeminimagesamplesize () method is very simple does not analyze the pull
The Computeimagescale () method, in contrast to the Computeimagesamplesize () method, asks for the scale of the initial dimension from the target dimension

Ioutils, L

In fact, the methods in the Ioutils and L classes are all simple, which is to encapsulate some common operations, simplifying our actual code volume.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

From design to implementation, step-by-step to implement the Android-universal-imageloader-tool class

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.