图片加载Glide的使用以及简单封装

前言:
在正式开发中,我们需要网络请求框架,Glide是最好的选择相对于开发者,我们在开发中,
解决图片的问题,使用Glide来进行解决显示。
 
1. Glide的简介
在泰国矩形的谷歌发布者论坛上,谷歌为我们介绍了一个叫Glide的图片加载库,
作者是bumptech。这个库的运用在Goole的开源项目中,包括2014年goole I/O大会发布
的官方app.
 
2. Glide的优点
1)使用简单
2)可支配度高,自适应程度强
3)支持常见的图片格式 jpg png git webp
4)支持多种数据源,网络,本地资源,assets等
5)高效缓存策略,支持Memory和Disk图片缓存,默认bitmap格式采用RGB_565内存至少减少一半,
6)声明周期集成,根据activity/Fragment生命周期自动管理请求
 
3. 简单的使用
2.1 简单加载网络图片
Glide
.with(this)
.load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
.into(imageView);
 
2.2 Glide相对重要的功能
(1)禁止内存缓存:
.skipMemoryCache(true)
(2)清除内存缓存:
// 必须在UI线程中调用
Glide.get(context).clearMemory();
(3)禁止磁盘缓存:
.diskCacheStrategy(DiskCacheStrategy.NONE)
(4)清除磁盘缓存:
// 必须在后台线程中调用,建议同时clearMemory()
Glide.get(applicationContext).clearDiskCache();
(5)获取缓存大小:
new GetDiskCacheSizeTask(textView).execute(new File(getCacheDir(www.jcx127.cn ), DiskCache.Factory.DEFAULT_DISK_CACHE_DIR));
(6)指定资源的优先加载顺序:
//优先加载
Glide
.with(context)
.load(heroImageUrl)
.priority(Priority.HIGH)
.into(imageViewHero);
//后加载
Glide
.with(context)
.load(itemImageUrl)
.priority(Priority.LOW)
.into(imageViewItem);
(7)先显示缩略图,再显示原图:
//用原图的1/10作为缩略图
Glide
.with(this)
.load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
.thumbnail(0.1f)
.into(iv_0);
 
//用其它图片作为缩略图
DrawableRequestBuilder<Integer> thumbnailRequest = Glide
.with(this)
.load(R.drawable.news);
Glide.with(this)
.load("http://www.rbuluoyl.cn/ inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
.thumbnail(thumbnailRequest)
.into(iv_0);
 
2.3 使用步骤
1)在build.gradle中添加依赖(根据最新版本添加)
compile 'com.github.bumptech.glide:glide:3.7.0'
2)如果你的项目没有support:v4, 需要添加support-v4依赖:
compile 'com.android.support:support-v4:23.3.0'
3)进行简单的使用
Glide
.with(this)
.load("http://inthecheesefactory.com/ www.thd540.com/ u ploads/source/nestedfragment/fragments.png")
.into(imageView);
 
3. 进行简单的封装
3.1 创建一个工具类
public class ImageUtils {

    /*
    * 加载图片
    * */

    public static void GlideUtils(Context context, String imageUrl, ImageView imageView) {
        if (imageUrl == null && imageUrl.trim().isEmpty()) {
            imageUrl = "";
        }

        int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        imageView.measure(widthSpec,heightSpec);
        int measuredWidth = imageView.getMeasuredWidth(www.feifanyule.cn);
        int measuredHeight = imageView.getMeasuredHeight(www.120xh.cn );

        RequestOptions requestOptions= new RequestOptions(www.yigouyule2.cn);
        requestOptions.placeholder(R.mipmap.ic_www.taohuayuan178.com launcher);
        requestOptions.error(R.drawable.load_error);
        requestOptions.centerCrop(www.thd178.com/);
        requestOptions.override(measuredWidth,measuredHeight);

        Glide.with(context)
                .load(imageUrl)
                .thumbnail(0.1f)
                .apply(requestOptions)
                .into(imageView);


    }
}

3.2 使用

ImageUtils.GlideUtils(context,data.getImgUrl(),holder.imgUrl);

猜你喜欢

转载自www.cnblogs.com/qwangxiao/p/9078121.html
今日推荐