[关闭]
@linux1s1s 2016-07-19T03:11:12.000000Z 字数 6123 阅读 1396

Fresco源码分析(2) - GenericDraweeHierarchy构建图层

Fresco 2016-07


转载 作者:Desmond

在本篇的内容中,作者将初步介绍Fresco是怎么构建图像层次的。

1 引言

DraweeHierarchy是所有Hierarchy的父接口,它内部只提供了一个基本而又不可缺失的功能:获取图层树的父节点图层。不过仅仅只有这个功能是不够的,Fresco紧接着用接口SettableHierarchy来继承它,声明一些具体的功能:

这几个函数都会在后面起到比较大的作用。
在接下来的内容中会介绍本节的主角:GenericDraweeHierarchy。它实现了SettableHierarchy接口,你可以从这个类中看到大部分Fresco处理图层的逻辑。

2 图层封装者 - GenericDraweeHierarchy

请记住这句话:GenericDraweeHierarchy只是负责装载每个图层信息的载体。如果你直接使用它去显示图片,那就意味着你将放弃Fresco提供的加载与缓存机制。你可以认为这么做之后SimpleDraweeView就退化成了一个简单的ImageView,只会将ArrayDrawable中的所有设置的图片按顺序显示出来。具体的细节我们将在Fresco源码分析(3) - DraweeView显示图层树中讨论。

首先看几个成员变量:

  1. /* 占位图层 */
  2. private final int mPlaceholderImageIndex;
  3. /* 进度条图层 */
  4. private final int mProgressBarImageIndex;
  5. /* 目标显示图层 */
  6. private final int mActualImageIndex;
  7. /* 重试图层 */
  8. private final int mRetryImageIndex;
  9. /* 失败图层 */
  10. private final int mFailureImageIndex;
  11. /* 控制覆盖图层 */
  12. private final int mControllerOverlayIndex;

简洁明了有木有!没错,这个GenericDraweeHierarchy就是封装与维护Drawable层次的家伙!你需要牢记以上这六种图层名字,它是Fresco的视图显示中最主要的六个图层。

2.1 建造者模式

如果你经常使用Fresco,你就会发现它的设计之中充斥着建造者模式。由于Fresco中的对象初始化经常是比较复杂的,建造者模式能为开发者在创建实例上省去很多功夫。

