Create Image with Reflection

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值