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> {
。。。
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

一个允许跨资源类型将转换应用到目标中的视图的接口。包装视图的目标将能够提供所有必要的参数并开始转换。不这样做的那将无法提供必要的参数,因此将被迫忽视过渡。这个接口是一种折衷,它允许在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();
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

一个接口包装了一个视图,它公开了必要的方法来运行各种类型的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();
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

返回视图中显示的当前绘制对象,如果不存在该绘制对象(或无法检索),则返回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);
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

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

/**
   * 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);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

从当前在给定视图中显示的前一个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);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

很显然和设置转换**.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> {
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

一个工厂类,它可以根据请求的状态生成不同的转换。
类型参数:
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);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

返回一个新的过渡。
参数:
数据源-资源被加载的数据源。
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> {
。。。
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

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

@Override
  public Transition<R> build(DataSource dataSource, boolean isFirstResource) {
    Transition<Drawable> transition = realFactory.build(dataSource, isFirstResource);
    return new BitmapGlideAnimation(transition);
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
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);
    }
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
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;
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

用于位图的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> {
。。。
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

一个工厂类,它生成一个新的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;
    }
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

总是返回相同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> {
 。。。
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

一个生成视图转换的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> {
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

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

小计

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


下面是一段smartApi使用介绍:

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

下载地址:

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