android 图片的模糊化处理,效果类似超级课程表的“我的中心"里头像背景,看起来很炫

在很多APP里,个人中心是必不可少的,这样就少不了点击头像换头像,设置专属于自己的头像,设置自己的头像后,将自己的头像设为背景,然后模糊化处理成背景图片,这样的效果看起来很炫。

1.将图片转换为Drawble,然后设置背景图片

[java]  view plain copy print ?
  1. Drawable drawable = BoxBlurFilter(bitmap);  
  2. backgroundLayout.setBackgroundDrawable(drawable);  

2,现在开始设置模糊化处理,首先是 BoxBlurFilter(),下面是方法的描述

[java]  view plain copy print ?
  1. /** 水平方向模糊度 */  
  2.     private static float hRadius = 10;  
  3.     /** 竖直方向模糊度 */  
  4.     private static float vRadius = 10;  
  5.     /** 模糊迭代度 */  
  6.     private static int iterations = 7;  
  7.   
  8.   
  9.     public static Drawable BoxBlurFilter(Bitmap bmp) {  
  10.         int width = bmp.getWidth();  
  11.         int height = bmp.getHeight();  
  12.         int[] inPixels = new int[width * height];  
  13.         int[] outPixels = new int[width * height];  
  14.         Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);  
  15.         bmp.getPixels(inPixels, 0, width, 00, width, height);  
  16.         for (int i = 0; i < iterations; i++) {  
  17.             blur(inPixels, outPixels, width, height, hRadius);  
  18.             blur(outPixels, inPixels, height, width, vRadius);  
  19.         }  
  20.         blurFractional(inPixels, outPixels, width, height, hRadius);  
  21.         blurFractional(outPixels, inPixels, height, width, vRadius);  
  22.         bitmap.setPixels(inPixels, 0, width, 00, width, height);  
  23.         Drawable drawable = new BitmapDrawable(bitmap);  
  24.         return drawable;  
  25.     }  
3,调用的方法有 blur()

[java]  view plain copy print ?
  1. public static void blur(int[] in, int[] out, int width, int height,  
  2.                            float radius) {  
  3.        int widthMinus1 = width - 1;  
  4.        int r = (int) radius;  
  5.        int tableSize = 2 * r + 1;  
  6.        int divide[] = new int[256 * tableSize];  
  7.   
  8.        for (int i = 0; i < 256 * tableSize; i++)  
  9.            divide[i] = i / tableSize;  
  10.   
  11.        int inIndex = 0;  
  12.   
  13.        for (int y = 0; y < height; y++) {  
  14.            int outIndex = y;  
  15.            int ta = 0, tr = 0, tg = 0, tb = 0;  
  16.   
  17.            for (int i = -r; i <= r; i++) {  
  18.                int rgb = in[inIndex + clamp(i, 0, width - 1)];  
  19.                ta += (rgb >> 24) & 0xff;  
  20.                tr += (rgb >> 16) & 0xff;  
  21.                tg += (rgb >> 8) & 0xff;  
  22.                tb += rgb & 0xff;  
  23.            }  
  24.   
  25.            for (int x = 0; x < width; x++) {  
  26.                out[outIndex] = (divide[ta] << 24) | (divide[tr] << 16)  
  27.                        | (divide[tg] << 8) | divide[tb];  
  28.   
  29.                int i1 = x + r + 1;  
  30.                if (i1 > widthMinus1)  
  31.                    i1 = widthMinus1;  
  32.                int i2 = x - r;  
  33.                if (i2 < 0)  
  34.                    i2 = 0;  
  35.                int rgb1 = in[inIndex + i1];  
  36.                int rgb2 = in[inIndex + i2];  
  37.   
  38.                ta += ((rgb1 >> 24) & 0xff) - ((rgb2 >> 24) & 0xff);  
  39.                tr += ((rgb1 & 0xff0000) - (rgb2 & 0xff0000)) >> 16;  
  40.                tg += ((rgb1 & 0xff00) - (rgb2 & 0xff00)) >> 8;  
  41.                tb += (rgb1 & 0xff) - (rgb2 & 0xff);  
  42.                outIndex += height;  
  43.            }  
  44.            inIndex += width;  
  45.        }  
  46.    }  
