From design to implementation, step by step to teach you to achieve android-universal-imageloader-decoding and display

Source: Internet
Author: User

Reproduced please indicate the source, this article from: Chaossss's Blog

Android-universal-imageloader Github Address

In the last blog post I gave you the analysis of the Android-universal-imageloader cache function design and implementation, I hope you can learn in the inside a lost things ha. Today, I will continue to explain the AUI core class image decoding and display functions, if you have not read the last blog post, you can poke me in to see ha, nonsense not much to say, the following into the topic:

Image decoding

Before considering a specific implementation, let's think about what the role of a decoder is. I believe that everyone can blurt out the answer: decoding. Yes, the core function of the decoder is decoding, in other words, when we design the Decoder class, we should follow a single principle of responsibility:

publicinterface ImageDecoder {    throws IOException;}

Small partners See the Decode () method in the Imagedecodinginfo parameter may be very confused, what is this? We're not just decoding pictures. Let's not worry, we'll start by simulating a whole picture decoding process:

To decode the image, we have to get the "source" of the image, the image may be from the network (then we have to call the download module to download), the image may be from the local file (then we have to read through the IO stream). After obtaining the source of the image, we will start decoding, we do not have to decode only one solution, sometimes it can be displayed on the line, and sometimes to the picture HD does not show it, or we need to zoom/enlarge the picture, or even need to customize the length and width of the picture, This means that we need a decoding helper class to store the information needed for decoding so that the decoding operation can be completed.

