From design to implementation, step-by-step teaching you to implement android-universal-imageloader-helper classes

Source: Internet
Author: User

Through the previous few posts. We analyzed the AUI cache, the tool class, the display and loading of the code. Today, we continue to study the source code of AUI, the core Auxiliary tool class in learning.

I hope you can learn something in it, ha.

Download

To download a picture, we imagine what we need: first we have to set up a supported protocol. There is a link to download. Download the image by the corresponding download link. In order to get the input stream of the picture, the rest is given to the image Display/loading class processing.

Then we can design the following interfaces:

 Public  interface imagedownloader {InputStream GetStream (String Imageuri, Object Extra)throwsIOException; Public enumScheme {HTTP ("http"), HTTPS ("https"), FILE ("File"), CONTENT ("Content"), ASSETS ("Assets"), drawable ("Drawable"), UNKNOWN ("");PrivateString scheme;PrivateString uriprefix; Scheme (String scheme) { This. scheme = scheme; Uriprefix = scheme +"://"; } Public StaticSchemeOfuri(String URI) {if(URI! =NULL) { for(Scheme s:values ()) {if(S.belongsto (URI)) {returnS }                }            }returnUNKNOWN; }Private Boolean Belongsto(String URI) {returnUri.tolowercase (locale.us). StartsWith (Uriprefix); } PublicStringWrap(String Path) {returnUriprefix + path; } PublicStringCrop(String URI) {if(!belongsto (URI)) {Throw NewIllegalArgumentException (String.Format ("URI [%1 $ s] doesn ' t has expected scheme [%2$s]", URI, Scheme)); }returnUri.substring (Uriprefix.length ()); }    }}

See here people may be puzzled, we are not design interface, why to do an enumeration type, and still in the inside to engage in so many messy things ... In fact. Suppose you have seen "thinking in Java" and you will know. In Java, an enum is actually a class. The enum class inherits the Java.lang.Enum class by default at compile time because of the enum definition. The enumeration class is itself actively added to the final keyword decoration. This also causes the enumeration class to not be inherited. In a more detailed explanation, we are able to do Google Kazakhstan by ourselves.

So why introduce this enumeration class in Imagedownloader? We'd better take a look at what's in the enumeration class first. In the enumeration class Scheme, there are four main methods, and these four methods are used to handle the URI, such as: Cut URI, add URI prefix, infer URI. In other words, the abstract function of Scheme is to modify the Uri.

The result of the Scheme's modification to the Uri will be given to imagedownloader complete download operation.

In other words, the abstraction of Scheme is actually inconsistent with the abstraction of Imagedownloader (Imagedownloader abstraction is the download image, and the Uri required to download the image requires the ability to be imagedownloader used, And the download is in fact unimportant), in order to reduce the coupling degree of the class, we implemented the Scheme class within the Imagedownloader.

Then one might ask, so do we create a different class? As far as I can see, it must be able. Since Scheme is also essentially a class, we create a new class. Declared as the final class. Add the corresponding static constants, methods. In fact the effect is the same.

So why did the writer do it? A friend who has seen "effective Java" may know that the best way to implement a singleton is to use an enum and explain it in detail. I'm not here to say more.

So the author is actually defining a singleton inside the interface.

What's an efficient-to-implement a singleton pattern in Java?

Baseimagedownloader implementation of the Imagedownloader interface, and according to our needs to achieve the corresponding picture download details, detailed no good explanation, we can read the source code ha ~

Listener

In the AUI, actually need to use the Listener is not many, after all, the picture loading is just a very small function module. So what exactly does the AUI include Listener?

    • Image loading listener: Monitoring the start, end, failure, and cancellation of image loading
    • Picture loading Progress Listener: Monitor the progress of picture loading
    • Picture loading scrolling Listener: When the picture is loading in progress, if scrolling occurs, stop loading, in other words, just load the picture that is displayed on the current screen. Pictures of the picture scrolling process are not loaded.

Someone might think it's very strange. Why image loading listener and picture loading progress listener should be implemented separately. Actually, I don't understand. I hope someone can give me an explanation ...

Assist

There are several classes in the assist we have mentioned in the previous blog post, I will no longer this repeatedly pull.

The simpler class I will also take a stroke. I hope you understand ha.

Deque

There are some double-ended queues in this area. such as Linkedblockingdeque, Lifolinkedblockingdeque. The corresponding knowledge of the double-ended queue. and detailed implementation I believe I don't have to be here nonsense, after all, the data structure of the course will talk about this knowledge point, which is also a major, must master data structures.

So what do we need to know here? That is: Why the dual-ended queue as the AUI data structure, the dual-end queues compared to other data structures in this scenario has any advantages.

We'd better take a look at where Deque is being used, and search for discoveries in the AUI library. Defaultconfigurationfactory called the Deque.

So what exactly is Defaultconfigurationfactory?

From the name of the class and the internal method name we can know. Defaultconfigurationfactory is AUI default configuration factory class, if the developer does not define the corresponding configuration options, AUI will use the default options set by this class, complete the corresponding loading, downloading, caching and so on ...

It's best to look at the following code snippet:

 public  static  Executor createexecutor  (int  ThreadPoolSize, int  threadpriority, Queueprocessingtype tasksprocessingtype) {boolean  LIFO = Tasksprocessingtype = Queueprocessingtype.lifo; Blockingqueue<runnable> taskqueue = LIFO? new  Lifolinkedblockingdeque<runnable> (): new  linkedblockingqueue<runnable > (); return  new  Threadpoolexecutor (ThreadPoolSize, ThreadPoolSize, 0  L, Timeunit.milliseconds, Taskqueue, Createthreadfactory (threadpriority,  "uil-pool-" )); }

In this piece of code. We get a thread pool and use Lifolinkedblockingdeque as the processing queue for threads. In other words, in the AUI library. Double-ended queue this data structure is used to complete the AUI thread processing. So why choose a double-ended queue, and why choose Lifolinkedblockingdeque as the default option?

The double-ended queue is selected. Because Threadpoolexecutor is using the producer-consumer model, using Blockingqueue as the task processing queue within the Threadpoolexecutor class satisfies the requirements of the producer-consumer model for the task storage data structure. and in our assist. Lifolinkedblockingdeque is a subclass of Linkedblockingdeque. And Linkedblockingdeque implements the Blockingdeque interface, and Blockingdeque interface inherits from Blockingqueue.

Others

The rest of the helper classes in assist I think are pretty simple, all of which are simplified for some basic API calls. Everyone see the source code can understand the ~

From design to implementation, step-by-step teaching you to implement android-universal-imageloader-helper classes

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.