[java]  view plain copy print ?
  1. public static void blur(int[] in, int[] out, int width, int height,  
  2.                            float radius) {  
  3.        int widthMinus1 = width - 1;  
  4.        int r = (int) radius;  
  5.        int tableSize = 2 * r + 1;  
  6.        int divide[] = new int[256 * tableSize];  
  7.   
  8.        for (int i = 0; i < 256 * tableSize; i++)  
  9.            divide[i] = i / tableSize;  
  10.   
  11.        int inIndex = 0;  
  12.   
  13.        for (int y = 0; y < height; y++) {  
  14.            int outIndex = y;  
  15.            int ta = 0, tr = 0, tg = 0, tb = 0;  
  16.   
  17.            for (int i = -r; i <= r; i++) {  
  18.                int rgb = in[inIndex + clamp(i, 0, width - 1)];  
  19.                ta += (rgb >> 24) & 0xff;  
  20.                tr += (rgb >> 16) & 0xff;  
  21.                tg += (rgb >> 8) & 0xff;  
  22.                tb += rgb & 0xff;  
  23.            }  
  24.   
  25.            for (int x = 0; x < width; x++) {  
  26.                out[outIndex] = (divide[ta] << 24) | (divide[tr] << 16)  
  27.                        | (divide[tg] << 8) | divide[tb];  
  28.   
  29.                int i1 = x + r + 1;  
  30.                if (i1 > widthMinus1)  
  31.                    i1 = widthMinus1;  
  32.                int i2 = x - r;  
  33.                if (i2 < 0)  
  34.                    i2 = 0;  
  35.                int rgb1 = in[inIndex + i1];  
  36.                int rgb2 = in[inIndex + i2];  
  37.   
  38.                ta += ((rgb1 >> 24) & 0xff) - ((rgb2 >> 24) & 0xff);  
  39.                tr += ((rgb1 & 0xff0000) - (rgb2 & 0xff0000)) >> 16;  
  40.                tg += ((rgb1 & 0xff00) - (rgb2 & 0xff00)) >> 8;  
  41.                tb += (rgb1 & 0xff) - (rgb2 & 0xff);  
  42.                outIndex += height;  
  43.            }  
  44.            inIndex += width;  
  45.        }  
  46.    }  
  47.   
  48.    public static void blurFractional(int[] in, int[] out, int width,  
  49.                                      int height, float radius) {  
  50.        radius -= (int) radius;  
  51.        float f = 1.0f / (1 + 2 * radius);  
  52.        int inIndex = 0;  
  53.   
  54.        for (int y = 0; y < height; y++) {  
  55.            int outIndex = y;  
  56.   
  57.            out[outIndex] = in[0];  
  58.            outIndex += height;  
  59.            for (int x = 1; x < width - 1; x++) {  
  60.                int i = inIndex + x;  
  61.                int rgb1 = in[i - 1];  
  62.                int rgb2 = in[i];  
  63.                int rgb3 = in[i + 1];  
  64.   
  65.                int a1 = (rgb1 >> 24) & 0xff;  
  66.                int r1 = (rgb1 >> 16) & 0xff;  
  67.                int g1 = (rgb1 >> 8) & 0xff;  
  68.                int b1 = rgb1 & 0xff;  
  69.                int a2 = (rgb2 >> 24) & 0xff;  
  70.                int r2 = (rgb2 >> 16) & 0xff;  
  71.                int g2 = (rgb2 >> 8) & 0xff;  
  72.                int b2 = rgb2 & 0xff;  
  73.                int a3 = (rgb3 >> 24) & 0xff;  
  74.                int r3 = (rgb3 >> 16) & 0xff;  
  75.                int g3 = (rgb3 >> 8) & 0xff;  
  76.                int b3 = rgb3 & 0xff;  
  77.                a1 = a2 + (int) ((a1 + a3) * radius);  
  78.                r1 = r2 + (int) ((r1 + r3) * radius);  
  79.                g1 = g2 + (int) ((g1 + g3) * radius);  
  80.                b1 = b2 + (int) ((b1 + b3) * radius);  
  81.                a1 *= f;  
  82.                r1 *= f;  
  83.                g1 *= f;  
  84.                b1 *= f;  
  85.                out[outIndex] = (a1 << 24) | (r1 << 16) | (g1 << 8) | b1;  
  86.                outIndex += height;  
  87.            }  
  88.            out[outIndex] = in[width - 1];  
  89.            inIndex += width;  
  90.        }  
  91.    }  
  92.   
  93.    public static int clamp(int x, int a, int b) {  
  94.        return (x < a) ? a : (x > b) ? b : x;  

  1.    }  
  2. 3,最后的效果如图

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值