日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2023/12/15 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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);

總結

以上是生活随笔為你收集整理的android 图片缓存工具类,Android工具类系列-Glide图片缓存与圆角的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。