我在自定义视图中使用DraweeHolder来显示图像。当我想用新资源更新图像时,我创建一个新的DraweeController,然后将setController创建为DraweeHolder。
我想要实现的是前一个图像和新图像之间的交叉淡化效果。 对于淡入/淡出,我将setAlpha递归到drawable。
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设置高/低分辨率图像也不能选择交叉渐变。
答案 0 :(得分:2)
你走在正确的轨道上。您必须编写自定义视图(例如,通过扩展ImageView
)。请记住,为了让Drawee正常工作,您需要连接一些东西,请阅读here如何做到这一点。
您需要两个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
}