Glide之TransformationUtils

Glide的TransformationUtils文档

通过Glide的TransformationUtils简化可使用的类

public class TransformationUtils {
    public static final int PAINT_FLAGS = Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG;
    private static final Paint DEFAULT_PAINT = new Paint(PAINT_FLAGS);

    private static final int CIRCLE_CROP_PAINT_FLAGS = PAINT_FLAGS | Paint.ANTI_ALIAS_FLAG;
    private static final Paint CIRCLE_CROP_SHAPE_PAINT = new Paint(CIRCLE_CROP_PAINT_FLAGS);
    private static final Paint CIRCLE_CROP_BITMAP_PAINT;

    static {
        CIRCLE_CROP_BITMAP_PAINT = new Paint(CIRCLE_CROP_PAINT_FLAGS);
        CIRCLE_CROP_BITMAP_PAINT.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    }

    private TransformationUtils(){}

    /**
     * CenterCrop 将图片等比例缩放,让图像的短边与ImageView的边长度相同,
     * 即不能留有空白,缩放后截取中间部分进行显示
     */
    public static Bitmap centerCrop(Bitmap inBitmap,int width,int height){
        if(inBitmap.getWidth() == width && inBitmap.getHeight() == height){
            return inBitmap;
        }
        final float scale;
        final float dx;
        final float dy;
        Matrix matrix = new Matrix();
        if(inBitmap.getWidth() * height > width * inBitmap.getHeight()){//横向面积 > 纵向面积
            scale = (float)height / (float)inBitmap.getHeight();//高是短边
            dx = (width - inBitmap.getWidth() * scale) * 0.5f;
            dy = 0;
        }else {
            scale = (float)width / (float)inBitmap.getWidth();//宽是短边
            dx = 0;
            dy = (height - inBitmap.getHeight() * scale) * 0.5f;
        }
        matrix.setScale(scale, scale);
        matrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
        Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        result.setHasAlpha(inBitmap.hasAlpha());
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(inBitmap, matrix, DEFAULT_PAINT);
        canvas.setBitmap(null);
        return result;
    }

    /**
     * ImageView的默认状态,大图等比例缩小,使整幅图能够居中显示在ImageView中,
     * 小图等比例放大,同样要整体居中显示在ImageView中
     * @return
     */
    public static Bitmap fitCenter(Bitmap inBitmap, int width, int height){
        if (inBitmap.getWidth() == width && inBitmap.getHeight() == height) {
            return inBitmap;
        }
        final float widthPercentage = width / (float) inBitmap.getWidth();
        final float heightPercentage = height / (float) inBitmap.getHeight();
        final float minPercentage = Math.min(widthPercentage, heightPercentage);
        int targetWidth = Math.round(minPercentage * inBitmap.getWidth());
        int targetHeight = Math.round(minPercentage * inBitmap.getHeight());
        if (inBitmap.getWidth() == targetWidth && inBitmap.getHeight() == targetHeight) {
            return inBitmap;
        }
        targetWidth = (int) (minPercentage * inBitmap.getWidth());
        targetHeight = (int) (minPercentage * inBitmap.getHeight());
        Bitmap toReuse = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
        toReuse.setHasAlpha(inBitmap.hasAlpha());
        Matrix matrix = new Matrix();
        matrix.setScale(minPercentage, minPercentage);
        Canvas canvas = new Canvas(toReuse);
        canvas.drawBitmap(inBitmap, matrix, DEFAULT_PAINT);
        canvas.setBitmap(null);
        return toReuse;
    }

    /**
     * 将图片大小大于ImageView的图片进行等比例缩小,
     * 直到整幅图能够居中显示在ImageView中,小于ImageView的图片不变,直接居中显示。
     */
    public static Bitmap centerInside(Bitmap inBitmap, int width, int height) {
        if (inBitmap.getWidth() <= width && inBitmap.getHeight() <= height) {
            return inBitmap;
        } else {
            return fitCenter(inBitmap, width, height);
        }
    }
    public static Bitmap circleCrop(Bitmap inBitmap, int destWidth, int destHeight) {
        int destMinEdge = Math.min(destWidth, destHeight);
        float radius = destMinEdge / 2f;

        int srcWidth = inBitmap.getWidth();
        int srcHeight = inBitmap.getHeight();

        float scaleX = destMinEdge / (float) srcWidth;
        float scaleY = destMinEdge / (float) srcHeight;
        float maxScale = Math.max(scaleX, scaleY);
        float scaledWidth = maxScale * srcWidth;
        float scaledHeight = maxScale * srcHeight;
        float left = (destMinEdge - scaledWidth) / 2f;
        float top = (destMinEdge - scaledHeight) / 2f;
        RectF destRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
        Bitmap toTransform = getAlphaSafeBitmap(inBitmap);
        Bitmap result = Bitmap.createBitmap(destMinEdge, destMinEdge, Bitmap.Config.ARGB_8888);
        result.setHasAlpha(true);
        Canvas canvas = new Canvas(result);
        canvas.drawCircle(radius, radius, radius, CIRCLE_CROP_SHAPE_PAINT);
        canvas.drawBitmap(toTransform, null, destRect, CIRCLE_CROP_BITMAP_PAINT);
        canvas.setBitmap(null);
        return result;
    }
    private static Bitmap getAlphaSafeBitmap(Bitmap maybeAlphaSafe) {
        Bitmap argbBitmap = Bitmap.createBitmap(maybeAlphaSafe.getWidth(),
                maybeAlphaSafe.getHeight(), Bitmap.Config.ARGB_8888);
        new Canvas(argbBitmap).drawBitmap(maybeAlphaSafe, 0, 0, null);
        return argbBitmap;
    }


    public static Bitmap roundedCorners(Bitmap inBitmap,int roundingRadius) {
        // Alpha is required for this transformation.
        if (roundingRadius <= 0) {
            throw new IllegalArgumentException("roundingRadius must be greater than 0.");
        }
        Bitmap toTransform = getAlphaSafeBitmap(inBitmap);
        Bitmap result = Bitmap.createBitmap(toTransform.getWidth(), toTransform.getHeight(), Bitmap.Config.ARGB_8888);
        result.setHasAlpha(true);
        BitmapShader shader = new BitmapShader(toTransform, Shader.TileMode.CLAMP,
                Shader.TileMode.CLAMP);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(shader);
        RectF rect = new RectF(0, 0, result.getWidth(), result.getHeight());
        Canvas canvas = new Canvas(result);
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        canvas.drawRoundRect(rect, roundingRadius, roundingRadius, paint);
        canvas.setBitmap(null);
        return result;
    }
}

这样就实现了类似ImageView 的 ScaleType的效果。

你可能感兴趣的:(Glide之TransformationUtils)