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

在很多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,最后的效果如图

592a39862e24877cd65c269e9fcd0071.png

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值