Android advanced Fuzzy Technology: How to Implement glass wool effect, Android glass wool

Source: Internet
Author: User
Tags vmin

Android advanced Fuzzy Technology: How to Implement glass wool effect, Android glass wool

Since the introduction of Blur effects in iOS systems, that is, the so-called glass wool, Blur effects, and matte effects, various systems have begun to compete for imitation. What is the effect? Let's take a look at it first, see the following figure:

We know the effect. How can we implement it in Android? To put it bluntly, we need to blur the image. Let's talk about the principles of Android advanced fuzzy technology first, as shown below:

· First, I created an empty bitmap and copied a part of the background. Then I will perform fuzzy processing on this bitmap and set it as the background of TextView.

· Use this bitmap to save the Canvas status;

· Move the Canvas to the position of TextView in the parent layout file;

· Draw the ImageView content to bitmap;

· In this case, we have a bitmap of the same size as TextView. It contains part of the ImageView, that is, the layout of the TextView;

· Create an Renderscript instance;

· Copy bitmap to the data sheet required by Renderscript;

· Create an Renderscript fuzzy processing instance;

· Set the input, and blur the radius;

· Copy the processed result back to the previous bitmap;

· Well, we have awakened bitmap for fuzzy processing. You can set it as the TextView background;

I recently am working on an App. One of the features requires special effects on image processing. After some research, I found the three implementation schemes, each of which has its own advantages and disadvantages, if the system's api is more than 16, you can use the method provided by the system to directly process the image, but xiaobian believes that the following solution is the best solution.

The Code is as follows:

public 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];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 code is implemented as follows:


If you want the source code, please support it a lot, share it with your circle of friends, send screenshots and emails to this public account after you share it with your circle of friends, And the editor will try to send the source code to you as soon as possible.


No.: smart_android (persistent copy)

Introduction: A non-famous programmer, who has the character "straight left" and "right", loves machine and programming. He is a programmer crawling on the mobile Internet!

Personal ID: loonggg

Weibo: qilang

QQ group: 413589216 more information and source code are available in QQ group files

Toutiao: search for "non-famous programmers" to subscribe to more information

Work: focus on the development and research of mobile Internet. I am dedicated to sharing my experience in IT technology and programmers. You are welcome to pay attention to and repost.



Many development video tutorials are available in this public account. Follow this public account, click dry goods Materials in the menu, and select the video tutorial to get the download URL.





Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.