经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android高斯模糊实现方案
来源:cnblogs  作者:安卓笔记侠  时间:2019/10/25 8:32:03  对本文有异议
1、使用Glide
  1. Glide.with(this)
  2. .load(service.getImageUri())
  3. .dontAnimate()
  4. .error(R.drawable.error_img)
  5. // 设置高斯模糊
  6. .bitmapTransform(new BlurTransformation(this, 14, 3))
  7. .into(imageview);

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

 

2、对图片高斯模糊,需要先将图片转成bitmap对象
  1. mport android.annotation.TargetApi;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.os.Build;
  5. import android.renderscript.Allocation;
  6. import android.renderscript.Element;
  7. import android.renderscript.RenderScript;
  8. import android.renderscript.ScriptIntrinsicBlur;
  9. public class BlurBitmapUtil {
  10. // 图片缩放比例(即模糊度)
  11. private static final float BITMAP_SCALE = 0.4f;
  12. /**
  13. * @param context 上下文对象
  14. * @param image 需要模糊的图片
  15. * @return 模糊处理后的Bitmap
  16. */
  17. @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
  18. public static Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) {
  19. // 计算图片缩小后的长宽
  20. int width = Math.round(image.getWidth() * BITMAP_SCALE);
  21. int height = Math.round(image.getHeight() * BITMAP_SCALE);
  22. // 将缩小后的图片做为预渲染的图片
  23. Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
  24. // 创建一张渲染后的输出图片
  25. Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
  26. // 创建RenderScript内核对象
  27. RenderScript rs = RenderScript.create(context);
  28. // 创建一个模糊效果的RenderScript的工具对象
  29. ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
  30. // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间
  31. // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去
  32. Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
  33. Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
  34. // 设置渲染的模糊程度, 25f是最大模糊度
  35. blurScript.setRadius(blurRadius);
  36. // 设置blurScript对象的输入内存
  37. blurScript.setInput(tmpIn);
  38. // 将输出数据保存到输出内存中
  39. blurScript.forEach(tmpOut);
  40. // 将数据填充到Allocation中
  41. tmpOut.copyTo(outputBitmap);
  42. return outputBitmap;
  43. }
  44. }

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

 

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

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

  1. <com.github.mmin18.widget.RealtimeBlurView
  2. android:id="@+id/blurview"
  3. android:layout_width="match_parent"
  4. android:layout_height="210dp"
  5. android:visibility="gone"
  6. app:realtimeBlurRadius="5dp"
  7. app:realtimeOverlayColor="#00000000" />

 

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

 

 

原文链接:http://www.cnblogs.com/ganchuanpu/p/11731625.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号