Android notes: image reflection, ReflectionImage

Source: Internet
Author: User

ReflectionImage class code:

Import android. content. context; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. linearGradient; import android. graphics. matrix; import android. graphics. paint; import android. graphics. porterDuff. mode; import android. graphics. porterduxfermode; import android. graphics. shader. tileMode; import android. graphics. drawable. bitmapDrawable; import android. util. attributeSet; import android. widget. imageView; // image Reflection public class ReflectionImage extends ImageView {// whether the Reflection mode is private boolean mReflectionMode = true; public ReflectionImage (Context context) {super (context );} public ReflectionImage (Context context, AttributeSet attrs) {super (context, attrs); // obtain bitmap of the original image and draw BitmapDrawable bd = (BitmapDrawable) this. getDrawable (); Bitmap originalImage = bd. getBitmap (); DoReflection (originalImage);} public ReflectionImage (Context 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;} // only expires, the same thing as in the constructor @ Override public 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 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, 0, 0, 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 in paint. setXfermode (new porterduduxfermode (Mode. DST_IN); // Draw a rectangle using the paint with our linear gradient canvasRef. drawRect (0, height, width, bitmapWithReflection. getHeight () + reflectionGap, paint); // call setImageBitmap in ImageView this. setImageBitmap (bitmapWithReflection );}}
















This article is from the "no trace in the sky but I fly over" blog. For more information, contact the author!

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.