Android advanced Blur technology for how to achieve the effect of frosted glass

Source: Internet
Author: User
Tags vmin

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

Effect we know, how to implement in Android, the White is the image of the blur, the small part of the first to tell you the Android Advanced fuzzy Technology Principles, as follows:

• First I created an empty bitmap, copied a portion of the background, and I then obfuscated the bitmap and set it to TextView background.

• Save the state of the canvas through this bitmap;

• Move the canvas to the TextView location in the parent layout file;

• Draw the contents of the ImageView into the bitmap;

• At this point, we have a bitmap of the same size as TextView, which contains part of the ImageView, which is the content of the layout behind the TextView;

• Create an instance of Renderscript;

• Copy the bitmap into the data sheet required by the Renderscript;

• Create an instance of Renderscript obfuscation;

• Set the input, radius range and then blur the process;

• Copy the processed results back to the previous bitmap;

• Well, we have already awakened bitmap to blur, and can set it to TextView background;

I recently made an app, which has a function need to image processing to achieve the effect of the glass, after some research, found in 3 implementations, each with advantages and disadvantages, if the system's API in more than 16, you can use the system provides methods to directly process the picture, But the small part thinks the solution below is the best way to achieve the results.

The code is as follows:

Public Bitmap Fastblur (context context, Bitmap sentbitmap, int radius) {Bitmap Bitmap = sentbitmap.copy (Sentbitmap.getcon Fig (), 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); in T 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 = divsum;int dv[] = new Int[temp];for (i = 0; i < temp; i++) {Dv[i] = (i/d Ivsum);} yw = Yi = 0;int[][] stack = new Int[div][3];int stackpointer;int stackstart;int[] sir;int rbs;int r1 = radius + 1;int rout Sum, 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 + = s IR[2];}} Stackpointer = radius;for (x = 0; x < W; + +) {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];goutsu M-= 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 & 0x0 000FF); Rinsum + = Sir[0];ginsum + sir[1];binsum + = sir[2];rsum + rinsum;gsum + ginsum;bsum + = Binsum;stackpointer = (STA Ckpointer + 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 code implements the following:


If you want to source, please support, share to friends Circle, send to share to the circle of friends screenshots and mailbox to this public number, small series will strive for the first time to send the source to you.


SMART_ANDROID&NBSP; (← long press copy)

Introduction: Non-famous programmers, the word Zhong straight right, number astringent lang, love to Machine, love programming, is crawling in the mobile internet in a code-maker!

Personal Number: LOONGGG

Micro-blog: astringent Lang

QQ Group: 413589216 More information and source code to do in QQ group files

Today's headline: Search for "non-famous programmers" subscription more information

Work: Focus on the development and research of mobile Internet, this number is dedicated to sharing it technology and procedures ape work experience. Welcome everyone to pay attention to and reprint.



a number of development video Tutorials are available at this public number, follow this public number , click on the dry materials in the menu, select the video tutorial to get the download URL.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android advanced Blur technology for how to achieve the effect of frosted glass

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.