So you should be able to understand the role of imagedecodinginfo now, right? Let's take a look at its source code:

 Public  class imagedecodinginfo {    Private FinalString ImageKey;Private FinalString Imageuri;Private FinalString Originalimageuri;Private FinalImageSize targetsize;Private FinalImagescaletype Imagescaletype;Private FinalViewscaletype Viewscaletype;Private FinalImagedownloader Downloader;Private FinalObject Extrafordownloader;Private Final BooleanConsiderexifparams;Private FinalOptions decodingoptions; Public Imagedecodinginfo(String ImageKey, String Imageuri, String Originalimageuri, ImageSize targetsize, Viewscaletype Viewscaletype, Imagedownloader Downloader, displayimageoptions displayoptions) { This. ImageKey = ImageKey; This. Imageuri = Imageuri; This. Originalimageuri = Originalimageuri; This. targetsize = targetsize; This. Imagescaletype = Displayoptions.getimagescaletype (); This. Viewscaletype = Viewscaletype; This. Downloader = Downloader; This. Extrafordownloader = Displayoptions.getextrafordownloader ();        Considerexifparams = Displayoptions.isconsiderexifparams (); Decodingoptions =NewOptions ();    Copyoptions (Displayoptions.getdecodingoptions (), decodingoptions); }Private void copyoptions(Options srcoptions, Options Destoptions)        {destoptions.indensity = srcoptions.indensity;        Destoptions.indither = Srcoptions.indither;        destoptions.ininputshareable = srcoptions.ininputshareable;        Destoptions.injustdecodebounds = Srcoptions.injustdecodebounds;        Destoptions.inpreferredconfig = Srcoptions.inpreferredconfig;        destoptions.inpurgeable = srcoptions.inpurgeable;        Destoptions.insamplesize = srcoptions.insamplesize;        destoptions.inscaled = srcoptions.inscaled;        destoptions.inscreendensity = srcoptions.inscreendensity;        destoptions.intargetdensity = srcoptions.intargetdensity; Destoptions.intempstorage = Srcoptions.intempstorage;if(Build.VERSION.SDK_INT >=Ten) CopyOptions10 (srcoptions, destoptions);if(Build.VERSION.SDK_INT >= One) CopyOptions11 (srcoptions, destoptions); }//get method omitted............@TargetApi(Ten)Private void CopyOptions10(Options srcoptions, Options Destoptions)    {destoptions.inpreferqualityoverspeed = Srcoptions.inpreferqualityoverspeed; }@TargetApi( One)Private void COPYOPTIONS11(Options srcoptions, Options Destoptions)        {destoptions.inbitmap = Srcoptions.inbitmap;    destoptions.inmutable = srcoptions.inmutable; }}
Baseimagedecoder

After we have obtained the base class imagedecoder of the image decoder, we have to complete our implementation. So now we have to think about the Imagedecoder-based abstraction: the Decode () method, which we derive from the implementation details of the Imagedecoder. In order to complete the image decoding operation, we will follow the following steps:

Get picture input stream and determine the actual size of the picture, whether it needs to be cut and whether it needs to be rotated, get pictures, picture information, and decode settings to get the picture that meets the requirements.

Since the realization of the idea has been decided, the concrete implementation is certainly not difficult to pull:

    @Override     PublicBitmapDecode(Imagedecodinginfo decodinginfo)throwsIOException {Bitmap decodedbitmap;        Imagefileinfo Imageinfo; InputStream ImageStream = Getimagestream (Decodinginfo);if(ImageStream = =NULL) {L.E (Error_no_image_stream, Decodinginfo.getimagekey ());return NULL; }Try{imageinfo = Defineimagesizeandrotation (ImageStream, decodinginfo);            ImageStream = Resetstream (ImageStream, decodinginfo);            Options decodingoptions = preparedecodingoptions (imageinfo.imagesize, decodinginfo); Decodedbitmap = Bitmapfactory.decodestream (ImageStream,NULL, decodingoptions); }finally{ioutils.closesilently (ImageStream); }if(Decodedbitmap = =NULL) {L.E (Error_cant_decode_image, Decodinginfo.getimagekey ()); }Else{Decodedbitmap = Considerexactscaleandorientatiton (Decodedbitmap, Decodinginfo, ImageInfo.exif.rotation,        ImageInfo.exif.flipHorizontal); }returnDecodedbitmap; }

Code I will not be all out, we can according to the idea of the way down here into the corresponding method to see the line. The Exifinfo and imagefileinfo two classes are just the auxiliary classes required by the decoding process, providing some information needed to process the picture, such as: the size of the picture file, whether the picture needs to be cut, rotated:

protected Static  class exifinfo {     Public Final intRotation Public Final BooleanFlipHorizontal;protected Exifinfo() { This. rotation =0; This. FlipHorizontal =false; }protected Exifinfo(intRotationBooleanFlipHorizontal) { This. rotation = rotation; This. fliphorizontal = FlipHorizontal; }}protected Static  class imagefileinfo {     Public FinalImageSize ImageSize; Public FinalExifinfo Exif;protected Imagefileinfo(ImageSize ImageSize, Exifinfo Exif) { This. imageSize = imageSize; This. EXIF = EXIF; }}
Picture Display

In fact, we will find that the image display and image decoding two functions in the implementation will be very similar: abstract single (picture display/image decoding), the implementation as long as the corresponding requirements based on the abstract implementation of the details.

publicinterface BitmapDisplayer {    void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom);}

We may need to display a circle/rectangle/Fillet Picture When we display the picture, or set the corresponding picture animation, etc... In fact, we just need to base on the corresponding picture shape, animation settings/implementation of the corresponding drawable and Animation on it. I don't think there's anything to say here ... But here we introduce an interface imageaware, which I think deserves our attention:

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

With this interface we get information about the view of the image displayed in the image display class, as well as the image displayed by the view, the shape of the image, and the ID of the display image. Some may say, however, there is no egg to use ... But you think about it, is that really the case? If we don't have this interface, how do we make the picture appear as a circle directly inside the display class, or do we add the corresponding animation? Be aware that the final implementation of these effects is applied to the View above.

Of course, showing round pictures and animations can certainly be done, such as customizing the view, or passing the view as a parameter to display, processing each view of the displayed image in the displays class, or getting the properties of the view. The practice in AUI is also worthy of reference because it strips this piece of logic and avoids coupling.

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

From design to implementation, step by step to teach you to achieve android-universal-imageloader-decoding and display

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.