Android给bitmap图加上倒影效果


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.   
  5.         int width = originalImage.getWidth();  
  6.         int height = originalImage.getHeight();  
  7.   
  8.         // This will not scale but will flip on the Y axis   
  9.         Matrix matrix = new Matrix();  
  10.         matrix.preScale(1, -1);  
  11.         // Create a Bitmap with the flip matrix applied to it.   
  12.         // We only want the bottom half of the image   
  13.         Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width,  
  14.                 height / 2, matrix, false);  
  15.   
  16.         // Create a new bitmap with same width but taller to fit reflection   
  17.         Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2),  
  18.                 Config.ARGB_8888);  
  19.   
  20.         // Create a new Canvas with the bitmap that's big enough for   
  21.         // the image plus gap plus reflection   
  22.         Canvas canvas = new Canvas(bitmapWithReflection);  
  23.         // Draw in the original image   
  24.         canvas.drawBitmap(originalImage, 00null);  
  25.         // Draw in the gap   
  26.         Paint defaultPaint = new Paint();  
  27.         canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);  
  28.         // Draw in the reflection   
  29.         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
  30.   
  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, 0x70ffffff0x00ffffff,  
  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 PorterDuffXfermode(Mode.DST_IN));  
  40.         // Draw a rectangle using the paint with our linear gradient   
  41.         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);  
  42.   
  43.         return bitmapWithReflection;  
  44.     }  

相关内容