Principle:
The original and reflection images are decomposed into two bitmap, and the reflection bitmap is designed to be half the height of the original and uniform in width. Then create a variable empty bitmap, with the width aligned with the original, Width is 1.5 times times the original image (width includes the original and reflection of the width of the sum), and then with the canvas associated with the variable empty bitmap, the canvas on the original and reflection map on the line, in order to hit the effect of the reflection, need to design gradient and other effects.
The main logic is as follows:
Package Com.jackie.revertbitmap;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.lineargradient;import Android.graphics.matrix;import Android.graphics.paint;import Android.graphics.porterduff;import Android.graphics.porterduffxfermode;import Android.graphics.shader;import Android.graphics.drawable.bitmapdrawable;import Android.os.bundle;import Android.widget.imageview;public Class Mainactivity extends Activity {private ImageView mrevertimageview; Private Bitmap Msourcebitmap; Original private Bitmap Mrevertbitmap; Inverted graph @Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mrevertimageview = (ImageView) Findviewbyid (R.id.im_revert); Msourcebitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.source); Mrevertimageview.setbackground (New BITMAPDRawable (Getresources (), Revertbitmap ())); } private Bitmap Revertbitmap () {//1. Inverted graph Matrix matrix = new Matrix (); Matrix.prescale (1,-1); Flip the int width = msourcebitmap.getwidth () to the x-axis; int height = msourcebitmap.getheight (); Generates an inverted graph with the same width as the original, with a height of half the original Mrevertbitmap = Bitmap.createbitmap (msourcebitmap, 0, HEIGHT/2, width, HEIGHT/2, Matri X, False); 2. To generate the original image plus an inverted chart, Mr. Cheng becomes a variable empty bitmap with a height of 1.5 times times the original height (including the height of the original and inverted graphs) int gap = 10; Gap blank Bitmap Bitmap = bitmap.createbitmap (width, height + height/2, Bitmap.Config.ARGB_8888); Paint paint = new paint (); Canvas canvas = new canvas (bitmap); Canvas.drawbitmap (msourcebitmap, 0, 0, paint); Draw the original Canvas.drawbitmap (mrevertbitmap, 0, height + gap, paint); Draw an inverted graph//3. The brush uses lineargradient linear gradient rendering lineargradient lg = new LinearGradient (0, Height + gap, width, bitmap. GetHeight (), 0xabff0000, 0x00ffff00, Shader.TileMode.MIRROR); Paint. Setshader (LG); 4. Specify the xfermode of the brush, which is the pattern to be drawn (different modes, different regions drawn) Paint.setxfermode (new Porterduffxfermode (PorterDuff.Mode.DST_ATOP)); 5. In the inverted plot area, draw a rectangular render layer canvas.drawrect (0, Height + gap, Width, bitmap.getheight (), paint); Paint.setxfermode (NULL); return bitmap; }//Zoom picture private Bitmap resizeimage (Bitmap Bitmap, int width, int height) {int originwidth = Bitmap.getwidth (); int originheight = Bitmap.getheight (); float scalewidth = width/originwidth; float scaleheight = height/originheight; Matrix matrix = new Matrix (); Matrix.postscale (ScaleWidth, ScaleHeight); Bitmap Resizebitmap = Bitmap.createbitmap (Bitmap, 0, 0, originwidth, Originheight, Matrix, True); return resizebitmap; }}
The pattern defined by Porterduffxfermode is as follows:
private static final xfermode[] Smodes = {
New Porterduffxfermode (PorterDuff.Mode.CLEAR),//drawing is not submitted to the canvas
New Porterduffxfermode (PorterDuff.Mode.SRC),//display upper drawing picture
New Porterduffxfermode (PorterDuff.Mode.DST),//display lower drawing picture
New Porterduffxfermode (PorterDuff.Mode.SRC_OVER),//normal drawing display, upper and lower layers draw overlay
New Porterduffxfermode (PorterDuff.Mode.DST_OVER),///upper and lower layers are displayed. The lower house shows
New Porterduffxfermode (PorterDuff.Mode.SRC_IN),//Draw intersection of two layers. Show Upper
New Porterduffxfermode (PorterDuff.Mode.DST_IN),//Draw intersection of two layers. Show downlevel
New Porterduffxfermode (PorterDuff.Mode.SRC_OUT),//take the upper layer to draw the non-intersecting part
New Porterduffxfermode (PorterDuff.Mode.DST_OUT),//Remove layer draw non-intersecting part
New Porterduffxfermode (PorterDuff.Mode.SRC_ATOP),//Remove the non-intersecting portion of the layer from the upper intersection section
New Porterduffxfermode (PorterDuff.Mode.DST_ATOP),//Take the upper non-intersecting part and the lower part of the intersection
New Porterduffxfermode (PorterDuff.Mode.XOR),//color filter effect
New Porterduffxfermode (PorterDuff.Mode.DARKEN),//color filter effect
New Porterduffxfermode (PorterDuff.Mode.LIGHTEN),//color filter effect
New Porterduffxfermode (PorterDuff.Mode.MULTIPLY),//color filter effect
New Porterduffxfermode (PorterDuff.Mode.SCREEN)//Color filter effect
};
As follows:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android to achieve image reflection effect