美文网首页
Glide源码解析

Glide源码解析

作者: 01_小小鱼_01 | 来源:发表于2018-07-12 22:34 被阅读35次

    在Android应用开发过程中,关于图片加载框架有很多:Glide、Picasso、Fresco、ImageLoader。个人觉得比较好用的还是Glide框架,一行代码就可以搞定图片加载,使用简单,链式调用优雅简洁,且支持Gif、SVG格式,和Activity/Fragment 生命周期相绑定。

    一、用法简洁
    // 依赖
    dependencies {  
        compile 'com.github.bumptech.glide:glide:3.7.0'  
        compile 'com.android.support:support-v4:23.2.1'  
    }  
    // 用法
    Glide.with(context).load(imageUrl).placeholder(phDrawable)
         .thumbnail(0.2f).into(imageView);
    
    二、源码解析
    public static RequestManager with(Context context) {
        RequestManagerRetriever retriever = RequestManagerRetriever.get();
        return retriever.get(context);
    }
    
    public static RequestManager with(Activity activity) {
        RequestManagerRetriever retriever = RequestManagerRetriever.get();
        return retriever.get(activity);
    }
    

    接下来在RequestManagerRetriever类的静态方法get

    private static final RequestManagerRetriever INSTANCE = 
                    new RequestManagerRetriever();
    
    // 公有静态方法实现单例模式
    public static RequestManagerRetriever get() {
        return INSTANCE;
    }
    
    // 0. 参数为 Application
    public RequestManager get(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);
    }
    
    /* Glide加载图片的生命周期和应用的生命周期一致,当退出应用时,图片停止加载。
     **  在getApplicationManager方法中,就是直接创建了一个RequestManager并返回,
     ** 在构建RequestManager对象时,传入了ApplicationLifecycle对象
     */
    private RequestManager getApplicationManager(Context context) {
        // Either an application context or we're on a background thread.
        if (applicationManager == null) {
            synchronized (this) {
                if (applicationManager == null) {
                    applicationManager = new RequestManager(
                      context.getApplicationContext(),
                      new ApplicationLifecycle(), new EmptyRequestManagerTreeNode());
                }
            }
        }
    
        return applicationManager;
    }
    
    // 1. 参数为Fragment
    public RequestManager get(Fragment fragment) {
        if (fragment.getActivity() == null) {
            throw new IllegalArgumentException("You cannot start a load on a fragment 
                                                before it is attached");
        }
        if (Util.isOnBackgroundThread()) {
            return get(fragment.getActivity().getApplicationContext());
        } else {
            FragmentManager fm = fragment.getChildFragmentManager();
            return supportFragmentGet(fragment.getActivity(), fm);
        }
    }
    
    RequestManager supportFragmentGet(Context context, FragmentManager fm) {
        SupportRequestManagerFragment current = getSupportRequestManagerFragment(fm);
        RequestManager requestManager = current.getRequestManager();
        if (requestManager == null) {
            requestManager = new RequestManager(context, 
            current.getLifecycle(), current.getRequestManagerTreeNode());
            current.setRequestManager(requestManager);
        }
        return requestManager;
    }
    
    // 2. 参数为Activity
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public RequestManager get(Activity activity) {
        if (Util.isOnBackgroundThread() 
            || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            return get(activity.getApplicationContext());
        } else {
            assertNotDestroyed(activity);
            android.app.FragmentManager fm = activity.getFragmentManager();
            return fragmentGet(activity, fm);
        }
    }
    
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    RequestManager fragmentGet(Context context, android.app.FragmentManager fm) {
        RequestManagerFragment current = getRequestManagerFragment(fm);
        RequestManager requestManager = current.getRequestManager();
        if (requestManager == null) {
            requestManager = new RequestManager(context, 
            current.getLifecycle(), current.getRequestManagerTreeNode());
            current.setRequestManager(requestManager);
        }
        return requestManager;
    }
    

    至此,with方法就介绍结束了,明天接着写,未完待续。。

    相关文章

      网友评论

          本文标题:Glide源码解析

          本文链接:https://www.haomeiwen.com/subject/dsqdpftx.html