DraweeHolder 中的 Fresco 淡入淡出
Fresco crossfading within a DraweeHolder
我在自定义视图中使用 DraweeHolder 来显示图像。当我想使用新资源更新图像时,我创建一个新的 DraweeController,然后将 setController 设置为 DraweeHolder。
我要实现的是新旧图像之间的淡入淡出效果。
对于 fade in/out,我递归地将 Alpha 设置为 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 设置 high/low 分辨率图像也没有提供淡入淡出的选项。
你走在正确的轨道上。您将必须编写自定义视图(例如,通过扩展 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
}
我在自定义视图中使用 DraweeHolder 来显示图像。当我想使用新资源更新图像时,我创建一个新的 DraweeController,然后将 setController 设置为 DraweeHolder。
我要实现的是新旧图像之间的淡入淡出效果。 对于 fade in/out,我递归地将 Alpha 设置为 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 设置 high/low 分辨率图像也没有提供淡入淡出的选项。
你走在正确的轨道上。您将必须编写自定义视图(例如,通过扩展 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
}