Android开发之实现毛玻璃模糊效果

手机API等级在16以上的,可以用这个方法:

if (VERSION.SDK_INT > 16) {
    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    final RenderScript rs = RenderScript.create(context);
    final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE,
            Allocation.USAGE_SCRIPT);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(radius /* e.g. 3.f */);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bitmap);
    return bitmap;
}

假设手机API等级小于16,那就用这个方法:

  @SuppressLint("NewApi")
    public static Bitmap fastblur(Context context, Bitmap sentBitmap, int radius) {

        Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
        if (radius < 1) {
            return (null);
        }
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int[] pix = new int[w * h];
//        Log.e("pix", w + " " + h + " " + pix.length);
        bitmap.getPixels(pix, 0, w, 0, 0, w, h);
        int wm = w - 1;
        int hm = h - 1;
        int wh = w * h;
        int div = radius + radius + 1;
        int r[] = new int[wh];
        int g[] = new int[wh];
        int b[] = new int[wh];
        int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
        int vmin[] = new int[Math.max(w, h)];
        int divsum = (div + 1) >> 1;
        divsum *= divsum;
        int temp = 256 * divsum;
        int dv[] = new int[temp];
        for (i = 0; i < temp; i++) {
            dv[i] = (i / divsum);
        }
        yw = yi = 0;
        int[][] stack = new int[div][3];
        int stackpointer;
        int stackstart;
        int[] sir;
        int rbs;
        int r1 = radius + 1;
        int routsum, goutsum, boutsum;
        int rinsum, ginsum, binsum;
        for (y = 0; y < h; y++) {
            rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
            for (i = -radius; i <= radius; i++) {
                p = pix[yi + Math.min(wm, Math.max(i, 0))];
                sir = stack[i + radius];
                sir[0] = (p & 0xff0000) >> 16;
                sir[1] = (p & 0x00ff00) >> 8;
                sir[2] = (p & 0x0000ff);
                rbs = r1 - Math.abs(i);
                rsum += sir[0] * rbs;
                gsum += sir[1] * rbs;
                bsum += sir[2] * rbs;
                if (i > 0) {
                    rinsum += sir[0];
                    ginsum += sir[1];
                    binsum += sir[2];
                } else {
                    routsum += sir[0];
                    goutsum += sir[1];
                    boutsum += sir[2];
                }
            }
            stackpointer = radius;
            for (x = 0; x < w; x++) {
                r[yi] = dv[rsum];
                g[yi] = dv[gsum];
                b[yi] = dv[bsum];
                rsum -= routsum;
                gsum -= goutsum;
                bsum -= boutsum;
                stackstart = stackpointer - radius + div;
                sir = stack[stackstart % div];
                routsum -= sir[0];
                goutsum -= sir[1];
                boutsum -= sir[2];
                if (y == 0) {
                    vmin[x] = Math.min(x + radius + 1, wm);
                }
                p = pix[yw + vmin[x]];
                sir[0] = (p & 0xff0000) >> 16;
                sir[1] = (p & 0x00ff00) >> 8;
                sir[2] = (p & 0x0000ff);
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
                rsum += rinsum;
                gsum += ginsum;
                bsum += binsum;
                stackpointer = (stackpointer + 1) % div;
                sir = stack[(stackpointer) % div];
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
                rinsum -= sir[0];
                ginsum -= sir[1];
                binsum -= sir[2];
                yi++;
            }
            yw += w;
        }
        for (x = 0; x < w; x++) {
            rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
            yp = -radius * w;
            for (i = -radius; i <= radius; i++) {
                yi = Math.max(0, yp) + x;
                sir = stack[i + radius];
                sir[0] = r[yi];
                sir[1] = g[yi];
                sir[2] = b[yi];
                rbs = r1 - Math.abs(i);
                rsum += r[yi] * rbs;
                gsum += g[yi] * rbs;
                bsum += b[yi] * rbs;
                if (i > 0) {
                    rinsum += sir[0];
                    ginsum += sir[1];
                    binsum += sir[2];
                } else {
                    routsum += sir[0];
                    goutsum += sir[1];
                    boutsum += sir[2];
                }
                if (i < hm) {
                    yp += w;
                }
            }
            yi = x;
            stackpointer = radius;
            for (y = 0; y < h; y++) {
                // Preserve alpha channel: ( 0xff000000 & pix[yi] )
                pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];
                rsum -= routsum;
                gsum -= goutsum;
                bsum -= boutsum;
                stackstart = stackpointer - radius + div;
                sir = stack[stackstart % div];
                routsum -= sir[0];
                goutsum -= sir[1];
                boutsum -= sir[2];
                if (x == 0) {
                    vmin[y] = Math.min(y + r1, hm) * w;
                }
                p = x + vmin[y];
                sir[0] = r[p];
                sir[1] = g[p];
                sir[2] = b[p];
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
                rsum += rinsum;
                gsum += ginsum;
                bsum += binsum;
                stackpointer = (stackpointer + 1) % div;
                sir = stack[stackpointer];
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
                rinsum -= sir[0];
                ginsum -= sir[1];
                binsum -= sir[2];
                yi += w;
            }
        }