1GenericDraweeHierarchy的建造者是GenericDraweeHierarchyBuilder。它内部维持着许多图层属性,主要有这两种:
- 默认的变量(渐变动画时间、ScaleType
- 程序的Resources实例
- 圆角矩形容器的一些参数
- 要放到每个图层容器中的Drawable实例及图层要应用的ScaleTypeMatrix等等。

这个Builder内部有大量的getter与setter,你可以为每个图层指定Drawable、ScaleType,以及目标显示图层还可以设置Matrix、Focus(配合ScaleTypeFOCUS_CROP时使用)、ColorFilter。

2.2 初始化图层

我们来看一下GenericDraweeHierarchy,从中能够理解Fresco是怎么初始化图层的。

  1. GenericDraweeHierarchy(GenericDraweeHierarchyBuilder builder) {
  2. mResources = builder.getResources();
  3. // 获取圆角参数
  4. mRoundingParams = builder.getRoundingParams();.
  5. // 初始化图层数为0
  6. int numLayers = 0;
  7. int numBackgrounds = (builder.getBackgrounds() != null) ? builder.getBackgrounds().size() : 0;
  8. int backgroundsIndex = numLayers;
  9. numLayers += numBackgrounds;

在这段代码中我们可以看到最开始初始化的是背景图层(顶层图层),会根据是否传入背景图层来判断图层数是否增减。再接着往下看:

  1. Drawable placeholderImageBranch = builder.getPlaceholderImage();
  2. if (placeholderImageBranch == null) {
  3. placeholderImageBranch = getEmptyPlaceholderDrawable();
  4. }
  5. placeholderImageBranch = maybeApplyRoundingBitmapOnly(
  6. mRoundingParams,
  7. mResources,
  8. placeholderImageBranch);
  9. placeholderImageBranch = maybeWrapWithScaleType(
  10. placeholderImageBranch,
  11. builder.getPlaceholderImageScaleType());
  12. mPlaceholderImageIndex = numLayers++;

在这段代码中,它对占位图层进行了以下处理:
- 获取图层Drawable资源,如果没有设置,它将创建一个透明图层。
- 根据圆角参数对图片进行圆角处理。
- 将待显示的Drawable资源包装进一个ScaleTypeDrawable中,处理缩放逻辑(关于ScaleTypeDrawable可以参考Fresco源码分析(1) - 图像层次与各类Drawable)。
- 记录图层在ArrayDrawable中的index,图层数量加一。

我们再看看目标显示图层的处理逻辑,与占位图层的处理有什么区别:

  1. Drawable actualImageBranch = null;
  2. mActualImageSettableDrawable = new SettableDrawable(mEmptyActualImageDrawable);
  3. actualImageBranch = mActualImageSettableDrawable;
  4. actualImageBranch = maybeWrapWithScaleType(
  5. actualImageBranch,
  6. builder.getActualImageScaleType(),
  7. builder.getActualImageFocusPoint());
  8. actualImageBranch = maybeWrapWithMatrix(
  9. actualImageBranch,
  10. builder.getActualImageMatrix());
  11. actualImageBranch.setColorFilter(builder.getActualImageColorFilter());
  12. mActualImageIndex = numLayers++;

与占位图层有区别的是它在显示图上多加了一了SettableDrawable容器(正常图层只有一个ScaleTypeDrawable容器),没有进行圆角处理。由于可以后续改变图像内容,它直接使用了默认的透明图来初始化图层,而且它还拥有ColorFilter、Matrix等特权。

需要注意的一点:在不显式指定图层内容的时候,占位图层、目标显示图层、控制覆盖图层将会创建透明图层实例,其他图层不会创建实例。 并且只有在指定内容的时候图层数量才会增加,除此以外其他图层与占位图、目标图层的初始化没有什么区别。

在初始化完基本图层之后,那我们接着看余下初始化过程:

  1. // overlays
  2. int overlaysIndex = numLayers;
  3. int numOverlays =
  4. ((builder.getOverlays() != null) ? builder.getOverlays().size() : 0) +
  5. ((builder.getPressedStateOverlay() != null) ? 1 : 0);
  6. numLayers += numOverlays;
  7. // controller overlay
  8. mControllerOverlayIndex = numLayers++;

这部分是初始化覆盖图层及控制覆盖图层。如果没有设置覆盖图层,他们不会被初始化。不过控制覆盖图层是会被初始化成透明图层的。

  1. // array of layers
  2. Drawable[] layers = new Drawable[numLayers];
  3. if (numBackgrounds > 0) {
  4. int index = 0;
  5. for (Drawable background : builder.getBackgrounds()) {
  6. layers[backgroundsIndex + index++] =
  7. maybeApplyRoundingBitmapOnly(mRoundingParams, mResources, background);
  8. }
  9. }
  10. if (mPlaceholderImageIndex >= 0) {
  11. layers[mPlaceholderImageIndex] = placeholderImageBranch;
  12. }
  13. // 各图层赋值
  14. if (mFailureImageIndex >= 0) {
  15. layers[mFailureImageIndex] = failureImageBranch;
  16. }
  17. if (numOverlays > 0) {
  18. int index = 0;
  19. if (builder.getOverlays() != null) {
  20. for (Drawable overlay : builder.getOverlays()) {
  21. layers[overlaysIndex + index++] = overlay;
  22. }
  23. }
  24. //按下时加在图片上的的覆盖图层
  25. if (builder.getPressedStateOverlay() != null) {
  26. layers[overlaysIndex + index++] = builder.getPressedStateOverlay();
  27. }
  28. }
  29. if (mControllerOverlayIndex >= 0) {
  30. layers[mControllerOverlayIndex] = mEmptyControllerOverlayDrawable;
  31. }

这段代码的意思也是简明易懂,它新建个Drawable数组,将存在的图层依次向里面添加。如果你了解了上一章的内容,你自然知道接下来做的是什么:用这个数组来初始化FadeDrawable,初始化整个视图层次。接着对图层做圆角处理(需要的话)。

  1. // 初始化FadeDrawable
  2. mFadeDrawable = new FadeDrawable(layers);
  3. mFadeDrawable.setTransitionDuration(builder.getFadeDuration());
  4. // 圆角处理
  5. Drawable maybeRoundedDrawable =
  6. maybeWrapWithRoundedOverlayColor(mRoundingParams, mFadeDrawable);
  7. // 包装成RootDrawable实例
  8. mTopLevelDrawable = new RootDrawable(maybeRoundedDrawable);
  9. mTopLevelDrawable.mutate();
  10. resetFade();
  11. }

resetFade()中将占位图 、背景图层、覆盖图层显示出来。

2.3 需要注意的一点:一个Drawable实例只能与一个DraweeHierarchy绑定!

如果绑定了多个DraweeHierarchy,会出问题。由于在初始化过程中它将Drawable数组赋值给FadeDrawable,而FadeDrawable又继承于ArrayDrawable,它会在初始化的时候为每个数组Drawable的Drawable.Callback设置为自己,若是TransfromAwareDrawable的话还会设置自己为TransformCallback。而我们可以在它的setDrawable(int index, Drawable drawable)函数中看到这么一段代码:

  1. if (drawable != mLayers[index]) {
  2. if (mIsMutated) {
  3. drawable = drawable.mutate();
  4. }
  5. DrawableUtils.setCallbacks(mLayers[index], null, null);
  6. DrawableUtils.setCallbacks(drawable, null, null);
  7. DrawableUtils.setDrawableProperties(drawable, mDrawableProperties);
  8. DrawableUtils.copyProperties(drawable, mLayers[index]);
  9. DrawableUtils.setCallbacks(drawable, this, this);
  10. mIsStatefulCalculated = false;
  11. mLayers[index] = drawable;
  12. invalidateSelf();
  13. }

可以看出,在替换图片时,它会将原来Drawable的这些回调都设置为null。由此很可能会导致Bug,请参考我的一篇翻译文章:Android LayerDrawable 和 Drawable.Callback

3 类图

由于类中方法、变量过多,作者对其做了大量精简,仅用于参考设计层次。

Class Diagram

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注