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时的图片模糊效果:

原图效果:
RenderScript-Blur-Before

高斯模糊:
RenderScript-Blur-After

注意: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,效果如下,感兴趣的同学可以一探究竟:

daimajia-AndroidViewHover.gif

关于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/

0 0
原创粉丝点击