Glide4.5分析

阿邹
• 阅读 1144

Glide4.5分析

Glide的基本流程介绍

常见调用方式 Glide.with(context).load((T)url).into(imageView); 这里调用了三个方法

    1. With
    1. Load
    1. Into

With方法:

首先进入Glide类中调用这个方法

public static RequestManager with(@NonNull Context context) {
  return getRetriever(context).get(context);
}

/**
这里get方法中传进去一个context然后闲进去非空判断
再判断是否再主线程中其次再判断context不能等于全局上下文
    这里说下为什么,如果是全局上下文不利于资源的销毁
然后再走,判断上下午是哪个类型,最后相应的返回。
*/
@NonNull
public RequestManager get(@NonNull Context context) {
  if (context == null) {
    throw new IllegalArgumentException("You cannot start a load on a null Context");
  } else if (Util.isOnMainThread() && !(context instanceof Application)) {
    if (context instanceof FragmentActivity) {
      return get((FragmentActivity) context);
    } else if (context instanceof Activity) {
      return get((Activity) context);
    } else if (context instanceof ContextWrapper) {
      return get(((ContextWrapper) context).getBaseContext());
    }
  }

  return getApplicationManager(context);
}

/**
如果上面的context是全局上下文,则会调用getApplicationManager方法,构造一个ApplicationManager对象返回。
ApplicationLifecycle是生命周期接口
EmptyRequestManagerTreeNode是树节点接口
public Set<RequestManager> getDescendants() {
    return Collections.emptySet();
}
看里面的方法应该是保存了许多RequestManager节点,负责取出
构建完毕返回

*/
private RequestManager getApplicationManager(@NonNull Context context) {
  // Either an application context or we're on a background thread.
  if (applicationManager == null) {
    synchronized (this) {
      if (applicationManager == null) {
        // Normally pause/resume is taken care of by the fragment we add to the fragment or
        // activity. However, in this case since the manager attached to the application will not
        // receive lifecycle events, we must force the manager to start resumed using
        // ApplicationLifecycle.

        // TODO(b/27524013): Factor out this Glide.get() call.
        Glide glide = Glide.get(context.getApplicationContext());
        applicationManager =
            factory.build(
                glide,
                new ApplicationLifecycle(),
                new EmptyRequestManagerTreeNode(),
                context.getApplicationContext());
      }
    }
  }

  return applicationManager;
}

这里又用到了RequestManagerRetriever类。 看这个类的注释 *创建新{@link com.bumptech.glide的静态方法集合。RequestManager}年代或 *从活动和片段中检索已有的。 应该是个工具类。

最后得到一个RequestManager对象

if (context instanceof FragmentActivity) {
  return get((FragmentActivity) context);
} else if (context instanceof Activity) {
  return get((Activity) context);
} else if (context instanceof ContextWrapper) {
  return get(((ContextWrapper) context).getBaseContext());
}
看上面的代码,对不同的activity进行不同的处理,
@NonNull
public RequestManager get(@NonNull FragmentActivity activity) {
  if (Util.isOnBackgroundThread()) {
    return get(activity.getApplicationContext());
  } else {
    assertNotDestroyed(activity);
    FragmentManager fm = activity.getSupportFragmentManager();
    return supportFragmentGet(activity, fm, null /*parentHint*/);
  }
}
@NonNull
public RequestManager get(@NonNull Fragment fragment) {
  Preconditions.checkNotNull(fragment.getActivity(),
        "You cannot start a load on a fragment before it is attached or after it is destroyed");
  if (Util.isOnBackgroundThread()) {
    return get(fragment.getActivity().getApplicationContext());
  } else {
    FragmentManager fm = fragment.getChildFragmentManager();
    return supportFragmentGet(fragment.getActivity(), fm, fragment);
  }
}

@NonNull
public RequestManager get(@NonNull Activity activity) {
  if (Util.isOnBackgroundThread()) {
    return get(activity.getApplicationContext());
  } else {
    assertNotDestroyed(activity);
    android.app.FragmentManager fm = activity.getFragmentManager();
    return fragmentGet(activity, fm, null /*parentHint*/);
  }
}

看上面的: get(FragmentActivity activity) get (Activity activity) 方法都会闲判断一下是否是后台线程 if (Util.isOnBackgroundThread()) 如果是后台线程最后会执行getApplicationManager(Context context)方法,也就是我们的我们上面最开始说的那个方法。否则,则都会创建一个fragment添加到活动中,也就是glide中的SupportRequestManagerFragment类,为什么要弄fragment绑定活动中,简答来看看SupportRequestManagerFragment的代码。

public class SupportRequestManagerFragment extends Fragment {


  /**
   * Returns true if the fragment is a descendant of our parent.
   */
  private boolean isDescendant(Fragment fragment) {
    Fragment root = this.getParentFragmentUsingHint();
    Fragment parentFragment;
    while ((parentFragment = fragment.getParentFragment()) != null) {
      if (parentFragment.equals(root)) {
        return true;
      }
      fragment = fragment.getParentFragment();
    }
    return false;
  }

