歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android給bitmap圖加上倒影效果

Android給bitmap圖加上倒影效果

日期:2017/3/1 11:14:58   编辑:Linux編程

Android給bitmap圖加上倒影效果:

  1. public static Bitmap createReflectedImage(Bitmap originalImage) {
  2. // The gap we want between the reflection and the original image
  3. final int reflectionGap = 4;
  4. int width = originalImage.getWidth();
  5. int height = originalImage.getHeight();
  6. // This will not scale but will flip on the Y axis
  7. Matrix matrix = new Matrix();
  8. matrix.preScale(1, -1);
  9. // Create a Bitmap with the flip matrix applied to it.
  10. // We only want the bottom half of the image
  11. Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width,
  12. height / 2, matrix, false);
  13. // Create a new bitmap with same width but taller to fit reflection
  14. Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2),
  15. Config.ARGB_8888);
  16. // Create a new Canvas with the bitmap that's big enough for
  17. // the image plus gap plus reflection
  18. Canvas canvas = new Canvas(bitmapWithReflection);
  19. // Draw in the original image
  20. canvas.drawBitmap(originalImage, 0, 0, null);
  21. // Draw in the gap
  22. Paint defaultPaint = new Paint();
  23. canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
  24. // Draw in the reflection
  25. canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
  26. // Create a shader that is a linear gradient that covers the reflection
  27. Paint paint = new Paint();
  28. LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
  29. bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff,
  30. TileMode.CLAMP);
  31. // Set the paint to use this shader (linear gradient)
  32. paint.setShader(shader);
  33. // Set the Transfer mode to be porter duff and destination in
  34. paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
  35. // Draw a rectangle using the paint with our linear gradient
  36. canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
  37. return bitmapWithReflection;
  38. }

Copyright © Linux教程網 All Rights Reserved