设为首页 加入收藏

TOP

Fresco源码解析 - Hierarachy-View-Controller(一)
2015-11-21 01:04:39 来源: 作者: 【 】 浏览:3
Tags:Fresco 源码 解析 Hierarachy-View-Controller

Fresco是一个MVC模型,由三大组件构成,它们的对应关系如下所示:

M -> DraweeHierarchy V -> DraweeView C -> DraweeController

M 所对应的 DraweeHierarchy 是一个有层次结构的数据结构,DraweeView 用来显示位于 DraweeHierarchy 最顶层的图像(top level drawable),DraweeController 则用来控制 DraweeHierarchy 的顶层图像是哪一个。

 o FadeDrawable (top level drawable)
 |
 +--o ScaleTypeDrawable
 |  |
 |  +--o BitmapDrawable
 |
 +--o ScaleTypeDrawable
    |
    +--o BitmapDrawable

三者的互动关系很简单,DraweeView 把获得的 Event 转发给 Controller,然后 Controller 根据 Event 来决定是否需要显示和隐藏 (包括动画)图像,而这些图像都存储在 Hierarchy 中,最后 DraweeView 绘制时直接通过 getTopLevelDrawable 就可以获取需要显示的图像。

\


需要注意的是,虽然现在最新的代码中,DraweeView 还是继承自 ImageView,但是以后会直接继承 View,所以我们用 DraweeView 时,尽量不要使用 ImageView 的API,例如 setImageXxxsetScaleType,注释里也写得很清楚。

Although ImageView is subclassed instead of subclassing View directly, this class does not suppZ??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcnQgSW1hZ2VWaWV3JnJzcXVvO3Mgc2V0SW1hZ2VYeHgsIHNldFNjYWxlVHlwZSBhbmQgc2ltaWxhciBtZXRob2RzLiBFeHRlbmRpbmcgSW1hZ2VWaWV3IGlzIGEgc2hvcnQgdGVybSBzb2x1dGlvbiBpbiBvcmRlciB0byBpbmhlcml0IHNvbWUgb2YgaXRzIGltcGxlbWVudGF0aW9uIChwYWRkaW5nIGNhbGN1bGF0aW9ucywgZXRjLikuIFRoaXMgY2xhc3MgaXMgbGlrZWx5IHRvIGJlIGNvbnZlcnRlZCB0byBleHRlbmQgVmlldyBkaXJlY3RseSBpbiB0aGUgZnV0dXJlLCBzbyA8c3Ryb25nPmF2b2lkIHVzaW5nIEltYWdlVmlldyZyc3F1bztzIG1ldGhvZHMgYW5kIHByb3BlcnRpZXM8L3N0cm9uZz4gKFQ1ODU2MTc1KS48L3A+DQo8L2Jsb2NrcXVvdGU+DQo8cD7TybnYz7XNvL/J0tS/tLP2o6w8Y29kZT5EcmF3ZWVWaWV3PC9jb2RlPiDW0LKiw7vT0CA8Y29kZT5EcmF3ZWVIaWVyYXJjaHk8L2NvZGU+ILrNIDxjb2RlPkRyYXdlZUNvbnRyb2xsZXI8L2NvZGU+IMDg0M21xLPJ1LGx5MG/o6y2+Na709DSu7j2IDxjb2RlPkRyYXdIb2xkZXI8L2NvZGU+IMDg0M21xCA8Y29kZT5tRHJhd0hvbGRlcjwvY29kZT6hozwvcD4NCjxwcmUgY2xhc3M9"brush:java;"> public class DraweeHolder implements VisibilityCallback { // other properties private DH mHierarchy; private DraweeController mController = null; // methods }

DraweeHolder 存储了 mHierarchymController,FB 为什么要这么设计呢?注释里也写得很清楚:

Drawee users, should, as a rule, use DraweeView or its subclasses. There are situations where custom views are required, however, and this class is for those circumstances.

稍微解释一下,这是一个解耦的设计,当我们不想使用 DraweeView,通过 ViewHolder 照样可以使用其他两个组件。比方说,自定义一个View,然后像 DraweeView 那样,在 View 中添加一个 DrawHolder 的成员变量。

再来看 DraweeView 的代码:

public class DraweeView
    
      extends ImageView { // other methods and properties /** Sets the hierarchy. */ public void setHierarchy(DH hierarchy) { mDraweeHolder.setHierarchy(hierarchy); super.setImageDrawable(mDraweeHolder.getTopLevelDrawable()); } /** Sets the controller. */ public void setController(@Nullable DraweeController draweeController) { mDraweeHolder.setController(draweeController); super.setImageDrawable(mDraweeHolder.getTopLevelDrawable()); } }
    

每次为 DraweeView 设置 hierarchycontroller 时,会同时通过 super.setImageDrawable(mDraweeHolder.getTopLevelDrawable()) 更新需要显示的图像。

/**
 * Gets the top-level drawable if hierarchy is set, null otherwise.
 */
public Drawable getTopLevelDrawable() {
  return mHierarchy == null ? null : mHierarchy.getTopLevelDrawable();
}

DraweeHierarchy 只定义了一个方法 - getTopLevelDrawable

public interface DraweeHierarchy {

  /**
   * Returns the top level drawable in the corresponding hierarchy. Hierarchy should always have
   * the same instance of its top level drawa
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇二叉排序树(c++实现) 下一篇将Clob对象转换成String对象

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: