Glide源码阅读之工厂模式3【TransitionFactory】【Transition】

参考阅读:

Glide多种组合使用方式记录–没有全部亲测,大家可以根据实际需要选用
Glide设计模式之工厂模式1【ModelLoaderFactory】
Glide设计模式之工厂模式2【DiskCache.Factory】
Glide工厂模式3【TransitionFactory】【Transition】
Glide设计模式之工厂模式4总结

Transition

包路径:com.bumptech.glide.request.transition.Transition
Transition接口业务是TransitionFactory的生产对象目标

/**
 * An interface that allows a transition to be applied to {@link android.view.View}s in {@link
 * com.bumptech.glide.request.target.Target}s in across resource types. Targets that wrap views will
 * be able to provide all of the necessary arguments and start the transition. Those that do not
 * will be unable to provide the necessary arguments and will therefore be forced to ignore the
 * transition. This interface is a compromise that allows view specific transition in Glide's
 * complex world of arbitrary resource types and arbitrary target types.
 *
 * @param <R> The type of the resource whose entrance will be transitioned.
 */

public interface Transition<R> {
。。。

一个允许跨资源类型将转换应用到目标中的视图的接口。包装视图的目标将能够提供所有必要的参数并开始转换。不这样做的那将无法提供必要的参数,因此将被迫忽视过渡。这个接口是一种折衷,它允许在Glide的任意资源类型和任意目标类型的复杂世界中查看特定的过渡。

类型参数:
R -其入口将被转换的资源的类型。



  /**
   * An interface wrapping a view that exposes the necessary methods to run the various types of
   * android animations as transitions: ({@link ViewTransition}, {@link ViewPropertyTransition} and
   * animated {@link android.graphics.drawable.Drawable}s).
   */
   
  interface ViewAdapter {
    /** Returns the wrapped {@link android.view.View}. */
    返回被包装的视图。
    View getView();

一个接口包装了一个视图,它公开了必要的方法来运行各种类型的android动画的过渡(ViewTransition, ViewPropertyTransition和animated Drawables)。


    /**
     * Returns the current drawable being displayed in the view, or null if no such drawable exists
     * (or one cannot be retrieved).
     */ 
    @Nullable
    Drawable getCurrentDrawable();

返回视图中显示的当前绘制对象,如果不存在该绘制对象(或无法检索),则返回null。


    /**
     * Sets the current drawable (usually an animated drawable) to display in the wrapped view.
     *
     * @param drawable The drawable to display in the wrapped view.
     */
     
    void setDrawable(Drawable drawable);
  }

设置当前绘制对象(通常是动画绘制对象)显示在换行视图中。


  /**
   * Animates from the previous {@link android.graphics.drawable.Drawable} that is currently being
   * displayed in the given view, if not null, to the new resource that should be displayed in the
   * view.
   *
   * @param current The new resource that will be displayed in the view.
   * @param adapter The {@link Transition.ViewAdapter} wrapping a view that can at least return an
   *     {@link android.view.View} from {@link Transition.ViewAdapter#getView()}.
   * @return True if in the process of running the transition, the new resource was put on the view,
   *     false if the caller needs to manually put the current resource on the view.
   */
   
  boolean transition(R current, ViewAdapter adapter);
}

从当前在给定视图中显示的前一个Drawable动画,如果不为空,到应该在视图中显示的新资源。
参数:
current—将在视图中显示的新资源。
适配器-过渡。ViewAdapter包装的视图至少可以从Transition.ViewAdapter.getView()返回一个视图。
返回:
如果在运行转换的过程中,新资源被放到视图上,则为True,如果调用者需要手动将当前资源放到视图上,则为false。

使用

GlideApp.with(context)
        .load(url)
        .transition(withCrossFade(factory))
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .placeholder(R.color.placeholder)
        .into(imageView);

很显然和设置转换**.transition(withCrossFade(factory))**相关

TransitionFactory

包路径:com.bumptech.glide.request.transition.TransitionFactory

/**
 * A factory class that can produce different {@link Transition}s based on the state of the request.
 *
 * @param <R> The type of resource that needs to be animated into the target.
 */

