Tool class for creating reflection effects in Android

Source: Internet
Author: User

First, sometimes we need to create the effect of reflection, we touch the most is the image can create a reflection, and the layout can still create a reflection.

Second, the tool class code

ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Bitmap.Config;ImportAndroid.graphics.Canvas;Importandroid.graphics.LinearGradient;ImportAndroid.graphics.Matrix;ImportAndroid.graphics.Paint;ImportAndroid.graphics.PorterDuff.Mode;ImportAndroid.graphics.PorterDuffXfermode;ImportAndroid.graphics.Shader.TileMode;ImportAndroid.view.View;/*** How to create image reflection * *@author  * */ Public classImagereflect {Private Static intReflectimageheight = 90;//The height of the reflection//Convert to Bitmap     Public StaticBitmap Convertviewtobitmap (View paramview) {paramview.measure (View.MeasureSpec.makeMeasureSpec (0, 0), View.MeasureSpec.makeMeasureSpec (0, 0)); Paramview.layout (0, 0, Paramview.getmeasuredwidth (), Paramview.getmeasuredheight ());        Paramview.builddrawingcache (); returnParamview.getdrawingcache (); }     Public StaticBitmap createcutreflectedimage (Bitmap parambitmap,intparamint) {        inti =parambitmap.getwidth (); intj =parambitmap.getheight (); Bitmap LOCALBITMAP2=NULL; if(J <= Paramint +reflectimageheight) {LOCALBITMAP2=NULL; } Else{Matrix Localmatrix=NewMatrix (); Localmatrix.prescale (1.0F, -1.0f); //System.out.println (J-reflectimageheight-//paramint);Bitmap LocalBitmap1 = Bitmap.createbitmap (parambitmap, 0, J-reflectimageheight-paramint, I, Reflectimageheight, LocalM Atrixtrue); LOCALBITMAP2=Bitmap.createbitmap (i, Reflectimageheight, Bitmap.Config.ARGB_8888); Canvas Localcanvas=NewCanvas (LOCALBITMAP2); Localcanvas.drawbitmap (LocalBitmap1,0.0F, 0.0F,NULL); LinearGradient locallineargradient=NewLinearGradient (0.0F, 0.0F, 0.0F, Localbitmap2.getheight (),-2130706433, 16777215, Tilemode.clamp); Paint Localpaint=NewPaint ();            Localpaint.setshader (locallineargradient); Localpaint.setxfermode (NewPorterduffxfermode (mode.dst_in)); Localcanvas.drawrect (0.0F, 0.0F, I, Localbitmap2.getheight (), localpaint); if(!localbitmap1.isrecycled ()) localbitmap1.recycle ();        System.GC (); }        returnLOCALBITMAP2; }     Public StaticBitmap createreflectedimage (Bitmap parambitmap,intparamint) {        inti =parambitmap.getwidth (); intj =parambitmap.getheight ();        Bitmap LOCALBITMAP2; if(J <=paramint) {LOCALBITMAP2=NULL; } Else{Matrix Localmatrix=NewMatrix (); Localmatrix.prescale (1.0F, -1.0f); Bitmap LocalBitmap1= Bitmap.createbitmap (parambitmap, 0, J-paramint, I, Paramint, Localmatrix,true); LOCALBITMAP2=Bitmap.createbitmap (i, Paramint, Bitmap.Config.ARGB_8888); Canvas Localcanvas=NewCanvas (LOCALBITMAP2); Localcanvas.drawbitmap (LocalBitmap1,0.0F, 0.0F,NULL); LinearGradient locallineargradient=NewLinearGradient (0.0F, 0.0F, 0.0F, Localbitmap2.getheight (),-2130706433, 16777215, Tilemode.clamp); Paint Localpaint=NewPaint ();            Localpaint.setshader (locallineargradient); Localpaint.setxfermode (NewPorterduffxfermode (mode.dst_in)); Localcanvas.drawrect (0.0F, 0.0F, I, Localbitmap2.getheight (), localpaint); }        returnLOCALBITMAP2; }         Public StaticBitmap createreflectedimage (Bitmap originalimage) {Final intReflectiongap = 4; intwidth =originalimage.getwidth (); intHeight =originalimage.getheight (); Matrix Matrix=NewMatrix (); Matrix.prescale (1,-1); Bitmap Reflectionimage= Bitmap.createbitmap (originalimage, 0, Height/2, width, HEIGHT/2, matrix,false); Bitmap bitmapwithreflection=bitmap.createbitmap (width, height+ HEIGHT/2), config.argb_8888); Canvas Canvas=NewCanvas (bitmapwithreflection); Canvas.drawbitmap (Originalimage,0, 0,NULL); Paint Defaultpaint=NewPaint (); Canvas.drawrect (0, height, width, height +Reflectiongap, Defaultpaint); Canvas.drawbitmap (Reflectionimage,0, height + reflectiongap,NULL); Paint Paint=NewPaint (); LinearGradient Shader=NewLinearGradient (0, Originalimage.getheight (),0, Bitmapwithreflection.getheight ()+ Reflectiongap, 0X70FFFFFF, 0X00FFFFFF, Tilemode.mirror);        Paint.setshader (shader); Paint.setxfermode (NewPorterduffxfermode (mode.dst_in)); Canvas.drawrect (0, height, width, bitmapwithreflection.getheight ()+reflectiongap, paint); returnbitmapwithreflection; }}

Three, there are many ways, you can choose according to the needs of their own.

Invocation Example:

PS: I created multiple reflections, so I used a for loop. The loop operation is an array of two images.

 for (int k = 0; k < reflect_count; k++) {            0));        }

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.