在很多APP里,个人中心是必不可少的,这样就少不了点击头像换头像,设置专属于自己的头像,设置自己的头像后,将自己的头像设为背景,然后模糊化处理成背景图片,这样的效果看起来很炫。
1.将图片转换为Drawble,然后设置背景图片
[java] view
plaincopyprint?
Drawable drawable = BoxBlurFilter(bitmap);
backgroundLayout.setBackgroundDrawable(drawable);
2,现在开始设置模糊化处理,首先是BoxBlurFilter(),下面是方法的描述
[java] view
plaincopyprint?
/** 水平方向模糊度 */
privatestaticfloathRadius =10;
/** 竖直方向模糊度 */
privatestaticfloatvRadius =10;
/** 模糊迭代度 */
privatestaticintiterations =7;
publicstaticDrawable BoxBlurFilter(Bitmap bmp) {
intwidth = bmp.getWidth();
intheight = bmp.getHeight();
int[] inPixels =newint[width * height];
int[] outPixels =newint[width * height];
Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
bmp.getPixels(inPixels, 0, width,0,0, width, height);
for(inti =0; i
blur(inPixels, outPixels, width, height, hRadius);
blur(outPixels, inPixels, height, width, vRadius);
}
blurFractional(inPixels, outPixels, width, height, hRadius);
blurFractional(outPixels, inPixels, height, width, vRadius);
bitmap.setPixels(inPixels, 0, width,0,0, width, height);
Drawable drawable = newBitmapDrawable(bitmap);
returndrawable;
}
3,调用的方法有blur()
[java] view
plaincopyprint?
publicstaticvoidblur(int[] in,int[] out,intwidth,intheight,
floatradius) {
intwidthMinus1 = width -1;
intr = (int) radius;
inttableSize =2* r +1;
intdivide[] =newint[256* tableSize];
for(inti =0; i <256* tableSize; i++)
divide[i] = i / tableSize;
intinIndex =0;
for(inty =0; y
intoutIndex = y;
intta =0, tr =0, tg =0, tb =0;
for(inti = -r; i <= r; i++) {
intrgb = in[inIndex + clamp(i,0, width -1)];
ta += (rgb >> 24) &0xff;
tr += (rgb >> 16) &0xff;
tg += (rgb >> 8) &0xff;
tb += rgb & 0xff;
}
for(intx =0; x
out[outIndex] = (divide[ta] <
| (divide[tg] <
inti1 = x + r +1;
if(i1 > widthMinus1)
i1 = widthMinus1;
inti2 = x - r;
if(i2 <0)
i2 = 0;
intrgb1 = in[inIndex + i1];
intrgb2 = in[inIndex + i2];
ta += ((rgb1 >> 24) &0xff) - ((rgb2 >>24) &0xff);
tr += ((rgb1 & 0xff0000) - (rgb2 &0xff0000)) >>16;
tg += ((rgb1 & 0xff00) - (rgb2 &0xff00)) >>8;
tb += (rgb1 & 0xff) - (rgb2 &0xff);
outIndex += height;
}
inIndex += width;
}
}
[java] view
plaincopyprint?
publicstaticvoidblur(int[] in,int[] out,intwidth,intheight,
floatradius) {
intwidthMinus1 = width -1;
intr = (int) radius;
inttableSize =2* r +1;
intdivide[] =newint[256* tableSize];
for(inti =0; i <256* tableSize; i++)
divide[i] = i / tableSize;
intinIndex =0;
for(inty =0; y
intoutIndex = y;
intta =0, tr =0, tg =0, tb =0;
for(inti = -r; i <= r; i++) {
intrgb = in[inIndex + clamp(i,0, width -1)];
ta += (rgb >> 24) &0xff;
tr += (rgb >> 16) &0xff;
tg += (rgb >> 8) &0xff;
tb += rgb & 0xff;
}
for(intx =0; x
out[outIndex] = (divide[ta] <
| (divide[tg] <
inti1 = x + r +1;
if(i1 > widthMinus1)
i1 = widthMinus1;
inti2 = x - r;
if(i2 <0)
i2 = 0;
intrgb1 = in[inIndex + i1];
intrgb2 = in[inIndex + i2];
ta += ((rgb1 >> 24) &0xff) - ((rgb2 >>24) &0xff);
tr += ((rgb1 & 0xff0000) - (rgb2 &0xff0000)) >>16;
tg += ((rgb1 & 0xff00) - (rgb2 &0xff00)) >>8;
tb += (rgb1 & 0xff) - (rgb2 &0xff);
outIndex += height;
}
inIndex += width;
}
}
publicstaticvoidblurFractional(int[] in,int[] out,intwidth,
intheight,floatradius) {
radius -= (int) radius;
floatf =1.0f / (1+2* radius);
intinIndex =0;
for(inty =0; y
intoutIndex = y;
out[outIndex] = in[0];
outIndex += height;
for(intx =1; x
inti = inIndex + x;
intrgb1 = in[i -1];
intrgb2 = in[i];
intrgb3 = in[i +1];
inta1 = (rgb1 >>24) &0xff;
intr1 = (rgb1 >>16) &0xff;
intg1 = (rgb1 >>8) &0xff;
intb1 = rgb1 &0xff;
inta2 = (rgb2 >>24) &0xff;
intr2 = (rgb2 >>16) &0xff;
intg2 = (rgb2 >>8) &0xff;
intb2 = rgb2 &0xff;
inta3 = (rgb3 >>24) &0xff;
intr3 = (rgb3 >>16) &0xff;
intg3 = (rgb3 >>8) &0xff;
intb3 = rgb3 &0xff;
a1 = a2 + (int) ((a1 + a3) * radius);
r1 = r2 + (int) ((r1 + r3) * radius);
g1 = g2 + (int) ((g1 + g3) * radius);
b1 = b2 + (int) ((b1 + b3) * radius);
a1 *= f;
r1 *= f;
g1 *= f;
b1 *= f;
out[outIndex] = (a1 <
outIndex += height;
}
out[outIndex] = in[width - 1];
inIndex += width;
}
}
publicstaticintclamp(intx,inta,intb) {
return(x b) ? b : x;
}
3,最后的效果如图