Fresco source parsing-initialization process analysis

Source: Internet
Author: User

Before using Fresco , it is important to initialize, the general initialization of the work will be Application.onCreate() completed, of course, can also be Drawee completed before use.

The Fresco itself provides two types of initialization, one using the default configuration initialization and the other using a user-defined configuration.

The following code is the two initialization methods provided by fresco. The first one only needs to provide one Context parameter, and the second one needs to provide a configuration instance of Imagepipeline ImagePipelineConfig .

/** Initializes Fresco with the default config. */publicstaticvoidinitialize(Context context) {  ImagePipelineFactory.initialize(context);  initializeDrawee(context);}
/** Initializes Fresco with the specified config. */publicstaticvoidinitialize(Context context, ImagePipelineConfig imagePipelineConfig) {  ImagePipelineFactory.initialize(imagePipelineConfig);  initializeDrawee(context);}

Let's start by analyzing the first way.

Start initialization
Fresco.initialized(context)
Initialize with default parameters

Com.facebook.drawee.backends.pipeline.Fresco

/** Initializes Fresco with the default config. */publicstaticvoidinitialize(Context context) {    ImagePipelineFactory.initialize(context);    initializeDrawee(context);}

It is ImagePipeline responsible for acquiring image data, which can be either a network picture or a local picture. Here is a Factory- ImagePipelineFactory to create the default ImagePipleline .

Create Imagepipeline

Com.facebook.imagepipeline.core.ImagePipelineFactory

/** Initializes {@link ImagePipelineFactory} with default config. */publicstaticvoidinitialize(Context context) {  initialize(ImagePipelineConfig.newBuilder(context).build());}

ImagePipelineConfigImagePipelineprovides the necessary parameters for the initialization work, which is built using the Builder mode .

ImagePipelineConfigContains a number of parameters, because when we call Fresco.initialize() the value passed a context parameter, so Fresco has not obtained any user-defined data, so all use the default value, the Builder class only provides the process of building, and the default value will need to wait until the new ImagePipelineConfig creation.

Builderpart of the source code can be seen, the initialization of a need for a ImagePipeline lot of parameters, the specific meaning of these parameters will be introduced in the following blog post.

 Public Static  class Builder {  PrivateSupplier<memorycacheparams> Mbitmapmemorycacheparamssupplier;PrivateCachekeyfactory mcachekeyfactory;Private FinalContext Mcontext;PrivateSupplier<memorycacheparams> Mencodedmemorycacheparamssupplier;PrivateExecutorsupplier Mexecutorsupplier;PrivateImagecachestatstracker Mimagecachestatstracker;PrivateImagedecoder Mimagedecoder;PrivateSupplier<boolean> Misprefetchenabledsupplier;PrivateDiskcacheconfig Mmaindiskcacheconfig;PrivateMemorytrimmableregistry Mmemorytrimmableregistry;PrivateNetworkfetcher Mnetworkfetcher;PrivatePoolfactory mpoolfactory;PrivateProgressivejpegconfig Mprogressivejpegconfig;PrivateSet<requestlistener> mrequestlisteners;Private BooleanMresizeandrotateenabledfornetwork =true;PrivateDiskcacheconfig Msmallimagediskcacheconfig;PrivateAnimatedimagefactory manimatedimagefactory;//Other methods}

From the Fresco initialize method we learned that ImagePipelineConfig this was created:

ImagePipelineConfig.newBuilder(context).build())

Instead Builder of providing a default value for the parameter, the default value is definitely assigned at the completion of the buid() method.

Com.facebook.imagepipeline.core.imagepipelinefactory$builder

publicbuild() {  returnnew ImagePipelineConfig(this);}

As can be seen from the above code, build() one is created and ImagePipelineConfig then this passed as a parameter to the constructor, and ImagePipelineConfig the constructor is based on Builder initializing itself.

The initialization strategy is simple:

    • If builder the parameter value in is empty, the default value is used.
    • If builder the parameter value in is not empty, the Builder supplied value is used.

A specific parameter can be used to see if it is empty, if it is builder.mBitmapMemoryCacheParamsSupplier new DefaultBitmapMemoryCacheParamsSupplier() not empty builder.mBitmapMemoryCacheParamsSupplier .

mBitmapMemoryCacheParamsSupplier =    builder.mBitmapMemoryCacheParamsSupplier == null ?        new DefaultBitmapMemoryCacheParamsSupplier(            (ActivityManager) builder.mContext.getSystemService(Context.ACTIVITY_SERVICE)) :        builder.mBitmapMemoryCacheParamsSupplier;

Finally, the static method is passed to the build ImagePipelineConfig instance ImagePipelineFactory initialize , and the initialization is done.

/** Initializes {@link ImagePipelineFactory} with the specified config. */publicstaticvoidinitialize(ImagePipelineConfig imagePipelineConfig) {  new ImagePipelineFactory(imagePipelineConfig);}

ImagePipelineFactoryInstances sInstance are used when initializing Drawee.

