Reflectionimage is inherited from Android. widget. imageview. It is used in the same way as other android controls.
The idea is to plagiarize a website.
The implementation is very simple. The detailed explanation is written into the comment. The overall idea is to re-draw the original image in the canvas, and reverse the image using matrix, transparent processing is actually a layer of gradient mask.
Import android. content. context; 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. porterduxfermode; import android. graphics. bitmap. config; import android. graphics. porterduff. mode; import android. graphics. shader. tilemode; import android. graphics. drawable. bitmapdrawable; import android. util. attributeset; import android. widget. imageview; public class reflectionimage extends imageview {// whether the reflection mode is private Boolean mreflectionmode = true; Public reflectionimage (context) {super (context);} public reflectionimage (context, attributeset attrs) {super (context, attrs); // obtain the bitmap of the original image and redraw bitmap originalimage = (bitmapdrawable) This. getdrawable ()). getbitmap (); doreflection (originalimage);} public reflectionimage (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle); bitmap originalimage = (bitmapdrawable) this. getdrawable ()). getbitmap (); doreflection (originalimage);} public void setreflectionmode (Boolean isref) {mreflectionmode = isref;} public Boolean getreflectionmode () {return mreflectionmode;} // gets lazy, only setimageresource is overwritten, and the same thing is done in the constructor @ overridepublic void setimageresource (INT resid) {bitmap originalimage = bitmapfactory. decoderesource (getresources (), resid); doreflection (originalimage); // super. setimageresource (resid);} private void doreflection (Bitmap originalimage) {final int reflectiongap = 4; // the spacing between the original image and the reflected image int width = originalimage. getwidth (); int Height = originalimage. getheight (); // reverse the matrix = new matrix (); matrix. prescale (1,-1); // reflectionimage is the transparent part below. You can set its height to 3/4 of the original height, which improves bitmap reflectionimage = bitmap. createbitmap (originalimage, width, height, matrix, false); // create a new bitmap, which is twice the original bitmap bitmapwithreflection = bitmap. createbitmap (width, (height + height), config. argb_8888); canvas canvasref = new canvas (bitmapwithreflection); // draw the original image canvasref first. drawbitmap (originalimage, 0, 0, null); // painting interval paint deafaultpaint = new paint (); canvasref. drawrect (0, height, width, height + reflectiongap, deafaultpaint); // canvasref of the image after the image is reversed. drawbitmap (reflectionimage, 0, height + reflectiongap, null); // create a gradient mask and put it on the reversed image below paint = new paint (); lineargradient shader = new lineargradient (0, originalimage. getheight (), 0, bitmapwithreflection. getheight () + reflectiongap, 0x80ffffff, 0x00ffffff, tilemode. clamp); // set the paint to use this shader (linear gradient) paint. setshader (shader); // set the transfer mode to be Porter Duff and destination inpaint. setxfermode (New porterduduxfermode (mode. dst_in); // draw a rectangle using the paint with our linear gradientcanvasref. drawrect (0, height, width, bitmapwithreflection. getheight () + reflectiongap, paint); // call setimagebitmapthis in imageview. setimagebitmap (bitmapwithreflection );}}