The simple realization of the effect _android of the wool glass by the Android fuzzy processing

Source: Internet
Author: User

Since the iOS system introduced the Blur effect, that is, the so-called glass, blur effect, matte effect, the major systems began to imitate, this is how an effect, we first look at, such as the following picture:

Realize the effect we all know, how to achieve in Android, plainly is the image of the fuzzy processing, small set first to tell us the Android Advanced fuzzy Technology Principles, as follows:

    • First I created an empty bitmap, copied a part of the background, and then I bitmap it and set it to the TextView background.
    • Preserve the state of canvas through this bitmap;
    • Move the canvas to the TextView position in the parent layout file;
    • The content of the ImageView is painted into the bitmap;
    • At this point, we have a textview the same size as the bitmap, it contains the ImageView part of the content, that is, textview behind a layer of the content of the layout;
    • Create an instance of Renderscript;
    • Copy the bitmap into a piece of data that renderscript needs;
    • Create an instance of Renderscript obfuscation;
    • Setting the input, radius and then fuzzy processing;
    • The processed results are copied back to the previous bitmap;
    • Well, we've awakened the bitmap, and can set it to the TextView background;

I recently made an app, which has a function to the image processing to achieve the effect of the hair glass, after a study, found 3 of the implementation, which have advantages and disadvantages, if the system's API in more than 16, you can use the method provided by the system directly processing pictures, However, the small part of the solution that the bottom is the best implementation effect.
The code is as follows:

Public Bitmap Fastblur (context context, Bitmap sentbitmap, int radius) {Bitmap Bitmap = sentbitmap.copy (Sentbitma 
 
    P.getconfig (), true); 
    if (RADIUS < 1) {return (NULL); 
    int w = bitmap.getwidth (); 
 
    int h = bitmap.getheight (); 
    int[] pix = new int[w * h]; 
 
    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++) {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; 
    } bitmap.setpixels (pix, 0, W, 0, 0, W, h); 
  return (bitmap); 
 }

The above is the entire content of this article, to help everyone easily achieve the effect of hair glass, I hope you like.

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.