Android Frosted glass effect Slide Menu

Source: Internet
Author: User

To achieve the goal I believe we all know how the effect of wool glass, but also can be said to be Gaussian blur effect. As follows:
This is a transparent, and will be Gaussian blur the effect of the background, it looks like a frosted glass, in fact, not only the side-by-side menu, as long as the view, theoretically can achieve this effect, next we will achieve this effect. The first step: The framework builds the Android studio I use, so it's easy to create a project with a side-by-side menu, and in the step of creating a new project, you can choose navigation Drawer activity:
Android Studio will automatically create a project with this side-by-side overlay of the current Activity menu, if it is eclipse, you have to implement, because the focus is not on the side-by-side menu, do not say the implementation of the side-by-side menu, you can go to the following source code address to see the source code, you can achieve. The second step: the realization of the principle before the implementation, you have to confirm, what kind of way to achieve this effect. Because the view real-time behind the drawer slide menu is changing, the view can only be intercepted in real time, Gaussian blur, and then as the background of drawer. This operation can only be done before the drawer is drawn, because if it is intercepted and processed at the time of drawing, it will not only be Kaka, but the captured image will not be needed now. Fortunately, there is an event in Android, called Predraw, so the name Incredibles, is to draw the event before the call, the Android view when drawing, will first draw the next layer, then get to view the level of the viewer, when drawing, The desired view is then Gaussian blurred, set to the background of the drawer, so that we can achieve the function we need. Perhaps I did not express clearly, but it doesn't matter, look at the following code parsing, you can understand. The third step: Implement the Code first, you need to get the control of the slide menu. The side-by-side menu generated by Android Studio is implemented with fragment, so you can get to it using the following code:
Mnavigationdrawerfragment = (navigationdrawerfragment)                Getsupportfragmentmanager (). Findfragmentbyid ( R.id.navigation_drawer);
This navigationdrawerfragment is our side-by-side menu implementation of the class, you can not control it. Then you need to get to the view in this fragment, get the View viewer and register the Predraw listener event:
Mnavigationdrawerfragment.getview (). Getviewtreeobserver (). Addonpredrawlistener (New Viewtreeobserver.onpredrawlistener () {                    });
This onpredrawlistener needs to implement the Onpredraw method, in this method, for the current view, and Gaussian blur. First find the root control and create a bitmap object to save it.
Private Framelayout view;private Bitmap Bitmap;
After the view is initialized, the view is started in the following way:
View.builddrawingcache (); bitmap = View.getdrawingcache ();
In this way, the current view is saved in the bitmap, then we want to Gaussian blur the image, about the Gaussian blur algorithm, there are many online, I would like to paste a (copy):
    public static Bitmap Doblur (Bitmap sentbitmap, int. radius, Boolean Canreuseinbitmap) {//Stack Blur v1.0 from http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html////Java Author:mario Klingeman n <mario at quasimondo.com>//http://incubator.quasimondo.com//created Feburary 29, 2004// Android Port:yahel Bouaziz <yahel at kayenko.com>//http://www.kayenko.com//ported April 5th, 201 2//This is a compromise between Gaussian Blur and Box Blur//It creates much better looking blurs than Bo        x Blur, but is//7x faster than my Gaussian Blur implementation. I called it Stack Blur because this describes best how this//filter works Internally:it creates a K IND of moving stack//of colors whilst scanning through the image. thereby it//just have to add one new block of color to the right side//the stack and remove THe leftmost color. The remaining//colors on the topmost layer of the stack is either added on//or reduced by one, Dependin        G on if they is on the right or//on the left side of the stack. If you is using this algorithm in your code "ADD///The following line:////Sta        CK Blur Algorithm by Mario Klingemann <[email protected]> Bitmap Bitmap;        if (canreuseinbitmap) {bitmap = Sentbitmap;        } else {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 dv[] = new int[256 * Divsum];        for (i = 0; i < * divsum; 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; + +) {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; + +) {rinsum = Ginsum = Binsum = Routsum = Goutsum = Boutsum = Rsum = Gsum = BS            um = 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;            }} bitmap.setpixels (pix, 0, W, 0, 0, W, h);    return (bitmap); }
The first parameter is a picture that requires a Gaussian blur, the second parameter can be interpreted as the blur effect size, and the third parameter indicates whether the bitmap can be reused. Well, with a good picture, you can set it to the background of drawer .... Are you sure? Think carefully, of course, is not right, now cut the picture is the whole view of the picture, and our drawer is slowly slide out, at any time may stop, so, using the full picture is definitely not. Then you need to calculate the location of the image to intercept the view, and set to the location of the drawer, in particular, refer to:
From the figure can be seen out, wherein the required parameters are connected, that is, as long as you can get one of the parameters, the other can get at the same time. The width of the drawer can be fixed, the example is 240, now only need to get to the side of the slide to find out which position can be calculated all the parameters. In view, there is a way, called Getlocationinwindow, can get to the current view in the entire window position, you can imagine that the obtained is definitely a negative number, through the width of drawer and this negative number, It is easy to figure out the width of the display. (in addition to the Getlocationinwindow in view, there are many other ways to get the position relative to the various views, you can look at it).
First get to Location:
int[] location = new Int[2];mnavigationdrawerfragment.getview (). Getlocationinwindow (location); Blur (Bitmap, ListView , location[0]);//Only X-coordinates are transmitted
The Blur function is the detail of our processing position, and the ListView is the only view in the fragment:
    private void Blur (Bitmap bkg, View view,int width) {Float scalefactor = 4;//scale picture, blur effect better after zooming float Radiu        s = 2; Bitmap overlay = Bitmap.createbitmap ((int) (View.getmeasuredwidth ()/scalefactor), (int) (View.getmeasuredhe        ight ()/scalefactor), Bitmap.Config.ARGB_8888);        Canvas canvas = new canvas (overlay);        Canvas.translate (-view.getleft ()/scalefactor,-view.gettop ()/scalefactor);        Canvas.scale (1/scalefactor, 1/scalefactor);        Paint paint = new paint ();        Paint.setflags (Paint.filter_bitmap_flag); float visiblewidth = slidemenuwidth +width;//visible wide int visibleheight = View.getheight ();//visible high//area captured from view, +                The reason for 10 and below-10 is that Gaussian blurred edges sometimes have dark shadows, so increase the blur area Rect src = new rect (0,0, (int) (visiblewidth) +10, visibleheight); RECTF dest = new RECTF (-width-10, 0, Slidemenuwidth, visibleheight);//Set drawer background area CANVAS.DRAWBI                TMap (bkg, SRC, dest, paint); Overlay = IMAGEUTILS.DOblur (overlay, (int) radius, true);//Gaussian Blur operation if (Build.VERSION.SDK_INT <) {//16level previously used this method, was discarded in 16                View.setbackgrounddrawable (New Bitmapdrawable (Getresources (), overlay));                } else {View.setbackground (new bitmapdrawable (Getresources (), overlay)); }        }
The entire event monitoring code is as follows:
Mnavigationdrawerfragment.getview (). Getviewtreeobserver (). Addonpredrawlistener (New Viewtreeobserver.onpredrawlistener () {            @Override public            boolean Onpredraw () {                if (bitmap = = null) {                    View.builddrawingcache ();                    Bitmap = View.getdrawingcache ();                }                int[] location = new Int[2];                Mnavigationdrawerfragment.getview (). Getlocationinwindow (location);                Blur (Bitmap, ListView, location[0]);//only x coordinates                return true;            }        );
Here, the slide-off menu of the frosted glass effect has been completed. Conclusion If you can understand the method I used above, I believe you can extrapolate, to achieve all the view of the glass effect, this example code, I will put on GitHub, here is the link address: https://github.com/xjyaikj/ Glassslidemenusample, if there is something wrong with the code, or where it is worth optimizing, I hope you can communicate with me, my contact information is below the left profile.

Android Frosted glass effect Slide Menu

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.