  private void registerFragmentWithRoot(FragmentActivity activity) {
    unregisterFragmentWithRoot();
    rootRequestManagerFragment = Glide.get(activity).getRequestManagerRetriever()
        .getSupportRequestManagerFragment(activity.getSupportFragmentManager(), null);
    if (!this.equals(rootRequestManagerFragment)) {
      rootRequestManagerFragment.addChildRequestManagerFragment(this);
    }
  }

  private void unregisterFragmentWithRoot() {
    if (rootRequestManagerFragment != null) {
      rootRequestManagerFragment.removeChildRequestManagerFragment(this);
      rootRequestManagerFragment = null;
    }
  }

  @Override
  public void onAttach(Context context) {
    super.onAttach(context);
    try {
      registerFragmentWithRoot(getActivity());
    } catch (IllegalStateException e) {
      // OnAttach can be called after the activity is destroyed, see #497.
      if (Log.isLoggable(TAG, Log.WARN)) {
        Log.w(TAG, "Unable to register fragment with root", e);
      }
    }
  }

  @Override
  public void onDetach() {
    super.onDetach();
    parentFragmentHint = null;
    unregisterFragmentWithRoot();
  }

  @Override
  public void onStart() {
    super.onStart();
    lifecycle.onStart();
  }

  @Override
  public void onStop() {
    super.onStop();
    lifecycle.onStop();
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    lifecycle.onDestroy();
    unregisterFragmentWithRoot();
  }
}

上面的内容有删除,我们这里重点关注一下生命周期里面调用的相应的接口。对这里在监听faagment的生命周期,我们知道activity销毁的时候fragment也会销毁,那么我们监听fargment也就拿到了activity的生命周期,这样在activity销毁的时候我们就可以对图片资源加载进行相应的操作,比如当我们的activity销毁以后他的图片就i没必要再进行加载了,可以进行相应的停止。

Load方法

Load方法用来设置我们的url地址 调用的方法都在RequestBuilder类中的 我们可以看到load方法后调用了loadGeneric方法 最终把赋值给了model字段,然后更改了一个变量的状态,看这个变量名称意思应该是是否设置model,现在是为true。

public RequestBuilder<TranscodeType> load(@Nullable Object model) {
  return loadGeneric(model);
}

private RequestBuilder<TranscodeType> loadGeneric(@Nullable Object model) {
  this.model = model;
  isModelSet = true;
  return this;
}

into方法

into方法开始也是在RequestBuild类中开始调用 可以看到RequestBuild中有泛型,这泛型稍后再去理解。 这边into方法中传了一个imageview对象,根据注释我们可以了解到,当进行into方法的时候就说明开始启动了图片加载,如果这个view之前正在加载中则会取消之前的任务,重新开始任务。 进去into方法以后可以看到首先就行了一些裁剪方面的设置,判断完之后return时候调用的into方法才是重头戏。 Return调用的into方法有三个参数,第二是null,第三个是请求参数构造(请求的选项)第一个参数分析

public ViewTarget<ImageView, TranscodeType> into(@NonNull ImageView view) {
  Util.assertMainThread();
  Preconditions.checkNotNull(view);

  RequestOptions requestOptions = this.requestOptions;
  if (!requestOptions.isTransformationSet()
      && requestOptions.isTransformationAllowed()
      && view.getScaleType() != null) {
    // Clone in this method so that if we use this RequestBuilder to load into a View and then
    // into a different target, we don't retain the transformation applied based on the previous
    // View's scale type.
    switch (view.getScaleType()) {
      case CENTER_CROP:
        requestOptions = requestOptions.clone().optionalCenterCrop();
        break;
      case CENTER_INSIDE:
        requestOptions = requestOptions.clone().optionalCenterInside();
        break;
      case FIT_CENTER:
      case FIT_START:
      case FIT_END:
        requestOptions = requestOptions.clone().optionalFitCenter();
        break;
      case FIT_XY:
        requestOptions = requestOptions.clone().optionalCenterInside();
        break;
      case CENTER:
      case MATRIX:
      default:
        // Do nothing.
    }
  }

  return into(
      glideContext.buildImageViewTarget(view, transcodeClass),
      /*targetListener=*/ null,
      requestOptions);
}

分析一下glideContext.buildImageViewTarget()

@NonNull
public <X> ViewTarget<ImageView, X> buildImageViewTarget(
    @NonNull ImageView imageView, @NonNull Class<X> transcodeClass) {
  return imageViewTargetFactory.buildTarget(imageView, transcodeClass);
}

接着我们就来来到了ImageViewTargetFactory类,里面就这么一个方法bildTarget,来看看这个方法事干嘛的,很显然事返回图片的类型,这里有俩种Bitmap,Drawable。最后返回了一个ViewTaget对象。视图目标对象。

