@mSolo
2015-04-10T13:12:44.000000Z
字数 6712
阅读 3567
Android
源码分析
Volley
图片处理库
开源
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="20dp"
android:layout_height="20dp"
fresco:placeholderImage="@drawable/my_drawable"
/>
public class SimpleDraweeView extends GenericDraweeView {
//
public class GenericDraweeView extends DraweeView<GenericDraweeHierarchy> {
//
public class DraweeView<DH extends DraweeHierarchy> extends ImageView {
//
public class GenericDraweeHierarchy implements SettableDraweeHierarchy {
//
public interface DraweeHierarchy {
public Drawable getTopLevelDrawable();
}
//
public interface SettableDraweeHierarchy extends DraweeHierarchy {
/**
* A SettableDraweeHierarchy that displays placeholder image until the actual image is set.
* If provided, failure image will be used in case of failure (placeholder otherwise).
* If provided, retry image will be used in case of failure when retrying is enabled.
* If provided, progressbar will be displayed until fully loaded.
* Each image can be displayed with a different scale type (or no scaling at all).
* Fading between the layers is supported.
*
* <p>
* Example hierarchy with placeholder, retry, failure and one actual image:
* <pre>
* o FadeDrawable (top level drawable)
* |
* +--o ScaleTypeDrawable
* | |
* | +--o Drawable (placeholder image)
* |
* +--o ScaleTypeDrawable
* | |
* | +--o SettableDrawable
* | |
* | +--o Drawable (actual image)
* |
* +--o ScaleTypeDrawable
* | |
* | +--o Drawable (retry image)
* |
* +--o ScaleTypeDrawable
* |
* +--o Drawable (failure image)
* </pre>
*
* <p>
*/
public class GenericDraweeHierarchy implements SettableDraweeHierarchy {
1. Facade : SimpleDraweeView、GenericDraweeView
2. Builder : GenericDraweeHierarchy、GenericDraweeHierarchyBuilder
3. Composite : GenericDraweeHierarchy、DraweeHolder、FadeDrawable
4. Chain of Responsibility : 职责链
Fresco.initialize(context)
|--- ImagePipelineFactory.initialize(context);
|--- initializeDrawee(context);
|-------- new PipelineDraweeControllerBuilderSupplier(context);
|-------- SimpleDraweeView.initialize(sDraweeControllerBuilderSupplier);
㊀ Model
ImagePipeline.java -> fetchImageFromBitmapCache()
FirstAvailableDataSourceSupplier.java -> AbstractDataSource.java
㊁ View Interface
DraweeHolder.java
㊂ View
SimpleDraweeView.java
㊃ Presenter
PipelineDraweeController.java -> AbstractDraweeController.java
补充
Instrumentation : debug(模块化配置)
public class InstrumentedDraweeView extends SimpleDraweeView implements Instrumented {
private Instrumentation mInstrumentation;
private ControllerListener<Object> mListener;
public InstrumentedDraweeView(Context context, GenericDraweeHierarchy hierarchy) {
super(context, hierarchy);
init();
}
public InstrumentedDraweeView(Context context) {
super(context);
init();
}
public InstrumentedDraweeView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public InstrumentedDraweeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
mInstrumentation = new Instrumentation(this);
mListener = new BaseControllerListener<Object>() {
@Override
public void onSubmit(String id, Object callerContext) {
mInstrumentation.onStart();
}
@Override
public void onFinalImageSet(
String id,
@Nullable Object imageInfo,
@Nullable Animatable animatable) {
mInstrumentation.onSuccess();
}
@Override
public void onFailure(String id, Throwable throwable) {
mInstrumentation.onFailure();
}
@Override
public void onRelease(String id) {
mInstrumentation.onCancellation();
}
};
}
@Override
public void initInstrumentation(String tag, PerfListener perfListener) {
mInstrumentation.init(tag, perfListener);
}
@Override
public void onDraw(final Canvas canvas) {
super.onDraw(canvas);
mInstrumentation.onDraw(canvas);
}
@Override
public void setImageURI(Uri uri, @Nullable Object callerContext) {
SimpleDraweeControllerBuilder controllerBuilder = getControllerBuilder()
.setUri(uri)
.setCallerContext(callerContext)
.setOldController(getController());
if (controllerBuilder instanceof AbstractDraweeControllerBuilder) {
((AbstractDraweeControllerBuilder<?,?,?,?>) controllerBuilder)
.setControllerListener(mListener);
}
setController(controllerBuilder.build());
}
public ControllerListener<Object> getListener() {
return mListener;
}
}