当前位置: 首页 > 编程笔记 >

Android中实现毛玻璃效果的3种方法

严景焕
2023-03-14
本文向大家介绍Android中实现毛玻璃效果的3种方法,包括了Android中实现毛玻璃效果的3种方法的使用技巧和注意事项,需要的朋友参考一下

最近在做一款叫叽叽的App(男银懂的),其中有一个功能需要对图片处理实现毛玻璃的特效

进过一番预研,找到了3中实现方案,其中各有优缺点:

1、如果系统的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;         }

2、 如果Api条件不满足,可以使用如下方法


@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);     }

3、以上方法都存在一个问题,性能较低,下面提供一个C实现


static int* StackBlur(int* pix, int w, int h, int radius) {

    int wm = w - 1;

    int hm = h - 1;

    int wh = w * h;

    int div = radius + radius + 1;

    int *r = (int *)malloc(wh * sizeof(int));     int *g = (int *)malloc(wh * sizeof(int));     int *b = (int *)malloc(wh * sizeof(int));     int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;

    int *vmin = (int *)malloc(MAX(w,h) * sizeof(int));

    int divsum = (div + 1) >> 1;     divsum *= divsum;     int *dv = (int *)malloc(256 * divsum * sizeof(int));     for (i = 0; i < 256 * divsum; i++) {         dv[i] = (i / divsum);     }

    yw = yi = 0;

    int(*stack)[3] = (int(*)[3])malloc(div * 3 * sizeof(int));     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 + (MIN(wm, MAX(i, 0)))];             sir = stack[i + radius];             sir[0] = (p & 0xff0000) >> 16;             sir[1] = (p & 0x00ff00) >> 8;             sir[2] = (p & 0x0000ff);

            rbs = r1 - 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] = 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 = MAX(0, yp) + x;

            sir = stack[i + radius];

            sir[0] = r[yi];             sir[1] = g[yi];             sir[2] = b[yi];

            rbs = r1 - 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] = 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;         }     }

    free(r);     free(g);     free(b);     free(vmin);     free(dv);     free(stack);     return(pix); }

 类似资料:
  • 本文向大家介绍Android实现毛玻璃效果的对话框,包括了Android实现毛玻璃效果的对话框的使用技巧和注意事项,需要的朋友参考一下 一个popwindow,在弹出的时候背景是原界面的截图加高斯模糊效果: 先给出popwindow的布局文件 里面那个自定义imageView控件在我上一篇博客里,下面是activity的布局 用于圆角的背景xml,放在drawable文件夹中 activity的源

  • 本文向大家介绍iOS毛玻璃效果的实现及图片模糊效果的三种方法,包括了iOS毛玻璃效果的实现及图片模糊效果的三种方法的使用技巧和注意事项,需要的朋友参考一下 App设计时往往会用到一些模糊效果或者毛玻璃效果,iOS目前已提供一些模糊API可以让我们方便是使用。 话说苹果在iOS7.0之后,很多系统界面都使用了毛玻璃效果,增加了界面的美观性,比如下图的通知中心界面; 但是其iOS7.0的SDK并没有提

  • 本文向大家介绍Android毛玻璃背景效果简单实现代码,包括了Android毛玻璃背景效果简单实现代码的使用技巧和注意事项,需要的朋友参考一下 思路:      1.使用findViewByid获得需要设置毛玻璃的控件。      2.调用 setBlurBackground(Bitmap bmp)方法(下面会给出),参数为Bitmap类型,给控件设置毛玻璃背景!      3.已完成, 方法:

  • 本文向大家介绍Android模糊处理实现图片毛玻璃效果,包括了Android模糊处理实现图片毛玻璃效果的使用技巧和注意事项,需要的朋友参考一下 本文实例讲解了Android 虚化图片、模糊图片、图片毛玻璃效果的实现方法,具体内容如下 效果如图: 在Android可以用RenderScript方便的实现这个方法: 但是RenderScript的这个方法需要Android API17,也就说需要在An

  • 本文向大家介绍Android模糊处理简单实现毛玻璃效果,包括了Android模糊处理简单实现毛玻璃效果的使用技巧和注意事项,需要的朋友参考一下 自从iOS系统引入了Blur效果,也就是所谓的毛玻璃、模糊化效果、磨砂效果,各大系统就开始竞相模仿,这是怎样的一个效果呢,我们先来看一下,如下面的图片: 实现效果大家都知道了,如何在Android中实现呢,说白了就是对图片进行模糊化处理,小编先给大家讲一下

  • 本文向大家介绍iOS自定义collectionView实现毛玻璃效果,包括了iOS自定义collectionView实现毛玻璃效果的使用技巧和注意事项,需要的朋友参考一下 先来看看效果图,由于录屏软件不给力,毛玻璃效果不明显,请见谅。 步骤详解: 说下思路,很简单,首先自定义一个collectionView, 重写它的initWithFrame:collectionViewLayout:方法,在这