Android RenderScript 简单高效实现图片的高斯模糊效果
来源:互联网 发布:战龙三国四灵进阶数据 编辑:程序博客网 时间:2024/05/29 16:05
高斯模糊(Gaussian blur)和毛玻璃效果(亦称磨砂效果),近两年在移动端的UI设计上越来越流行,特别是iOS手机上出现的较多,iOS系统也提供了相应的API帮助开发人员分分钟实现这两个效果。而Android系统则经历了一个漫长的探索过程,对图片的处理,从Java算法到NDK方式实现等,各种摸索层出不穷。
值得欣慰的是,Google终于在API 11中引入了RenderScript,一个强大的图片处理框架,帮助Android开发人员专注于图片处理算法而不是API的调度工作。使用RenderScript进行图片处理,还需要了解RenderScript Intrinsics,一些可以帮助RenderScript快速实现各种图片处理的操作类。比如ScriptIntrinsicBlur,可以简单高效地帮助我们实现高斯模糊效果:
public Bitmap blurBitmap(Bitmap bitmap){ //Let's create an empty bitmap with the same size of the bitmap we want to blur Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); //Instantiate a new Renderscript RenderScript rs = RenderScript.create(getApplicationContext()); //Create an Intrinsic Blur Script using the Renderscript ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps Allocation allIn = Allocation.createFromBitmap(rs, bitmap); Allocation allOut = Allocation.createFromBitmap(rs, outBitmap); //Set the radius of the blur: 0 < radius <= 25 blurScript.setRadius(25.0f); //Perform the Renderscript blurScript.setInput(allIn); blurScript.forEach(allOut); //Copy the final bitmap created by the out Allocation to the outBitmap allOut.copyTo(outBitmap); //recycle the original bitmap bitmap.recycle(); //After finishing everything, we destroy the Renderscript. rs.destroy(); return outBitmap; }
通过设置模糊半径(radius)的大小来控制图片的清晰度,简短的几行代码轻松实现图片的高斯模糊处理,我们看一下radius等于最大值25时的图片模糊效果:
原图效果:
高斯模糊:
注意:ScriptIntrinsicBlur的相关方法只支持API 17及以上版本的系统,为了兼容旧版本,Google供了support.v8包,在使用RenderScript和Intrinsics类时,引入v8包中的相关类即可:
import android.support.v8.renderscript.Allocation;import android.support.v8.renderscript.Element;import android.support.v8.renderscript.RenderScript;import android.support.v8.renderscript.ScriptIntrinsicBlur;
同时,在app/build.gradle
文件的defaultConfig
配置中,添加如下两行内容即可:
defaultConfig { ...... renderscriptTargetApi 19 renderscriptSupportModeEnabled true}
在设计上巧妙地运用高斯模糊往往能达到出乎意料的体验效果,比如大神daimajia就利用RenderScript和NineOldAndroids做了一个比较有创意的UI交互,开源库为:AndroidViewHover,效果如下,感兴趣的同学可以一探究竟:
关于Android平台的图片模糊处理,在GitHub上有一些较为优秀的开源类库,笔者整理了一些,推荐给大家学习使用:
- 500px/500px-android-blur
- Dimezis/BlurView
- wasabeef/Blurry
- kikoso/android-stackblur
本文由 亦枫 创作并首发于 亦枫的个人博客 ,同步授权微信公众号:技术鸟(NiaoTech)。
欢迎各种形式地交流与转载,注明作者及出处即可。
本文标题为: Android RenderScript 简单高效实现图片的高斯模糊效果
本文链接为:http://yifeng.studio/2016/10/20/android-renderscript-blur/
- Android RenderScript 简单高效实现图片的高斯模糊效果
- RenderScript----Android图片模糊效果的简单实现
- Android利用RenderScript对图片处理实现高斯模糊效果。
- RenderScript-图片的高斯模糊
- 【android开发】RenderScript 实现高斯模糊。
- 移动端UI设计越来越流行的高斯模糊(Gaussian blur)和毛玻璃效果(磨砂效果),如何使用Android RenderScript简单实现?
- Android高斯模糊(RenderScript)
- 使用RenderScript实现高斯模糊(毛玻璃/磨砂)效果
- 使用RenderScript实现高斯模糊(毛玻璃/磨砂)效果
- 用 Renderscript 实现高效率的图片模糊效果
- 图片高斯模糊效果的实现
- Android模糊一张图片的简单方法之RenderScript
- Android学习------使用RenderScript ,ScriptIntrinsicBlur实现高斯模糊
- Android:用RenderScript实现模糊效果
- 高斯模糊效果的简单实现
- 高斯模糊效果的简单实现
- 高斯模糊实现&RenderScript(渲染脚本)
- android 图片的高斯模糊实现
- MSP430G2553 WDT的定时器模式例子
- 与指向的类型无关;转换要求 reinterpret_cast、c 样式转换或函数样式转换
- 关于ubuntu网卡支持问题的解决办法
- 一些常用的python爬虫技巧总结【文末彩蛋福利】
- Access、Hybrid和Trunk三种模式的理解
- Android RenderScript 简单高效实现图片的高斯模糊效果
- IOS后台运行 之 后台播放音乐
- 100天土鸡饲养计划(71)
- Jquery实现自定义滚动条,DIV滑动效果
- Java networking学习笔记(一):URL的构造函数
- Eclipse Debug 配置
- Unreal Engine 4:数据存储
- Java面试知识点总结
- C#实现类似百度网盘、360云盘在“我的电脑”磁盘驱动器