您好,登錄后才能下訂單哦!
利用Glide怎么實現一個高斯模糊效果?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
第一步:引入依賴
implementation 'com.github.bumptech.glide:glide:4.7.1' implementation 'jp.wasabeef:glide-transformations:2.0.1'
第二步:自定義繼承自BitmapTransformation的類
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 androidx.annotation.NonNull; import com.bumptech.glide.Glide; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; import java.nio.charset.Charset; import java.security.MessageDigest; import jp.wasabeef.glide.transformations.internal.FastBlur; import jp.wasabeef.glide.transformations.internal.RSBlur; /** * 虛化Transformation * 更多效果參考:https://github.com/wasabeef/glide-transformations */ public class BlurTransformation extends BitmapTransformation { private static String STRING_CHARSET_NAME = "UTF-8"; private static final String ID = "com.kevin.glidetest.BlurTransformation"; private static Charset CHARSET = Charset.forName(STRING_CHARSET_NAME); private static final byte[] ID_BYTES = ID.getBytes(CHARSET); 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 protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { Bitmap source = toTransform ; 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); return bitmap; } @Override public int hashCode() { return ID.hashCode(); } @Override public boolean equals(Object obj) { return obj instanceof BlurTransformation; } @Override public void updateDiskCacheKey(MessageDigest messageDigest) { messageDigest.update(ID_BYTES); } }
第三步:實際使用
String url="url"; Glide.with(this).load(url) .apply(RequestOptions.bitmapTransform(new BlurTransformation(this,25,3))) .into(img); //參數用來設置模糊程度
關于利用Glide怎么實現一個高斯模糊效果問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。