zoukankan      html  css  js  c++  java
  • Android中创建倒影效果的工具类

              

          一、有时候我们需要创建倒影的效果,我们接触最多的都是图片能够创建倒影,而布局依然可以创建倒影。

          二、工具类代码

    import android.graphics.Bitmap;
    import android.graphics.Bitmap.Config;
    import android.graphics.Canvas;
    import android.graphics.LinearGradient;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.PorterDuff.Mode;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Shader.TileMode;
    import android.view.View;
    
    /**
     * 创建图片倒影的方法
     * 
     * @author 
     * */
    public class ImageReflect {
        private static int reflectImageHeight = 90;//倒影的高度
    
        //转化为bitmap
        public static Bitmap convertViewToBitmap(View paramView) {
            paramView.measure(View.MeasureSpec.makeMeasureSpec(0, 0), View.MeasureSpec.makeMeasureSpec(0, 0));
            paramView.layout(0, 0, paramView.getMeasuredWidth(), paramView.getMeasuredHeight());
            paramView.buildDrawingCache();
            return paramView.getDrawingCache();
        }
    
        public static Bitmap createCutReflectedImage(Bitmap paramBitmap, int paramInt) {
            int i = paramBitmap.getWidth();
            int j = paramBitmap.getHeight();
            Bitmap localBitmap2 = null;
            if (j <= paramInt + reflectImageHeight) {
                localBitmap2 = null;
            } else {
                Matrix localMatrix = new Matrix();
                localMatrix.preScale(1.0F, -1.0F);
                // System.out.println(j - reflectImageHeight -
                // paramInt);
                Bitmap localBitmap1 = Bitmap.createBitmap(paramBitmap, 0, j - reflectImageHeight - paramInt, i, reflectImageHeight, localMatrix, true);
                localBitmap2 = Bitmap.createBitmap(i, reflectImageHeight, Bitmap.Config.ARGB_8888);
                Canvas localCanvas = new Canvas(localBitmap2);
                localCanvas.drawBitmap(localBitmap1, 0.0F, 0.0F, null);
                LinearGradient localLinearGradient = new LinearGradient(0.0F, 0.0F, 0.0F, localBitmap2.getHeight(), -2130706433, 16777215, TileMode.CLAMP);
                Paint localPaint = new Paint();
                localPaint.setShader(localLinearGradient);
                localPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
                localCanvas.drawRect(0.0F, 0.0F, i, localBitmap2.getHeight(), localPaint);
                if (!localBitmap1.isRecycled())
                    localBitmap1.recycle();
                System.gc();
            }
            return localBitmap2;
        }
    
        public static Bitmap createReflectedImage(Bitmap paramBitmap, int paramInt) {
            int i = paramBitmap.getWidth();
            int j = paramBitmap.getHeight();
            Bitmap localBitmap2;
            if (j <= paramInt) {
                localBitmap2 = null;
            } else {
                Matrix localMatrix = new Matrix();
                localMatrix.preScale(1.0F, -1.0F);
                Bitmap localBitmap1 = Bitmap.createBitmap(paramBitmap, 0, j - paramInt, i, paramInt, localMatrix, true);
                localBitmap2 = Bitmap.createBitmap(i, paramInt, Bitmap.Config.ARGB_8888);
                Canvas localCanvas = new Canvas(localBitmap2);
                localCanvas.drawBitmap(localBitmap1, 0.0F, 0.0F, null);
                LinearGradient localLinearGradient = new LinearGradient(0.0F, 0.0F, 0.0F, localBitmap2.getHeight(), -2130706433, 16777215, TileMode.CLAMP);
                Paint localPaint = new Paint();
                localPaint.setShader(localLinearGradient);
                localPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
                localCanvas.drawRect(0.0F, 0.0F, i, localBitmap2.getHeight(), localPaint);
            }
            return localBitmap2;
        }
        
        public static Bitmap createReflectedImage(Bitmap originalImage) {
    
            final int reflectionGap = 4;
    
            int width = originalImage.getWidth();
            int height = originalImage.getHeight();
    
            Matrix matrix = new Matrix();
            matrix.preScale(1, -1);
    
            Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
                    height / 2, width, height / 2, matrix, false);
    
            Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
                    (height + height / 2), Config.ARGB_8888);
    
            Canvas canvas = new Canvas(bitmapWithReflection);
    
            canvas.drawBitmap(originalImage, 0, 0, null);
    
            Paint defaultPaint = new Paint();
            canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
    
            canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
    
            Paint paint = new Paint();
            LinearGradient shader = new LinearGradient(0,
                    originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
                            + reflectionGap, 0x70ffffff, 0x00ffffff,
                    TileMode.MIRROR);
    
            paint.setShader(shader);
    
            paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    
            canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
                    + reflectionGap, paint);
    
            return bitmapWithReflection;
        }
    }

      三、这里面有很多种方法,可以自己根据需求去选择。

          调用示例:

          ps:我这是创建多个倒影,所以用了一个for循环。循环操作的是两个图片数组。

    for (int k = 0; k < REFLECT_COUNT; k++) {
                arrayOfImageView[k].setImageBitmap(ImageReflect.createCutReflectedImage(ImageReflect.convertViewToBitmap(imagereflect[k]), 0));
            }
  • 相关阅读:
    2018 ACM 网络选拔赛 徐州赛区
    2018 ACM 网络选拔赛 焦作赛区
    2018 ACM 网络选拔赛 沈阳赛区
    poj 2289 网络流 and 二分查找
    poj 2446 二分图最大匹配
    poj 1469 二分图最大匹配
    poj 3249 拓扑排序 and 动态规划
    poj 3687 拓扑排序
    poj 2585 拓扑排序
    poj 1094 拓扑排序
  • 原文地址:https://www.cnblogs.com/yejiurui/p/3725947.html
Copyright © 2011-2022 走看看