Fresco analysis (i) ———— brief analysis

Source: Internet
Author: User

/*** This article original * but* can be reproduced to any site or app* but* reprint also to press the "Basic Law" * so* please indicate the source and author * *

Official Source Address

Fresco Official tall Introduction (1)(Note: There is a wall ahead) Fresco Official Tall introduction (2)(Note: There is a wall ahead) PrefaceAlthough the title is Fresco Analysis (a), it does not mean that there will be 2345. basic content according to the flow of development, and the general and their own analysis of the process of the library consistent, the code was a long time ago probably looked at the day, but did not do any records, this is a right when the record, a long time to see, but, drawing to hit the Wall (escape. However, the picture may not be very normative, to understand the line. a lot of pictures loaded library, mainstream volley,universal imageloader, Glide,picasso,fresco. the reason for fresco:
    1. Facebook Productions
    2. A lot of code.
Introduction:Fresco,facebook Open source image loading framework for Android apps, efficient and functional.
    1. Support Load network, local storage and resource picture;
    2. Provides level three cache (level two memory and first level internal storage);
    3. Support jpegs,pngs,gifs,webps, etc., also support progressive JPEG, excellent animation support;
    4. Picture fillet, scale, custom background, overlays, etc.;
    5. Excellent memory management;
    6. 2.3 (gingerbread) or above.
(In fact, the above is redundant ~ ~ ~) Body StartThis article involves getting pictures that may not be too prescriptive , if there is obsessive-compulsive disorder, please ignorecontent does not have a lot of code, afraid of voluminous, so can only be an approximate, cautious reading . EngineeringClone code down, the look of the project is probably this:
Briefly talk about project structure:
    • Sample module Below is an example, there are several examples of projects, such as demo, etc.;
    • Drawee module, mainly UI-related things, such as Draweeview, drawable related classes;
    • imagepipeline Module, the core of the whole project, image loading, memory, cache management, bitmap processing and so on core logic;
    • Fbcore Module, can be said to be the base class library, some tools, basic interface, file processing and so on;
    • Drawee Backends, is based on Drawee module in the implementation of some of the interface, for example, if you have used volley, you can continue to use volley as part of the picture loading frame to do loading pictures;
    • Imagepipeline Backends is also based on the implementation of certain interfaces in the Imagepipeline module, such as HTTP requests using Okhttp, which can continue to be implemented using Okhttp;
Project structure is probably the case, red is the three module that needs attention, and the others are easy jobs. Usage:Directly on the code: XML: 
< Com.facebook.drawee.view.SimpleDraweeView Android:id = "@+id/baseline_jpeg" Android:layout_width = "Match_parent" Android:layout_height = "0DP" Android:layout_weight = "1" Fresco:actualimagescaletype = "Fitcenter" />

Code:
1 uri uri = Uri.parse ("http://omitted"); // 2 imagerequest request = Imagerequestbuilder.newbuilderwithsource (URI)3 . Setprogressiverenderingenabled (true)4. Build (); 5 Draweecontroller controller = fresco.newdraweecontrollerbuilder ()6. Setimagerequest (Request)7. Build (); 8 Mprogressivejpegview.setcontroller (Controller);

The code is simple and easy to understand, but relative to other picture loading frameworks, it is relatively complex to feel such a call. Of course, you can continue to encapsulate, reducing unnecessary duplication of code. Process Analysis:The analysis process is basically the first thing I do to analyze the open source project, and once the process is over, it will probably be able to explore clearly. Then, there is the following picture, the figure is big,Mobile Party cautious Point~ ~ ~ To see the details, you can click to see the big picture or download .... (The process is selected in the demo example of the network loading picture flow, the other is about the same.) ) (Many details are omitted from the flowchart, but the main process is described.(The picture of me ....) A complete request to the response process is this, quite complicated, with patience to take a closer look (the slag map is hard to be patient too). Although complex, it is true that, in terms of implementation, extensibility and functionality are quite perfect, as Facebook itself says: Several Excellent open source libraries exist that perform these sequences- Picasso, Universal Image Loa Der, Glide, and volley, to name a few. All of these has made important contributions to Android development. We believe our new library goes further in several important ways.

Key points: Key points of analysis are mainly focused on some of the key interfaces and classes. This is done by the module. First analyzeDraweemodule, the main following three:
    1. Draweeview
    2. Draweehierarchy
    3. Draweecontroller
The relationship of these three classes is probably an MVC pattern:
    • Draweeview inherits ImageView, that is V, is responsible for showing draweehierarchy;
    • Draweehierarchy is M, can think of it is composed of multilayer drawable, each layer for the picture to provide a specific function, such as scale,fade-in,layering and so on;
    • Draweecontroller is C, which handles the core control logic. For example, make a request to pipeline (or another picture loading library), receive the corresponding event, and control the Draweehierarchy based on different events, and receive the event from Draweeview and then make an operation to cancel the network request, reclaim the resource, and so on.
The details are not discussed here, but one thing to mention is that the Draweeview settings are not setbitmap methods, and all operations are performed on Draweeview drawable, whether it is updating, fillet, etc. Then analyzeFbcore:
    1. DataSource
    2. Datasubscriber
Fbcore are basically some basic classes, these two interfaces are no exception.
    • Datasource,java inside the futures of the alternative, literally know, it represents the source of data, it and futures is different, can have a series of result, not one.
    • Datasubscriber, which corresponds to DataSource, is used to receive the results returned from the DataSource, and can also know the approximate effect from the literal meaning
DataSource any state changes, Datasubscriber should receive the corresponding event and then process it. The simple understanding is that DataSource represents the data processing flow and results, datasubscriber like callback, receiving events continuously. These are both interfaces and there are many different implementations that are not discussed here. The two main implementation classes are Abstractdatasource and Basedatasubcriber, and the other implementations are basically inheriting the two abstract classes. Finally,Imagepipeline, the most core of the module. Although the core module, but the core module is actually a few key points, oriented interface programming, basically find the key interface, the entire framework is clear.
    1. Imagepipeline and Imagepipelineconfig
    2. Producer and Consumer
    • Imagepipeline is the entry point of the entire module, and the primary interface to get the picture is called through it.
    • Imagepipelineconfig, as the name implies, is used to configure imagepipeline properties, such as memory cache configuration, local file cache configuration, Bitmapconfig, and so on.
ProducerAnd CosumerObvious. producersAnd ConsumerMode. ProducerIn the imagepipeline there are various implementations, more than 10 kinds. For example, include NetworkfetcherproducerLocalassetfetcherproducerLocalfilefetchproducerWait a minute. And these producer are where the final "output" picture is, the source of the upper picture ( DataSource<T>) is obtained from here. Of course producer also contains some specific processing, such as encode,resize the picture and so on. Different treatments may correspond to different producer. And many producer are available through producerfactoryThis factory class gets. And the design of a clever place is that producer is often a layer of embedded, what meaning, basically is similar to our usual IO stream:
New BufferedReader ( new InputStreamReader (new fileinputstream (file));

In this way, the underlying raw data, a layer of specific processing, and then produce a final result, easy to use the upper layer directly. For example:
 public <T> threadhandoffproducer<t> newbackgroundthreadhandoffproducer (Producer< T> inputproducer) {returnnew threadhandoffproducer<t>( Mexecutorsupplier.forlightweightbackgroundtasks (), inputproducer);}

The parameter is also a producer, which can then be nested continuously. This in Impineline is called producer sequence (producer chain? )。 corresponding to have producersequencefactoryThis factory class, use it to get different producer sequence. Consumeris used to receive producer output results, the final step back to the upper UI. Operation:  The process of running the entire library (actually an activity diagram) is briefly as follows (detailedDouble your Patience .See sequence diagram above):Frame:  The framework is probably such (personal summary): imagepipeline Frame Diagram Other:Because the whole library contains a lot of things, functional and extensibility is very strong, and there are many clever design, this article can not be described in detail, the above analysis is the overall framework of the library, although the whole library is not small, but in fact, the shelf on the top of something just. Here are some other things to watch out for Key Point:
  1. Cache mechanism (level three cache), how the memory cache is managed, and how the file cache is stored and read;
  2. Memory management, to prevent oom, mainly bitmap and other memory occupied large object processing; The main use of sharedreference This class, related to closeablereference, the entire library is everywhere, is actually a support reference count of the object, type C + + shared_ptr, when the count is 0 o'clock, it can be released.
  3. Different image format corresponding to the processing, image processing involves some third-party native library, such as giflib,libjpeg,libpng, etc.;
  4. A variety of different drawable implementations, especially progressive drawable and GIF, and draweehierarchy for the implementation of the image switching;
  5. multithreaded processing. Why is it called pipeline? This is also worth examining. Pipeline familiar with the operating system know that the modern CPU architecture is pipelined to achieve parallelism. Personally, Imagepipeline also has this meaning, split a task into multiple independent units, and then parallel processing. Also mentioned in the official documentation. Yes
      
  6. The entire library implementation design, a variety of design patterns into the mess. Builder,factory,wrapper,producer/consumer,adapter and so on.
  7. other details, discover for yourself ... .
Summary: Fresco does provide a very powerful feature, and the support is perfect. However, compared to other similar libraries is different, natural advantages and disadvantages exist. Advantages and disadvantages of the reference is: Http://stackoverflow.com/questions/29363321/picasso-v-s-imageloader-v-s-fresco-vs-glide, Here is a comparison of several image loading libraries, frescoPros and consAs follows:(-)
-Huge size of library
-App Freeze while loading big images from the Internet into ListView
-Huge size of cache
(+)
-Pretty fast image loader
-A lot of functionalityFresco pros and cons, because it is not used in the actual project, so there is no detailed data, pending further confirmation, but the library is really big, which is more obvious. ~ ~ ~

Fresco analysis (i) ———— brief analysis

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.