//        Log.e("pix", w + " " + h + " " + pix.length);
        bitmap.setPixels(pix, 0, w, 0, 0, w, h);
        return (bitmap);
    }
毛玻璃效果,直接拿去用就行了。

时间: 2024-10-15 02:46:14

Android开发之实现毛玻璃模糊效果的相关文章

Android利用RenderScript实现毛玻璃模糊效果示例

RenderScript 介绍 在开始之前,先看下 RenderScript 的官方介绍: RenderScript is a framework for running computationally intensive tasks at high performance on Android. RenderScript is primarily oriented for use with data-parallel computation, although serial workloads

Android开发之毛玻璃效果实例代码_Android

这是在网上找的,不过忘了在哪里找的,经过很多比较测试,发现这个方法不会 oom,目前来看 我一直没有遇过,今天才找到这个以前建立的工程,记录下来: 先给大家展示下效果图: public class FastBlur { public static Bitmap doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap) { // This is a compromise between Gaussian Blur and Box

sdk-问一个Android开发的环境安装问题!求解!

问题描述 问一个Android开发的环境安装问题!求解! 我安装Android SDK点击SDK Manager.exe之后这个界面不是有很多的包可以安装吗?怎么我的这个就是这么点希望大神帮我下! 解决方案 参考下这个, http://jingyan.baidu.com/article/b0b63dbfd0948c4a483070ea.html 因为现在国内连不上google了... 解决方案二: 参考下这个, http://jingyan.baidu.com/article/b0b63dbfd

android开发查询数据库的实现类和dao层

问题描述 android开发查询数据库的实现类和dao层 之前开发的项目数据单机的吧,现在在公司要开发联网的项目,就用上了数据库,我就要写这前我写的实现类和接口,还有DBHelper,那是我做网站的时候用于查询数据库的,请问做app操作数据库和我之前写的一样么,写个DBHelper,实现类集成接口,增删查改写到实现类里.感觉这流程是不怎么熟悉啊 解决方案 ActiveAndroid 一个andriod DB操作的开源库,很方便 解决方案二: 你用ado比较好点----

android 求职-Android开发求职应聘必备书籍??

问题描述 Android开发求职应聘必备书籍?? 请教各位前辈.大神,校招季已经开始了,我是应届毕业生,想找一份Android的开发工作,请问要哪些知识储备和条件,准备应聘面试有哪些经典的Android书可以看看啊?请教各路大神.前辈.小弟感激不尽!! 解决方案 第一行代码.....

sessionstorage-html5 Android开发 sessionStorage问题

问题描述 html5 Android开发 sessionStorage问题 5C 会话存储的sessionStorage是存储在内存条中的吗?当安卓系统内存不足的时候,会不会被系统回收机制回收掉? 解决方案 HTMl5的sessionStorage和localStor- 解决方案二: 如果被回收,整个APP可能都被回收了,再重新登录就可以了.不用担心.

解析json数据-android 开发中 json解析问题出错啊

问题描述 android 开发中 json解析问题出错啊 {"msg":1,"msgbox":"返回歌手分类!","data":"[ { "rownum":"11","id":"1047","userName":"敖日格勒","picurl":"mobile1_1047

android开发如何声明动态一维数组

问题描述 android开发如何声明动态一维数组 我是一个Android新手,我现在想读取一个文件,文件大小未知,我想用 byte[] buffer = new byte[3072]; temp_stream = new FileInputStream(img_name); temp_stream.read(buffer); buffer接收,如何声明一个动态byte[] buffer. 解决方案 Java获得文件大小的方法(通过FileInputStream) 根据指定文件创建FileInpu

软件-请教一下Android开发 服务器端的问题

问题描述 请教一下Android开发 服务器端的问题 我是新手,不太了解服务器.想问问服务器对于一个软件,比如人人网的android客户端吧,来说的角色是什么? 软件做到什么程度了开始搭建服务器? 还有就是例如我写的本地注册登录账号的功能,在搭建服务器的时候这些代码应该是经过少部分改动就能加到服务器上注册登录吧? 解决方案 我们是项目开始的时候就把所需的接口都定义啊,客户端先用假数据,服务器端也开始写,写好了就直接调用.首先一定要把借口定义好,要不然改的代码可就多了