Initialize Drawee

From the above analysis, we learned that it Fresco is initialized first ImagePipeline , and ImagePipeline the instance is stored in a ImagePipelineFactory static variable of one type sInstance --and then the initialization begins Drawee .

privatestaticvoidinitializeDrawee(Context context) {  new PipelineDraweeControllerBuilderSupplier(context);  SimpleDraweeView.initialize(sDraweeControllerBuilderSupplier);}

First, new one PipelineDraweeControllerBuilderSupplier , it's PipelineDraweeControllerBuilder the one Supplier . Suppliernot provided by the JDK, but Fresco is moved directly from the guava , the code is simple and provides only one get method.

/** * A class that can supply objects of  A single type. Semantically, this could * is a factory, generator, builder, closure, or something else entirely. No * Guarantees is implied by this interface. * *  @author  Harry Heymann *  @since  2.0 (   Imported from Google collections Library) */ public interface supplier<t> {/** * Retrieves an instance of the appropriate type.   The returned object may or is not is a new instance, depending on the implementation. * *  @return  An instance of the appropriate type */ T get ();}  

As the name implies, Supplier is a provider , the user includes but not limited to,,,, the factory generator builder closure interface method is get() used to return the instance it provides, it should be noted that this instance can be new or not.

Here, PipelineDraweeControllerBuilderSupplier the usage is more like a Factory , it implements the Supplier interface.

 Public  class pipelinedraweecontrollerbuildersupplier implementsSupplier <pipelinedraweecontrollerbuilder> {      Private FinalContext Mcontext;Private FinalImagepipeline Mimagepipeline;Private FinalPipelinedraweecontrollerfactory mpipelinedraweecontrollerfactory;Private FinalSet<controllerlistener> mboundcontrollerlisteners; PublicPipelinedraweecontrollerbuildersupplier (Context context) { This(Context, imagepipelinefactory.getinstance ()); } PublicPipelinedraweecontrollerbuildersupplier (context context, imagepipelinefactory imagepipelinefactory) { This(Context, Imagepipelinefactory,NULL); } PublicPipelinedraweecontrollerbuildersupplier (context context, imagepipelinefactory imagepipelinefactory, Set&lt ;    Controllerlistener> boundcontrollerlisteners) {mcontext = context;    Mimagepipeline = Imagepipelinefactory.getimagepipeline (); Mpipelinedraweecontrollerfactory =NewPipelinedraweecontrollerfactory (Context.getresources (), Deferredreleaser.getinstance (), ImagePipelin    Efactory.getanimateddrawablefactory (), uithreadimmediateexecutorservice.getinstance ());  Mboundcontrollerlisteners = boundcontrollerlisteners; } @Override PublicPipelinedraweecontrollerbuilder get () {return NewPipelinedraweecontrollerbuilder (Mcontext, Mpipelinedraweecontrollerfactory, Mimagepipeline, M  Boundcontrollerlisteners); }}

Constructor PipelineDraweeControllerBuilderSupplier(Context context) that uses the instance that was Fresco created in the initalize method ImagePipelineFactoryBuilder ImagePipelineFactory .

this(context, ImagePipelineFactory.getInstance());

getMethod tells us, ImagePipeline will be stored in PipelineDraweeController , about Controller can refer to fresco source code parsing-Hierarachy-view-controller.

PipelineDraweeControllerIt will also store one mPipelineDraweeControllerFactory .

 Public classpipelinedraweecontrollerfactory {PrivateResources mresources;PrivateDeferredreleaser Mdeferredreleaser;PrivateAnimateddrawablefactory manimateddrawablefactory;PrivateExecutor Muithreadexecutor; Public pipelinedraweecontrollerfactory(Resources resources, Deferredreleaser Deferredreleaser, Animateddrawablefactory animateddrawablefactory,    Executor uithreadexecutor) {mresources = resources;    Mdeferredreleaser = Deferredreleaser;    Manimateddrawablefactory = animateddrawablefactory;  Muithreadexecutor = Uithreadexecutor; } PublicPipelinedraweecontrollerNewcontroller(supplier<datasource<closeablereference<closeableimage>>> datasourcesupplier, String ID, Object Callercontext) {return NewPipelinedraweecontroller (Mresources, Mdeferredreleaser, Manimateddrawablefactory, mUiThreadEx  Ecutor, Datasourcesupplier, ID, callercontext); }}

This mPipelineDraweeControllerFactory will newController create an instance by creating one PipelineDraweeController .

In the end, the initialization work is done.

Although the above analysis is simple, but clearly comb the Fresco initialization process, but still is far from enough, from the above code can be seen, the initialization of the corresponding components (Drawee, imagepipeline) use a lot of design patterns, if not too familiar with these design patterns, It may be difficult to understand. More critical is that the initialization of the corresponding components with a large number of parameters, behind each parameter will involve a lot of knowledge points, in the subsequent blog post, we come to analyze each.

Fresco source parsing-initialization process 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.