public interface TransitionFactory<R> {

一个工厂类,它可以根据请求的状态生成不同的转换。
类型参数:
R -需要动画到目标中的资源的类型。




  /**
   * Returns a new {@link Transition}.
   *
   * @param dataSource The {@link com.bumptech.glide.load.DataSource} the resource was loaded from.
   * @param isFirstResource True if this is the first resource to be loaded into the target.
   */
   
  Transition<R> build(DataSource dataSource, boolean isFirstResource);
}

返回一个新的过渡。
参数:
数据源-资源被加载的数据源。
isFirstResource -如果这是加载到目标中的第一个资源,则为True。

具体的实现工厂类如下列表

  • BitmapContainerTransitionFactory
  • BitmapTransitionFactory
  • DrawableCrossFadeFactory
  • NoTransition.NoAnimationFactory
  • ViewAnimationFactory
  • ViewPropertyAnimationFactory

BitmapContainerTransitionFactory

包路径:com.bumptech.glide.request.transition.BitmapContainerTransitionFactory

/**
 * A {@link TransitionFactory} for complex types that have a {@link android.graphics.Bitmap} inside.
 * The transitioning bitmap is wrapped in a {@link android.graphics.drawable.BitmapDrawable}. Most
 * commonly used with {@link DrawableCrossFadeFactory}.
 *
 * @param <R> The type of the composite object that contains the {@link android.graphics.Bitmap} to
 *     be transitioned.
 */
public abstract class BitmapContainerTransitionFactory<R> implements TransitionFactory<R> {
。。。

一个内部有位图的复杂类型的TransitionFactory。转换位图被包装在BitmapDrawable中。最常用的是drawablecrosfadefactory。
类型参数:
R -包含要转换的位图的复合对象的类型。

 @Override
  public Transition<R> build(DataSource dataSource, boolean isFirstResource) {
    Transition<Drawable> transition = realFactory.build(dataSource, isFirstResource);
    return new BitmapGlideAnimation(transition);
  }
private final class BitmapGlideAnimation implements Transition<R> {
    private final Transition<Drawable> transition;

    BitmapGlideAnimation(Transition<Drawable> transition) {
      this.transition = transition;
    }

    @Override
    public boolean transition(R current, ViewAdapter adapter) {
      Resources resources = adapter.getView().getResources();
      Drawable currentBitmap = new BitmapDrawable(resources, getBitmap(current));
      return transition.transition(currentBitmap, adapter);
    }
  }

BitmapTransitionFactory

包路径:com.bumptech.glide.request.transition.BitmapTransitionFactory

/**
 * A {@link TransitionFactory} for {@link android.graphics.Bitmap}s that uses a Drawable transition
 * factory to transition from an existing drawable already visible on the target to the new bitmap.
 *
 * @see BitmapContainerTransitionFactory
 */
public class BitmapTransitionFactory extends BitmapContainerTransitionFactory<Bitmap> {
  public BitmapTransitionFactory(@NonNull TransitionFactory<Drawable> realFactory) {
    super(realFactory);
  }

