浅析Android高斯模糊实现方案

浅析Android高斯模糊实现方案,第1张

概述浅析Android高斯模糊实现方案 1.使用Glide Glide.with(this) .load(service.getImageUri()) .dontAnimate() .error(R.drawable.error_img) // 设置高斯模糊 .bitmapTransform(new BlurTransformation(this, 14, 3)) .into(imageview); 适用场景:动态配置的背景图片 2.对图片高斯模糊,需要先将图片转成bitmap对象 mport android.annotation.T

1、使用GlIDe

GlIDe.with(this)     .load(service.getimageUri())     .dontAnimate()     .error(R.drawable.error_img)     // 设置高斯模糊     .bitmaptransform(new Blurtransformation(this,14,3))     .into(imagevIEw);

适用场景:动态配置的背景图片

2、对图片高斯模糊,需要先将图片转成bitmap对象

mport androID.annotation.TargetAPI;import androID.content.Context;import androID.graphics.Bitmap;import androID.os.Build;import androID.renderscript.Allocation;import androID.renderscript.Element;import androID.renderscript.RenderScript;import androID.renderscript.ScriptIntrinsicBlur;public class BlurBitmapUtil { // 图片缩放比例(即模糊度) private static final float BITMAP_SCALE = 0.4f; /**  * @param context 上下文对象  * @param image 需要模糊的图片  * @return 模糊处理后的Bitmap  */ @TargetAPI(Build.VERSION_CODES.JELLY_BEAN_MR1) public static Bitmap blurBitmap(Context context,Bitmap image,float blurRadius) {  // 计算图片缩小后的长宽  int wIDth = Math.round(image.getWIDth() * BITMAP_SCALE);  int height = Math.round(image.getHeight() * BITMAP_SCALE);  // 将缩小后的图片做为预渲染的图片  Bitmap inputBitmap = Bitmap.createScaledBitmap(image,wIDth,height,false);  // 创建一张渲染后的输出图片  Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);  // 创建RenderScript内核对象  RenderScript rs = RenderScript.create(context);  // 创建一个模糊效果的RenderScript的工具对象  ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs,Element.U8_4(rs));  // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间  // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去  Allocation tmpIn = Allocation.createFromBitmap(rs,inputBitmap);  Allocation tmpOut = Allocation.createFromBitmap(rs,outputBitmap);  // 设置渲染的模糊程度,25f是最大模糊度  blurScript.seTradius(blurRadius);  // 设置blurScript对象的输入内存  blurScript.setinput(tmpIn);  // 将输出数据保存到输出内存中  blurScript.forEach(tmpOut);  // 将数据填充到Allocation中  tmpOut.copyTo(outputBitmap);  return outputBitmap; }}

不推荐:使用bitmap,频繁 *** 作的话比较耗性能。

3、使用高斯模糊遮罩,可以对指定区域进行模糊,不需要处理单张图片(推荐!!)

推荐一个github上的项目,亲测有效。https://github.com/mmin18/RealtimeBlurView

<com.github.mmin18.Widget.RealtimeBlurVIEw      androID:ID="@+ID/blurvIEw"      androID:layout_wIDth="match_parent"      androID:layout_height="210dp"      androID:visibility="gone"      app:realtimeBlurRadius="5dp"      app:realtimeOverlaycolor="#00000000" />

app:realtimeOverlaycolor="#00000000",这里设置成透明色,效果就如同直接对图片进行高斯模糊。

总结

以上所述是小编给大家介绍的AndroID高斯模糊实现方案,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

总结

以上是内存溢出为你收集整理的浅析Android高斯模糊实现方案全部内容,希望文章能够帮你解决浅析Android高斯模糊实现方案所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1144346.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存