图片加载库Glide的封装工具类,方便以后使用
直接上源码。注释得已经很清晰了,直接调用即可。
package com.liuguilin.lovewallpaper.utils;
/*
* Created by 火龙裸先生 on 2017/3/3 0003.
*/ import android.content.Context;
import android.widget.ImageView; import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.SimpleTarget; public class GlideUtils { /**
* Glide特点
* 使用简单
* 可配置度高,自适应程度高
* 支持常见图片格式 Jpg png gif webp
* 支持多种数据源 网络、本地、资源、Assets 等
* 高效缓存策略 支持Memory和Disk图片缓存 默认Bitmap格式采用RGB_565内存使用至少减少一半
* 生命周期集成 根据Activity/Fragment生命周期自动管理请求
* 高效处理Bitmap 使用Bitmap Pool使Bitmap复用,主动调用recycle回收需要回收的Bitmap,减小系统回收压力
* 这里默认支持Context,Glide支持Context,Activity,Fragment,FragmentActivity
*/ //默认加载
public static void loadImageView(Context mContext, String path, ImageView mImageView) {
Glide.with(mContext).load(path).into(mImageView);
} //加载指定大小
public static void loadImageViewSize(Context mContext, String path, int width, int height, ImageView mImageView) {
Glide.with(mContext).load(path).override(width, height).into(mImageView);
} //填充
public static void loadImageCrop(Context mContext, String path, ImageView mImageView) {
Glide.with(mContext).load(path).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).into(mImageView);
} //设置加载中以及加载失败图片
public static void loadImageViewLoding(Context mContext, String path, ImageView mImageView, int lodingImage, int errorImageView) {
Glide.with(mContext).load(path).placeholder(lodingImage).error(errorImageView).into(mImageView);
} //设置加载中以及加载失败图片并且指定大小
public static void loadImageViewLodingSize(Context mContext, String path, int width, int height, ImageView mImageView, int lodingImage, int errorImageView) {
Glide.with(mContext).load(path).override(width, height).placeholder(lodingImage).error(errorImageView).into(mImageView);
} //设置跳过内存缓存
public static void loadImageViewCache(Context mContext, String path, ImageView mImageView) {
Glide.with(mContext).load(path).skipMemoryCache(true).into(mImageView);
} //设置下载优先级
public static void loadImageViewPriority(Context mContext, String path, ImageView mImageView) {
Glide.with(mContext).load(path).priority(Priority.NORMAL).into(mImageView);
} /**
* 策略解说:
* <p>
* all:缓存源资源和转换后的资源
* <p>
* none:不作任何磁盘缓存
* <p>
* source:缓存源资源
* <p>
* result:缓存转换后的资源
*/ //设置缓存策略
public static void loadImageViewDiskCache(Context mContext, String path, ImageView mImageView) {
Glide.with(mContext).load(path).diskCacheStrategy(DiskCacheStrategy.ALL).into(mImageView);
} /**
* api也提供了几个常用的动画:比如crossFade()
*/ //设置加载动画
public static void loadImageViewAnim(Context mContext, String path, int anim, ImageView mImageView) {
Glide.with(mContext).load(path).animate(anim).into(mImageView);
} /**
* 会先加载缩略图
*/ //设置缩略图支持
public static void loadImageViewThumbnail(Context mContext, String path, ImageView mImageView) {
Glide.with(mContext).load(path).thumbnail(0.1f).into(mImageView);
} /**
* api提供了比如:centerCrop()、fitCenter()等
*/ //设置动态转换
public static void loadImageViewCrop(Context mContext, String path, ImageView mImageView) {
Glide.with(mContext).load(path).centerCrop().into(mImageView);
} //设置动态GIF加载方式
public static void loadImageViewDynamicGif(Context mContext, String path, ImageView mImageView) {
Glide.with(mContext).load(path).asGif().into(mImageView);
} //设置静态GIF加载方式
public static void loadImageViewStaticGif(Context mContext, String path, ImageView mImageView) {
Glide.with(mContext).load(path).asBitmap().into(mImageView);
} //设置监听的用处 可以用于监控请求发生错误来源,以及图片来源 是内存还是磁盘 //设置监听请求接口
public static void loadImageViewListener(Context mContext, String path, ImageView mImageView, RequestListener<String, GlideDrawable> requstlistener) {
Glide.with(mContext).load(path).listener(requstlistener).into(mImageView);
} //项目中有很多需要先下载图片然后再做一些合成的功能,比如项目中出现的图文混排 //设置要加载的内容
public static void loadImageViewContent(Context mContext, String path, SimpleTarget<GlideDrawable> simpleTarget) {
Glide.with(mContext).load(path).centerCrop().into(simpleTarget);
} //清理磁盘缓存
public static void GuideClearDiskCache(Context mContext) {
//理磁盘缓存 需要在子线程中执行
Glide.get(mContext).clearDiskCache();
} //清理内存缓存
public static void GuideClearMemory(Context mContext) {
//清理内存缓存 可以在UI主线程中进行
Glide.get(mContext).clearMemory();
}
}
图片加载库Glide的封装工具类,方便以后使用的更多相关文章
- Google图片加载库Glide的简单封装GlideUtils
Google图片加载库Glide的简单封装GlideUtils 因为项目里用的Glide的地方比较多,所有简单的封装了以下,其实也没什么,就是写了个工具类,但是还是要把基础说下 Glide的Githu ...
- Android 图片加载库Glide 实战(二),占位符,缓存,转换自签名高级实战
http://blog.csdn.net/sk719887916/article/details/40073747 请尊重原创 : skay <Android 图片加载库Glide 实战(一), ...
- android图片加载库Glide
什么是Glide? Glide是一个加载图片的库,作者是bumptech,它是在泰国举行的google 开发者论坛上google为我们介绍的,这个库被广泛的运用在google的开源项目中. Glide ...
- android 图片加载库 Glide 的使用介绍
一:简介 在泰国举行的谷歌开发者论坛上,谷歌为我们介绍了一个名叫 Glide 的图片加载库,作者是bumptech.这个库被广泛的运用在google的开源项目中,包括2014年google I/O大会 ...
- Google推荐的图片加载库Glide介绍
英文原文 Introduction to Glide, Image Loader Library for Android, recommended by Google 译文首发 http://jco ...
- Google推荐的图片加载库Glide
英文原文 Introduction to Glide, Image Loader Library for Android, recommended by Google 首发地址 http://jco ...
- Aandroid 图片加载库Glide 实战(一),初始,加载进阶到实践
原文: http://blog.csdn.net/sk719887916/article/details/39989293 skay 初识Glide 为何使用 Glide? 有经验的 Android ...
- Android图片加载库的理解
前言 这是“基础自测”系列的第三篇文章,以Android开发需要熟悉的20个技术点为切入点,本篇重点讲讲Android中的ImageLoader这个库的一些理解,在Android上最让人头疼是 ...
- fackbook的Fresco (FaceBook推出的Android图片加载库-Fresco)
[Android开发经验]FaceBook推出的Android图片加载库-Fresco 欢迎关注ndroid-tech-frontier开源项目,定期翻译国外Android优质的技术.开源库.软件 ...
随机推荐
- 使用git时出现Please make sure you have the correct access rights and the repository exists.问题已解决。
使用git时,出现Please make sure you have the correct access rights and the repository exists.问题已解决. 今天我在使用 ...
- GDOI2018滚粗记
day-50: 高中全体成员去了北京训练,我被虐成傻逼(貌似总分全校倒数第2). day-20: 回广州了,间断式略微考好55555..... day0: 早上起床好像有点晚qwq 然后简单打了个FF ...
- HTTP协议及WWW服务应用
一.用户访问网站的流程图 二.DNS解析的流程图 三.用户访问网站的基本流程原理阐述 ① 用户在浏览器中输入请求的地址回车 ② 先找本地的缓存和Hosts文件,有解析的对应IP直接返回个客户端IP地址 ...
- _new_()与_init_()的区别
先上代码 其中,__new__()不是一定要有,只有继承自object的类才有,该方法可以return父类(通过super(当前类名, cls).__new__())出来的实例,或者直接是obje ...
- (转)Python科学计算之Pandas详解,pythonpandas
https://www.cnblogs.com/linux-wangkun/p/5903380.html-------pandas 学习(1): pandas 数据结构之Series https:// ...
- 渐进增强与优雅降级 && css3中普通属性和前缀属性的书写顺序
什么是渐进增强与优雅降级? 服务器和浏览器是不同的.当服务器有新版本时,开发人员直接使用新版本的服务器提供服务即可:但是浏览器端,不同的用户使用的浏览器版本不同,型号差异大,我们不可能让用户强制更新 ...
- 部分替换mysql表中某列的字段
UPDATE `table_name` SET `field_name` = replace (`field_name`,'from_str','to_str') WHERE `field_name` ...
- 正则中str.match(pattern)与pattern.exec(str)的区别
这两个函数除了调用对象以及参数不同之外,<javascript高级程序设计>中对exec描述比较详细,对match只是说返回数组跟exec一样.书中并没有说只说了正则在非全局模式下的情况, ...
- 【数组】Find Minimum in Rotated Sorted Array
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- CSS Sprites(CSS精灵) 的优缺点
CSS Sprites 的优点: 1.减少图片的字节 2.减少了网页的http请求,从而大大的提高了页面的性能 3.解决了网页设计师在图片命名上的困扰,只需对一张集合的图片上命 ...