  @Override
  @NonNull
  protected Bitmap getBitmap(@NonNull Bitmap current) {
    return current;
  }
}

用于位图的TransitionFactory,它使用一个Drawable转换工厂从一个已经在目标上可见的可绘制对象转换到新的位图。

DrawableCrossFadeFactory

/**
 * A factory class that produces a new {@link Transition} that varies depending on whether or not
 * the drawable was loaded from the memory cache and whether or not the drawable is the first image
 * to be put on the target.
 *
 * <p>Resources are usually loaded from the memory cache just before the user can see the view, for
 * example when the user changes screens or scrolls back and forth in a list. In those cases the
 * user typically does not expect to see a transition. As a result, when the resource is loaded from
 * the memory cache this factory produces an {@link NoTransition}.
 */
// Public API.
@SuppressWarnings("WeakerAccess")
public class DrawableCrossFadeFactory implements TransitionFactory<Drawable> {
。。。

一个工厂类,它生成一个新的Transition,这个Transition取决于绘制对象是否从内存缓存中加载,以及绘制对象是否是第一个放到目标上的图像。
资源通常在用户可以看到视图之前从内存缓存加载,例如当用户更改屏幕或在列表中来回滚动时。在这些情况下,用户通常不希望看到转换。因此,当资源从内存缓存加载时,这个工厂产生一个NoTransition

NoTransition.NoAnimationFactory

/**
   * A factory that always returns the same {@link NoTransition}.
   *
   * @param <R> the resource type that will be transitioned into a {@link
   *     com.bumptech.glide.request.target.Target}.
   */
  public static class NoAnimationFactory<R> implements TransitionFactory<R> {
    @SuppressWarnings("unchecked")
    @Override
    public Transition<R> build(DataSource dataSource, boolean isFirstResource) {
      return (Transition<R>) NO_ANIMATION;
    }
  }

总是返回相同NoTransition的工厂。
类型参数:
R -将被转换到Target的资源类型。

ViewAnimationFactory

/**
 * A {@link TransitionFactory} that produces {@link ViewTransition}s.
 *
 * @param <R> The type of the resource that will be transitioned into a view.
 */
public class ViewAnimationFactory<R> implements TransitionFactory<R> {
 。。。

一个生成视图转换的TransitionFactory。
类型参数:
R -将被转换到视图的资源的类型。

ViewPropertyAnimationFactory

/**
 * A {@link TransitionFactory} that produces ViewPropertyAnimations.
 *
 * @param <R> The type of the resource that will be transitioned into a view.
 */
public class ViewPropertyAnimationFactory<R> implements TransitionFactory<R> {
 

一个生成ViewPropertyAnimations的TransitionFactory。
类型参数:
R -将被转换到视图的资源的类型。

小计

在Glide里比较通用的工厂模式结构使用形式,看完前几篇这篇也很容易理解意图

更多

单例模式
Glide设计模式之单例模式
空对象模式
Glide设计模式之空对象模式【EmptyModelLoader】【EmptyList<E>
建造者模式
Glide多种组合使用方式记录–没有全部亲测,大家可以根据实际需要选用
Glide设计模式之建造者(builder)模式1【GlideBuilder】
Glide设计模式之建造者(builder)模式2【RequestBuilder】
Glide设计模式之建造者(builder)模式3【RequestOptions】【BaseRequestOptions】
Glide设计模式之建造者(builder)模式4总结【MemorySizeCalculator】【GlideExecutor】【PreFillType】【LazyHeaders】
工厂模式
Glide设计模式之工厂模式1【ModelLoaderFactory】
Glide设计模式之工厂模式2【DiskCache.Factory】
Glide工厂模式3【TransitionFactory】【Transition】
Glide设计模式之工厂模式4总结

自研产品推荐

历时一年半多开发终于smartApi-v1.0.0版本在2023-09-15晚十点正式上线
smartApi是一款对标国外的postman的api调试开发工具,由于开发人力就作者一个所以人力有限,因此v1.0.0版本功能进行精简,大功能项有:

  • api参数填写
  • api请求响应数据展示
  • PDF形式的分享文档
  • Mock本地化解决方案
  • api列表数据本地化处理
  • 再加上UI方面的打磨

为了更好服务大家把之前的公众号和软件激活结合,如有疑问请大家反馈到公众号即可,下个版本30%以上的更新会来自公众号的反馈。
嗯!先解释不上服务端原因,API调试工具的绝大多数时候就是一个数据模型、数据处理、数据模型理解共识的问题解决工具,所以作者结合自己十多年开发使用的一些痛点来打造的,再加上服务端开发一般是面向企业的,作者目前没有精力和时间去打造企业服务。再加上没有资金投入所以服务端开发会滞后,至于什么时候会进行开发,这个要看募资情况和用户反馈综合考虑。虽然目前国内有些比较知名的api工具了,但作者使用后还是觉得和实际使用场景不符。如果有相关吐槽也可以在作者的公众号里反馈蛤!
下面是一段smartApi使用介绍:
在这里插入图片描述

下载地址:

https://pan.baidu.com/s/1kFAGbsFIk3dDR64NwM5y2A?pwd=csdn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lichong951

你的鼓励决定更新的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值