栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

浅析Android高斯模糊实现方案

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

浅析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.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;
 
 @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

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

总结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/154724.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号