[关闭]
@mSolo 2015-04-10T13:12:44.000000Z 字数 6712 阅读 3567

Fresco源码阅读(一)

Android 源码分析 Volley 图片处理库 开源


初级

工程结构

  1. drawe
  2. drawee-pipeline
  3. drawee-volley
  4. fbcore
  5. imagepipeline
  6. imagepipeline-okhttp

基本代码统计

背景知识

接口

分析入口

  1. <com.facebook.drawee.view.SimpleDraweeView
  2. android:id="@+id/my_image_view"
  3. android:layout_width="20dp"
  4. android:layout_height="20dp"
  5. fresco:placeholderImage="@drawable/my_drawable"
  6. />
  1. public class SimpleDraweeView extends GenericDraweeView {
  2. //
  3. public class GenericDraweeView extends DraweeView<GenericDraweeHierarchy> {
  4. //
  5. public class DraweeView<DH extends DraweeHierarchy> extends ImageView {
  6. //
  7. public class GenericDraweeHierarchy implements SettableDraweeHierarchy {
  8. //
  9. public interface DraweeHierarchy {
  10. public Drawable getTopLevelDrawable();
  11. }
  12. //
  13. public interface SettableDraweeHierarchy extends DraweeHierarchy {
  1. /**
  2. * A SettableDraweeHierarchy that displays placeholder image until the actual image is set.
  3. * If provided, failure image will be used in case of failure (placeholder otherwise).
  4. * If provided, retry image will be used in case of failure when retrying is enabled.
  5. * If provided, progressbar will be displayed until fully loaded.
  6. * Each image can be displayed with a different scale type (or no scaling at all).
  7. * Fading between the layers is supported.
  8. *
  9. * <p>
  10. * Example hierarchy with placeholder, retry, failure and one actual image:
  11. * <pre>
  12. * o FadeDrawable (top level drawable)
  13. * |
  14. * +--o ScaleTypeDrawable
  15. * | |
  16. * | +--o Drawable (placeholder image)
  17. * |
  18. * +--o ScaleTypeDrawable
  19. * | |
  20. * | +--o SettableDrawable
  21. * | |
  22. * | +--o Drawable (actual image)
  23. * |
  24. * +--o ScaleTypeDrawable
  25. * | |
  26. * | +--o Drawable (retry image)
  27. * |
  28. * +--o ScaleTypeDrawable
  29. * |
  30. * +--o Drawable (failure image)
  31. * </pre>
  32. *
  33. * <p>
  34. */
  35. public class GenericDraweeHierarchy implements SettableDraweeHierarchy {

设计模式入口

1. Facade : SimpleDraweeView、GenericDraweeView

Created with Raphaël 2.1.2SimpleDraweeViewSimpleDraweeViewGenericDraweeViewGenericDraweeViewDraweeViewDraweeViewDraweeHolderDraweeHolder读取xml属性设置GenericDraweeHierarchyBuilder请求代理请求代理设置控制器

2. Builder : GenericDraweeHierarchy、GenericDraweeHierarchyBuilder

Created with Raphaël 2.1.2GenericDraweeHierarchyGenericDraweeHierarchyGenericDraweeHierarchyBuilderGenericDraweeHierarchyBuilder设置默认图片、进度条图片设置重试图片、加载失败图片等设置滤镜等效果

3. Composite : GenericDraweeHierarchy、DraweeHolder、FadeDrawable

4. Chain of Responsibility : 职责链

事件流入口(MVP 模式)


  1. Fresco.java 设置 PipelineDraweeControllerBuilderSupplier 以便初始化 SimpleDraweeView 得到 PipelineDraweeBuilder

    Fresco.initialize(context)
    |--- ImagePipelineFactory.initialize(context);
    |--- initializeDrawee(context);
    |-------- new PipelineDraweeControllerBuilderSupplier(context);
    |-------- SimpleDraweeView.initialize(sDraweeControllerBuilderSupplier);

  2. SimpleDraweeView.java 配置 DraweeController (设置 DataSource 数据源,索引:getDataSource() )
    |--- init() -> mSimpleDraweeControllerBuilder = sDraweeControllerBuilderSupplier.get();
    |--- setImageURI(Uri uri, @Nullable Object callerContext)
    |-------- .setUri()
    |-------- .build() -> AbstractDraweeControllerBuilder.java -> buildController() ->
    |-------------- PipelineDraweeControllerBuilder.java -> obtainController
    |-------- setController(controller) -> mDraweeHolder.setController(draweeController);
  3. DraweeView.java 执行 onAttachedToWindow() -> DraweeController 执行 onAttach()
    |--- submitRequest()
    |-------- getControllerListener().onSubmit(mId, mCallerContext);
    |-------- getDataSource() -> ImagePipeline.java -> fetchImageFromBitmapCache()
    |------------- submitFetchRequest() -> CloseableProducerToDataSourceAdapter.create()
    |-------- new BaseDataSubscriber() -> mDataSource.subscribe();
  4. 获取 DataSource 时,提交 request(索引:构造器 -> produceResults() )
    |--- AbstractProducerToDataSourceAdapter.java(构造器)-> produceResults()
    |-------- createConsumer() -> AbstractDataSource.java -> setResult()
    |------------- setResultInternal() -> notifyDataSubscribers()

㊀ Model

ImagePipeline.java -> fetchImageFromBitmapCache()
FirstAvailableDataSourceSupplier.java -> AbstractDataSource.java

㊁ View Interface

DraweeHolder.java

㊂ View

SimpleDraweeView.java

㊃ Presenter

PipelineDraweeController.java -> AbstractDraweeController.java

补充

其它知识

Sample

Instrumentation : debug(模块化配置)

  1. public class InstrumentedDraweeView extends SimpleDraweeView implements Instrumented {
  2. private Instrumentation mInstrumentation;
  3. private ControllerListener<Object> mListener;
  4. public InstrumentedDraweeView(Context context, GenericDraweeHierarchy hierarchy) {
  5. super(context, hierarchy);
  6. init();
  7. }
  8. public InstrumentedDraweeView(Context context) {
  9. super(context);
  10. init();
  11. }
  12. public InstrumentedDraweeView(Context context, AttributeSet attrs) {
  13. super(context, attrs);
  14. init();
  15. }
  16. public InstrumentedDraweeView(Context context, AttributeSet attrs, int defStyle) {
  17. super(context, attrs, defStyle);
  18. init();
  19. }
  20. private void init() {
  21. mInstrumentation = new Instrumentation(this);
  22. mListener = new BaseControllerListener<Object>() {
  23. @Override
  24. public void onSubmit(String id, Object callerContext) {
  25. mInstrumentation.onStart();
  26. }
  27. @Override
  28. public void onFinalImageSet(
  29. String id,
  30. @Nullable Object imageInfo,
  31. @Nullable Animatable animatable) {
  32. mInstrumentation.onSuccess();
  33. }
  34. @Override
  35. public void onFailure(String id, Throwable throwable) {
  36. mInstrumentation.onFailure();
  37. }
  38. @Override
  39. public void onRelease(String id) {
  40. mInstrumentation.onCancellation();
  41. }
  42. };
  43. }
  44. @Override
  45. public void initInstrumentation(String tag, PerfListener perfListener) {
  46. mInstrumentation.init(tag, perfListener);
  47. }
  48. @Override
  49. public void onDraw(final Canvas canvas) {
  50. super.onDraw(canvas);
  51. mInstrumentation.onDraw(canvas);
  52. }
  53. @Override
  54. public void setImageURI(Uri uri, @Nullable Object callerContext) {
  55. SimpleDraweeControllerBuilder controllerBuilder = getControllerBuilder()
  56. .setUri(uri)
  57. .setCallerContext(callerContext)
  58. .setOldController(getController());
  59. if (controllerBuilder instanceof AbstractDraweeControllerBuilder) {
  60. ((AbstractDraweeControllerBuilder<?,?,?,?>) controllerBuilder)
  61. .setControllerListener(mListener);
  62. }
  63. setController(controllerBuilder.build());
  64. }
  65. public ControllerListener<Object> getListener() {
  66. return mListener;
  67. }
  68. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注