时间:2021-05-19
使用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); //参数用来设置模糊程度以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
在前几天写过一个使用glide-transformations的方法实现高斯模糊的方法,今天偶然间有发现一个大神写的另一个方法,感觉挺不错的,分享一下:效果图:
核心需要高斯模糊的库compile'jp.wasabeef:glide-transformations:2.0.1'针对于3.7的版本使用方法为://加载背景,
本文实例为大家分享了Unityshader实现高斯模糊效果的具体代码,供大家参考,具体内容如下正常图:高斯模糊效果图:shader代码:Shader"Custo
premiere在为视频添加了高斯模糊效果之后,我们知道默认都是整个视频画面会产生高斯模糊的效果,如果现在我只希望在垂直方向上呈现出高斯模糊的效果的话,该如何来
一、效果演示项目中用到了高斯模糊效果,查阅过一些资料,考虑到性能问题最终还是选择使用Android自带的RenderScript库来实现,关于使用RenderS