1,使用這個框架快兩年了,今天去github上去看了一下,貌似已經從3.X升級到4.X了,想着本身尚未對這個框架在博客上作過總結,因此這裏打算出三篇博客來介紹,內容有基本使用、3.X與4.X的不通、封裝、到最後的源碼解析,因此今天從最簡單的基本使用開始,廢話很少說,開魯開魯。。。java
2,基本使用android
添加依賴git
compile 'com.github.bumptech.glide:glide:3.7.0'
① 簡單的展現圖片github
Glide.with(this) .load(url) .into(iv);
② 添加佔位符和加載動畫express
這裏的佔位符我只是簡單的寫了個Drawable,對應的方法是placeholder,而動畫是直接使用Glide自帶的淡入淡出crossFade效果,apache
<?xml version="1.0" encoding="utf-8"?> <inset xmlns:android="http://schemas.android.com/apk/res/android"> <shape android:shape="rectangle"> <gradient android:angle="0" android:endColor="@android:color/holo_blue_bright" android:startColor="@android:color/holo_blue_bright" /> <!--<stroke android:color="#000000" android:width="1dp" />--> </shape> </inset>
Glide.with(this) .load(url) .placeholder(R.drawable.loading_background) .crossFade(3000) .error(R.mipmap.ic_launcher) .into(iv);
效果以下:canvas
③ 展現本地圖片或者gif圖片緩存
這裏的展現本地圖片就很簡單了,把load裏面的地址換成咱們對應的本地地址就行,很簡單。而Gldie相對於Picasso和ImageLoad有不一樣的是Glide能展現Gif圖片而這兩個框架不行,而gif展現很簡單,和其它的正常圖片展現同樣網絡
Glide.with(this) .load(url2) .error(R.mipmap.ic_launcher) .into(iv);
若是想將該gif當作bitmap來加載,則只須要加上asBitmap()方法,若是想判斷該圖片是否爲gif圖片則可使用asGif()來判斷,若是不是的話會展現.error()裏面的圖片app
Glide.with(this) .load(url2) .asGif() .placeholder(R.drawable.loading_background) .crossFade(3000) // .asBitmap() .error(R.mipmap.ic_launcher) .into(iv);
效果以下:
④ 加載圓形圖片
這裏要使用bitmapTransform(bitmapTransform)方法,方法裏面對象是自定義的TransFormation。
CropCircleTransformation.java
package jp.wasabeef.glide.transformations; /** * Copyright (C) 2017 Wasabeef * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import com.bumptech.glide.Glide; import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapResource; public class CropCircleTransformation implements Transformation<Bitmap> { private BitmapPool mBitmapPool; public CropCircleTransformation(Context context) { this(Glide.get(context).getBitmapPool()); } public CropCircleTransformation(BitmapPool pool) { this.mBitmapPool = pool; } @Override public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) { Bitmap source = resource.get(); int size = Math.min(source.getWidth(), source.getHeight()); int width = (source.getWidth() - size) / 2; int height = (source.getHeight() - size) / 2; Bitmap bitmap = mBitmapPool.get(size, size, Bitmap.Config.ARGB_8888); if (bitmap == null) { bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); BitmapShader shader = new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); if (width != 0 || height != 0) { // source isn't square, move viewport to center Matrix matrix = new Matrix(); matrix.setTranslate(-width, -height); shader.setLocalMatrix(matrix); } paint.setShader(shader); paint.setAntiAlias(true); float r = size / 2f; canvas.drawCircle(r, r, r, paint); return BitmapResource.obtain(bitmap, mBitmapPool); } @Override public String getId() { return "CropCircleTransformation()"; } }
使用
Glide.with(this) .load(url) .bitmapTransform(new CropCircleTransformation(this)) .into(iv);
效果以下:
⑤ 添加虛化效果
編寫類BlurTransformation.java
package jp.wasabeef.glide.transformations; /** * Copyright (C) 2017 Wasabeef * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.os.Build; import android.renderscript.RSRuntimeException; import com.bumptech.glide.Glide; import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapResource; import jp.wasabeef.glide.transformations.internal.FastBlur; import jp.wasabeef.glide.transformations.internal.RSBlur; public class BlurTransformation implements Transformation<Bitmap> { private static int MAX_RADIUS = 25; private static int DEFAULT_DOWN_SAMPLING = 1; private Context mContext; private BitmapPool mBitmapPool; private int mRadius; private int mSampling; public BlurTransformation(Context context) { this(context, Glide.get(context).getBitmapPool(), MAX_RADIUS, DEFAULT_DOWN_SAMPLING); } public BlurTransformation(Context context, BitmapPool pool) { this(context, pool, MAX_RADIUS, DEFAULT_DOWN_SAMPLING); } public BlurTransformation(Context context, BitmapPool pool, int radius) { this(context, pool, radius, DEFAULT_DOWN_SAMPLING); } public BlurTransformation(Context context, int radius) { this(context, Glide.get(context).getBitmapPool(), radius, DEFAULT_DOWN_SAMPLING); } public BlurTransformation(Context context, int radius, int sampling) { this(context, Glide.get(context).getBitmapPool(), radius, sampling); } public BlurTransformation(Context context, BitmapPool pool, int radius, int sampling) { mContext = context.getApplicationContext(); mBitmapPool = pool; mRadius = radius; mSampling = sampling; } @Override public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) { Bitmap source = resource.get(); int width = source.getWidth(); int height = source.getHeight(); int scaledWidth = width / mSampling; int scaledHeight = height / mSampling; Bitmap bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888); if (bitmap == null) { bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); canvas.scale(1 / (float) mSampling, 1 / (float) mSampling); Paint paint = new Paint(); paint.setFlags(Paint.FILTER_BITMAP_FLAG); canvas.drawBitmap(source, 0, 0, paint); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { try { bitmap = RSBlur.blur(mContext, bitmap, mRadius); } catch (RSRuntimeException e) { bitmap = FastBlur.blur(bitmap, mRadius, true); } } else { bitmap = FastBlur.blur(bitmap, mRadius, true); } return BitmapResource.obtain(bitmap, mBitmapPool); } @Override public String getId() { return "BlurTransformation(radius=" + mRadius + ", sampling=" + mSampling + ")"; } }
使用
Glide.with(this) .load(url) .bitmapTransform(new BlurTransformation(this,10)) .into(iv);
效果以下:
以上是咱們的一些經常使用效果,還有一些其餘的thumbnail() 縮略圖、override()處理圖
3. 緩存機制的介紹
和常見的三級緩存同樣,Glide的緩存機制有三種 Glide的緩存書序也是 內存 -- 磁盤 -- 網絡
首先了解一下內存緩存,和內存掛鉤的方法就是咱們的skipMemoryCache()
Glide.with(this) .load(url2) .skipMemoryCache(true) .into(iv);
表示是否跳過磁盤緩存,默認的是false
磁盤緩存
Glide的磁盤緩存爲四種,默認的是result 至於什麼是 (有個方法是override方式 意思說是例如 這張圖在網上的寬度和高度是 300px*300px 而實際上在咱們的移動端ui給咱們的效果圖是300*150,這時候咱們就可使用這個方法
,將300*300的網絡原圖處理成300*150 )
①.ALL:緩存原圖(SOURCE)和處理圖(RESULT)
②.NONE:什麼都不緩存
③.SOURCE:只緩存原圖(SOURCE)
④.RESULT:只緩存處理圖(RESULT) —默認值
Glide.with(this)
.load(url2)
.override(300,150)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(iv);
磁盤緩存目錄 項目/cache/image_manager_disk_cache
關於如何清理緩存內容
//清除內存緩存 Glide.get(this).clearMemory(); //清除磁盤緩存 (要在子線程中進行) Glide.get(MainActivity.this).clearDiskCache();
關於清除單個圖片的緩存
關於清除單個圖片緩存 因爲Glide可能會緩存一張圖片的多個分辨率的圖片,而且文件名是被哈希過的,因此並不能很好的刪除單個資源的緩存 谷歌官方解釋 Because File names are hashed keys, there is no good way to simply delete all of the cached files on disk that correspond to a particular url or file path. The problem would be simpler if you were only ever allowed to load or cache the original image, but since Glide also caches thumbnails and provides various transformations, each of which will result in a new File in the cache, tracking down and deleting every cached version of an image is difficult. In practice, the best way to invalidate a cache file is to change your identifier when the content changes (url, uri, file path etc).
今天沒狀態,很久沒寫博客了,狀態回來不,感受本身頭腦愈來愈懶了,改天找回狀態接着寫