android 图片缓存工具类,Android工具类系列-Glide图片缓存与圆角

Glide的图片缓存和清除图片缓存

public class GlideCacheUtil {

private static GlideCacheUtil inst;

public static GlideCacheUtil getInstance() {

if (inst == null) {

inst = new GlideCacheUtil();

}

return inst;

}

/**

* 清除图片磁盘缓存

*/

public void clearImageDiskCache(final Context context) {

try {

if (Looper.myLooper() == Looper.getMainLooper()) {

new Thread(new Runnable() {

@Override

public void run() {

Glide.get(context).clearDiskCache();

// BusUtil.getBus().post(new GlideCacheClearSuccessEvent());

}

}).start();

} else {

Glide.get(context).clearDiskCache();

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 清除图片内存缓存

*/

public void clearImageMemoryCache(Context context) {

try {

if (Looper.myLooper() == Looper.getMainLooper()) { //只能在主线程执行

Glide.get(context).clearMemory();

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 清除图片所有缓存

*/

public void clearImageAllCache(Context context) {

clearImageDiskCache(context);

clearImageMemoryCache(context);

String ImageExternalCatchDir=context.getExternalCacheDir()+ ExternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR;

deleteFolderFile(ImageExternalCatchDir, true);

}

/**

* 获取Glide造成的缓存大小

*

* @return CacheSize

*/

public String getCacheSize(Context context) {

try {

return getFormatSize(getFolderSize(new File(context.getCacheDir() + "/"+ InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR)));

} catch (Exception e) {

e.printStackTrace();

}

return "";

}

/**

* 获取指定文件夹内所有文件大小的和

*

* @param file file

* @return size

* @throws Exception

*/

private long getFolderSize(File file) throws Exception {

long size = 0;

try {

File[] fileList = file.listFiles();

for (File aFileList : fileList) {

if (aFileList.isDirectory()) {

size = size + getFolderSize(aFileList);

} else {

size = size + aFileList.length();

}

}

} catch (Exception e) {

e.printStackTrace();

}

return size;

}

/**

* 删除指定目录下的文件,这里用于缓存的删除

*

* @param filePath filePath

* @param deleteThisPath deleteThisPath

*/

private void deleteFolderFile(String filePath, boolean deleteThisPath) {

if (!TextUtils.isEmpty(filePath)) {

try {

File file = new File(filePath);

if (file.isDirectory()) {

File files[] = file.listFiles();

for (File file1 : files) {

deleteFolderFile(file1.getAbsolutePath(), true);

}

}

if (deleteThisPath) {

if (!file.isDirectory()) {

file.delete();

} else {

if (file.listFiles().length == 0) {

file.delete();

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

/**

* 格式化单位

*

* @param size size

* @return size

*/

private static String getFormatSize(double size) {

double kiloByte = size / 1024;

if (kiloByte < 1) {

return size + "Byte";

}

double megaByte = kiloByte / 1024;

if (megaByte < 1) {

BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));

return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";

}

double gigaByte = megaByte / 1024;

if (gigaByte < 1) {

BigDecimal result2 = new BigDecimal(Double.toString(megaByte));

return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";

}

double teraBytes = gigaByte / 1024;

if (teraBytes < 1) {

BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));

return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";

}

BigDecimal result4 = new BigDecimal(teraBytes);

return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";

}

}

圆形图片工具类

public class GlideCircleTransform extends BitmapTransformation {

public GlideCircleTransform(Context context) {

super(context);

}

@Override

protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {

return circleCrop(pool, toTransform);

}

private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {

if (source == null) return null;

int size = Math.min(source.getWidth(), source.getHeight());

int x = (source.getWidth() - size) / 2;

int y = (source.getHeight() - size) / 2;

// TODO this could be acquired from the pool too

Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);

Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);

if (result == null) {

result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);

}

Canvas canvas = new Canvas(result);

Paint paint = new Paint();

paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));

paint.setAntiAlias(true);

float r = size / 2f;

canvas.drawCircle(r, r, r, paint);

return result;

}

@Override

public String getId() {

return getClass().getName();

}

}

使用

.transform(new GlideCircleTransform(context))

网络链接失败加载图片

.error(R.mipmap.ic_launcher)

Glide.with(context).load(desc.get(position).getUserImage()).transform(new GlideCircleTransform(context)).error(R.mipmap.ic_launcher).into(holder.imgHomelv);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值