public <Z> ViewTarget<ImageView, Z> buildTarget(@NonNull ImageView view,
    @NonNull Class<Z> clazz) {
  if (Bitmap.class.equals(clazz)) {
    return (ViewTarget<ImageView, Z>) new BitmapImageViewTarget(view);
  } else if (Drawable.class.isAssignableFrom(clazz)) {
    return (ViewTarget<ImageView, Z>) new DrawableImageViewTarget(view);
  } else {
    throw new IllegalArgumentException(
        "Unhandled class: " + clazz + ", try .as*(Class).transcode(ResourceTranscoder)");
  }
}

分析完第一次参数以后接着走来到了下面的into方法:

private <Y extends Target<TranscodeType>> Y into(
    @NonNull Y target,
    @Nullable RequestListener<TranscodeType> targetListener,
    @NonNull RequestOptions options) {
  Util.assertMainThread();
  Preconditions.checkNotNull(target);
  if (!isModelSet) {
    throw new IllegalArgumentException("You must call #load() before calling #into()");
  }

  options = options.autoClone();
  Request request = buildRequest(target, targetListener, options);

  Request previous = target.getRequest();
  if (request.isEquivalentTo(previous)
      && !isSkipMemoryCacheWithCompletePreviousRequest(options, previous)) {
    request.recycle();
          if (!Preconditions.checkNotNull(previous).isRunning()) {
           previous.begin();
          }
    return target;
  }

  requestManager.clear(target);
  target.setRequest(request);
  requestManager.track(target, request);

  return target;
}

先看这个

判断

if (request.isEquivalentTo(previous)
    && !isSkipMemoryCacheWithCompletePreviousRequest(options, previous))

第一个request与previous是否相等,第二个 !options.isMemoryCacheable() && previous.isComplete() 第二个第一个是否内存缓存 第二个第二个previous请求是否完成,只有当不使用内存缓存以及previous请求完成的时候才能过。 进来了就先释放掉request资源,然后再检查previous是否为空,不为空是否在运行,如果不再运行则发起请求。 接着往下面走,先在requestManager管理中清除这个target目标,然后重新发起。 现在我们接着看requestManager.track()方法。

void track(Target<?> target, Request request) {
  targetTracker.track(target);
  requestTracker.runRequest(request);
}
接着看requestTracker.runRequest()方法。
public void runRequest(Request request) {
  requests.add(request);
  if (!isPaused) {
    request.begin();
  } else {
    pendingRequests.add(request);
  }
}

这个方法的注释也明确的告诉了我们 开始跟踪给定的请求。这里有一个参数isPauesd默认事为true,那么我们当启动app第一次进来的时候请求就不会在这里被执行,而是都添加集合中等待执行。这里俩个集合说明一下。 private final Set requests = Collections.newSetFromMap(new WeakHashMap<Request, Boolean>()); private final List pendingRequests = new ArrayList<>(); 第一个集合requests是请求集合 弱引用 第二个集合pendingRequests是待请求集合 强引用 第一个就是为了发送执行,第二个的作用就是为了保持引用,防止弱引用被回收。

那么上面我说了isPaused这个字段第一次运行默认是为false的,那么虽然你添加进去了,但是这里不会去执行操作,这个开始网络请求的操作在哪里开始执行呢,当我们第一次进来的时候,

public void resumeRequests() {
  isPaused = false;
  for (Request request : Util.getSnapshot(requests)) {
    if (!request.isComplete() && !request.isCancelled() && !request.isRunning()) {
      request.begin();
    }
  }
  pendingRequests.clear();
}

RequestManager:
public void onStart() {
  resumeRequests();
  targetTracker.onStart();
}

没错当我们第一进来的时候是在这里开始执行我们的网络请求的,当我们的生命周期走到了onStart的时候,就会把原本等待队列中的网络请求开始执行。这也对应了我们生命周期的方法,生命周期存在的时候发送网络请求,不然界面都没有你加载图片也没意义,那么当我们生命周期开始走到销毁的时候就会停止一切图片加载,界面都要销毁了你还加载图片干嘛,这不是浪费资源吗。这里对于怎么监听activity的生命周期上面也有介绍就是添加了一个fragment和activity绑定。 当第一次以后我们的ispaues会变成true状态,所以之后我们进行的图片加载会直接发送网络请求,不会进入到强引用队列中等待。

END

Glide的缓存机制刨析

我们自己都知道,如果你来写一个图片加载框架,肯定会想到做缓存减少不必要的网络请求,所以glide也不例外。

待续

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
Java日期时间API系列31
  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前
Stella981 Stella981
2年前
JS 苹果手机日期显示NaN问题
问题描述newDate("2019122910:30:00")在IOS下显示为NaN原因分析带的日期IOS下存在兼容问题解决方法字符串替换letdateStr"2019122910:30:00";datedateStr.repl
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
2年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
阿邹
阿邹
Lv1
想要忘记那么多过往偏偏清醒到荒唐
文章
8
粉丝
1
获赞
1