0

我在自定义视图中使用 DraweeHolder 来显示图像。当我想用新资源更新图像时,我创建一个新的 DraweeController,然后将控制器设置为 DraweeHolder。

我想要实现的是旧图像和新图像之间的交叉淡入淡出效果。对于淡入/淡出,我递归地将 Alpha 设置为可绘制对象。

void handleTransition(DraweeHolder draweeHolder, long startingTime) {
    Drawable drawable = draweeHolder.getTopLevelDrawable().mutate();
    int duration = 2000;
    long currTime = System.currentTimeMillis();
    int diff = (int) (currTime - startingTime);
    int toAlpha = Math.max(0, 255 - (255 * diff / duration));
    drawable.setAlpha(toAlpha);
    invalidate();
}


@Override
protected void onDraw(Canvas canvas) {
    Log.d(TAG, "onDraw()");
    super.onDraw(canvas);
    synchronized (this) {
        handleTransition(draweeHolder, mCurrentTransStartTime);
    }
}

它似乎工作正常,除了 onDraw 将被调用太多次。但是,我不知道如何在此 DraweeHolder 中同时出现两个图像(交叉淡入淡出)。即使为 DraweeHolder 设置高/低分辨率图像也不会提供交叉淡入淡出的选项。

4

1 回答 1

2

你在正确的轨道上。您将不得不编写自定义视图(例如通过扩展ImageView)。请记住,您需要连接一些东西才能让 Drawee 正常工作,请在此处阅读如何做到这一点。

您将需要两个DraweeHolders. 使用多个支架时,最好将它们保存在MultiDraweeHolder

  private void init() {
    mMultiDraweeHolder = new MultiDraweeHolder<GenericDraweeHierarchy>();
    // holders for the two images to cross-fade between
    Drawable drawables[] = new Drawable[2];
    for (int i = 0; i < drawables.length; i++) {
      GenericDraweeHierarchy hierarchy = createDraweeHierarchy();
      mMultiDraweeHolder.add(DraweeHolder.create(hierarchy, getContext()));
      drawables[i] = hierarchy.getTopLevelDrawable();
    }
    mFadeDrawable = new FadeDrawable(drawables);
    // no need to override onDraw, ImageView superclass will correctly draw our fade drawable if we set it like this:
    super.setImageDrawable(mFadeDrawable);
    mCurrentIndex = 0;
  }

  void loadNextImage(DraweeController controller) {
    mCurrentIndex = (mCurrentIndex + 1) % 2;
    mMultiDraweeHolder.get(mCurrentIndex).setController(controller);
    mFadeDrawable.fadeToLayer(mCurrentIndex);
  }

  GenericDraweeHierarchy createDraweeHierarchy() {
    // create a new drawee hierarchy here,
    // as each image needs to have its own instance
  }
于 2016-01-27T17:11:06.763 回答