Create Image with reflection

Source: Internet
Author: User
  1. /**
  2. * This code is courtesy of Neil Davies at http://www.inter-fuser.com
  3. * @ Param context the current context
  4. * @ Param originalimage the original bitmap image used to create the reflection
  5. * @ Return the bitmap with a reflection
  6. */
  7. Public static bitmap createreflectedimage (context, bitmap originalimage ){
  8. // The gap we want between the reflection and the original image
  9. Final int reflectiongap = 4;
  10. Int width = originalimage. getwidth ();
  11. Int Height = originalimage. getheight ();
  12. // This will not scale but will flip on the Y axis
  13. Matrix matrix = new matrix ();
  14. Matrix. prescale (1,-1 );
  15. // Create a bitmap with the flip matrix applied to it.
  16. // We only want the bottom half of the image
  17. Bitmap reflectionimage = bitmap. createbitmap (originalimage, 0, height/2, width, height/2, matrix, false );
  18. // Create a new bitmap with same width but taller to fit reflection
  19. Bitmap bitmapwithreflection = bitmap. createbitmap (width
  20. , (Height + height/2), config. argb_8888 );
  21. // Create a new canvas with the bitmap that's big enough
  22. // The image plus gap plus reflection
  23. Canvas canvas = new canvas (bitmapwithreflection );
  24. // Draw in the original image
  25. Canvas. drawbitmap (originalimage, 0, 0, null );
  26. // Draw in the gap
  27. Paint defaultpaint = new paint ();
  28. Canvas. drawrect (0, height, width, height + reflectiongap, defapaint paint );
  29. // Draw in the reflection
  30. Canvas. drawbitmap (reflectionimage, 0, height + reflectiongap, null );
  31. // Create a shader that is a linear gradient that covers the reflection
  32. Paint paint = new paint ();
  33. Lineargradient shader = new lineargradient (0, originalimage. getheight (), 0,
  34. Bitmapwithreflection. getheight () + reflectiongap, 0x70ffffff, 0x00ffffff,
  35. Tilemode. Clamp );
  36. // Set the paint to use this shader (linear gradient)
  37. Paint. setshader (shader );
  38. // Set the transfer mode to be Porter Duff and destination in
  39. Paint. setxfermode (New porterduxfermode (mode. dst_in ));
  40. // Draw a rectangle using the paint with our linear gradient
  41. Canvas. drawrect (0, height, width,
  42. Bitmapwithreflection. getheight () + reflectiongap, paint );
  43. Return bitmapwithreflection;

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.