Android拿來系列之萬能圖片處理方案
阿新 • • 發佈:2019-02-17
Glide
匯入依賴
//圓形 矩形圖片 邊框
compile 'cn.gavinliu.android.lib:ShapedImageView:0.8.5'
//Glide
compile 'com.github.bumptech.glide:glide:3.7.0'
// Glide 的 各種圖形載入無邊框
compile 'jp.wasabeef:glide-transformations:3.0.1'
CV三個類
/** * description: Glide清除快取工具類 * author: WDSG * date: 2017/3/27 */ public class GlideCacheUtil { private static GlideCacheUtil mInstance; private static Context mContext; public static GlideCacheUtil getInstance(Context context) { if (null == mInstance) { mInstance = new GlideCacheUtil(); mContext = context; } return mInstance; } public static void free() { mInstance = null; mContext = null; } // 獲取Glide磁碟快取大小 public String getCacheSize() { try { return getFormatSize(getFolderSize(new File(mContext.getCacheDir() + "/" + GlideConfig.GLIDE_CARCH_DIR))); } catch (Exception e) { e.printStackTrace(); return "獲取失敗"; } } // 清除Glide磁碟快取,自己獲取快取資料夾並刪除方法 public boolean cleanCatchDisk() { return deleteFolderFile(mContext.getCacheDir() + "/" + GlideConfig.GLIDE_CARCH_DIR, true); } // 清除圖片磁碟快取,呼叫Glide自帶方法 public boolean clearCacheDiskSelf() { try { if (Looper.myLooper() == Looper.getMainLooper()) { new Thread(new Runnable() { @Override public void run() { Glide.get(mContext).clearDiskCache(); } }).start(); } else { Glide.get(mContext).clearDiskCache(); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } // 清除Glide記憶體快取 public boolean clearCacheMemory() { try { if (Looper.myLooper() == Looper.getMainLooper()) { //只能在主執行緒執行 Glide.get(mContext).clearMemory(); return true; } } catch (Exception e) { e.printStackTrace(); } return false; } // 獲取指定資料夾內所有檔案大小的和 public 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; } // 格式化單位 public 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 boolean deleteFolderFile(String filePath, boolean deleteThisPath) { 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(); } } } return true; } catch (Exception e) { e.printStackTrace(); return false; } } }
public class GlideConfig { // 圖片快取最大容量,150M,根據自己的需求進行修改 public static final int GLIDE_CATCH_SIZE = 150 * 1000 * 1000; // 圖片快取子目錄 public static final String GLIDE_CARCH_DIR = "image_cache_app"; public static int getGlideCatchSize() { return GLIDE_CATCH_SIZE; } public static String getGlideCarchDir() { return GLIDE_CARCH_DIR; } }
public class GlideConfiguration implements GlideModule { //FIXME: 該配置類需要在AndroidManifest.xml中宣告 // <meta-data // android:name="lib.self.network.image.glide.GlideModule.GlideConfiguration" // android:value="GlideModule" /> @Override public void applyOptions(Context context, GlideBuilder builder) { //自定義Glide預設的Bitmap格式(載入圖片的質量) builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888); //自定義快取目錄 builder.setDiskCache(new InternalCacheDiskCacheFactory(context, GlideConfig.GLIDE_CARCH_DIR, //快取路徑 GlideConfig.GLIDE_CATCH_SIZE)); //快取大小 } @Override public void registerComponents(Context context, Glide glide, Registry registry) { } }
清單
<meta-data
android:name="com.as.demo7_4.GlideConfiguration"
android:value="GlideModule" />
圓形圓角帶邊框
<cn.gavinliu.android.lib.shapedimageview.ShapedImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="100dp"
android:scaleType="fitXY"
app:round_radius="10dp"
app:shape_mode="round_rect"
android:src="@mipmap/ic_launcher"
app:stroke_color="@color/colorAccent"
app:stroke_width="2dp" />
載入不同的形狀
//手動設定圖片尺寸
ImageView imageHuodong = mViewBinding.imageDuodong;
int screenWidth = ScreenUtils.getScreenWidth();
int screenHeight = screenWidth * 2459/ 790;
ViewGroup.LayoutParams layoutParams = imageHuodong.getLayoutParams();
layoutParams.width=screenWidth;
layoutParams.height=screenHeight;
Glide.with(this)
// .asBitmap()
// .asGif()
.load(imagegif)
// .apply(new RequestOptions().override(screenWidth,screenHeight))
.into(imageHuodong);
2.使用毛玻璃/高斯模糊
Blur 那個 倆個引數 //第一個引數 25 的第二個 縮放度 越大越 模糊 x分之1