Glide Target


一、类结构

基类 Target<Z>
子类 BaseTarget<Z>

BaseTarget 的子类
PreloadTarget,
SimpleTarget<T>,
ViewTarget<T extends View>

Target 类图

二、ImageViewTarget

index class ImageViewTarget 说明
1 Bitmap BitmapImageViewTarget
2 Drawable DrawableImageViewTarget
3 GifDrawable DrawableImageViewTarget isAssignableFrom方法

从Resource<R>获取具体数据,Target 的 onResourceReady() 方法。

@Override
public void onResourceReady(@NonNull Z resource, @Nullable Transition<? super Z> transition) {
    if (transition == null || !transition.transition(resource, this)) {
        setResourceInternal(resource);
    } else {
        maybeUpdateAnimatable(resource);
    }
}

不需要Transition 渐变,直接显示,内部 setResourceInternal() 方法。

抽象方法

protected abstract void setResource(@Nullable Z resource);

子类 BitmapImageViewTarget 或 DrawableImageViewTarget 重写。
资源是 Bitmap 和 Drawable。
分别调用 ImageView 的 setImageBitmap() 和 setImageDrawable() 方法。

maybeUpdateAnimatable() 方法,动画 ,Resource<T> 包装资源类型是 Animatable,如 GifDrawable。

如果需要渐变,不需要 setResource,直接 maybeUpdateAnimatable()。
渐变 transition() 方法已经做过 setDrawable()。

三、Transition 过渡

RequestBuilder 中的 transition() 方法。

public RequestBuilder<TranscodeType> transition(
      TransitionOptions<?, ? super TranscodeType> transitionOptions) {
    this.transitionOptions = Preconditions.checkNotNull(transitionOptions);
    isDefaultTransitionOptionsSet = false;
    return this;
}

TransitionFactory 工厂,来自 TransitionOptions 。

TransitionOptions 类型 包括
BitmapTransitionOptions
DrawableTransitionOptions

public static DrawableTransitionOptions withCrossFade() {
    return new DrawableTransitionOptions().crossFade();
}


public DrawableTransitionOptions crossFade() {
    return crossFade(new DrawableCrossFadeFactory.Builder());
}

withCrossFade() 方法, 可传 duration。
DrawableCrossFadeFactory 工厂。
创建 Transition<Drawable> ,即 DrawableCrossFadeTransition。

渐变方法。

public boolean transition(Drawable current, ViewAdapter adapter) {
    Drawable previous = adapter.getCurrentDrawable();
    if (previous == null) {
      previous = new ColorDrawable(Color.TRANSPARENT);
    }
    TransitionDrawable transitionDrawable =
        new TransitionDrawable(new Drawable[] { previous, current });
    transitionDrawable.setCrossFadeEnabled(isCrossFadeEnabled);
    transitionDrawable.startTransition(duration);
    adapter.setDrawable(transitionDrawable);
    return true;
}

通过系统 TransitionDrawable 类,实现渐变。
ViewAdapter 接口,即 ImageViewTarget 的实现接口。


任重而道远

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容