The realization of the fuzzy effect of the development of Android

Source: Internet
Author: User
Tags abs vmin

Mobile API level of more than 16, you can use this method:

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

Assuming the mobile API level is less than 16, use this method:

@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);
}
Hair glass effect, directly to use on the line.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.