Android bitmap适配设置大小;设置圆角、翻转、按比例伸缩

2023-01-08 16:45:30

        原始裁剪
     Bitmap bitmap_img = Bitmap.createBitmap(bitmap_max, 0, 0, modelImage.getImgWidth(), modelImage.getImgHeight());





    /**
     *根据给定的宽和高进行拉伸,使得图片的一端一定是满铺的 
     *
     * @param bitmap
     *            源
     * @param dst_w
     *            输出宽度
     * @param dst_h
     *            输出高度
     * @return
     */
    public static Bitmap imageScaleMin(Bitmap bitmap, int dst_w, int dst_h) {
        int src_w = bitmap.getWidth();
        int src_h = bitmap.getHeight();
        float scale_w = ((float) dst_w) / src_w;
        float scale_h = ((float) dst_h) / src_h;

        float min = Math.min(scale_h, scale_w);
//        最大边需要增加的倍数到屏幕边缘,从而达到长宽放大相同倍数,图片不会被压缩,
        Matrix matrix = new Matrix();
        matrix.postScale(min, min);
        Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, src_w, src_h, matrix,
                true);

        return dstbmp;
    }


    /**
     * 以宽高为约束,将图片放大,且裁剪中心区域;
     *
     * 图片大于屏幕的宽高时,只需要中心区域的部分;
     * 由于bimap是等比按照屏幕缩放的,所有有一端一定是与屏幕相匹配的
     * @param bitmap
     * @return
     */
    public static Bitmap imageCenterArea(Bitmap bitmap,int dst_w, int dst_h){

        int src_w = bitmap.getWidth();
        int src_h = bitmap.getHeight();
        float scale_w = ((float) dst_w) / src_w;
        float scale_h = ((float) dst_h) / src_h;
        boolean is_w=false;//是否以w放大

        float max = Math.max(scale_h, scale_w);
        if (max==scale_w){
            is_w=true;
        }else {
            is_w=false;
        }
        Matrix matrix = new Matrix();

        matrix.postScale(max, max);
        Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, src_w, src_h, matrix, true);

        int bitmap_w=dstbmp.getWidth();
        int bitmap_h=dstbmp.getHeight();

        Bitmap bitmapCenter=null;
        if (is_w){
//            以宽边的放大系数放大的;
            int start_y=bitmap_h-dst_h;
            bitmapCenter = Bitmap.createBitmap(dstbmp, 0, start_y, bitmap_w, dst_h);
        }else {
            int start_x=bitmap_w-dst_w;
            bitmapCenter = Bitmap.createBitmap(dstbmp, start_x, 0, dst_w, bitmap_h);
        }

        return bitmapCenter;
    }


    // 放大缩小图片
    public static Bitmap scaleTo(final Bitmap bitmapOrg, final int newWidth, final int newHeight) {
        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        if (width == 0 || height == 0) {
            return bitmapOrg;
        }
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        return Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);
    }

/**
 *根据给定的宽和高进行拉伸
 * @param bitmap
 *            源
 * @param dst_w
 *            输出宽度
 * @param dst_h
 *            输出高度
 * @return
 */
public static Bitmap imageScale(Bitmap bitmap, int dst_w, int dst_h) {
    int src_w = bitmap.getWidth();
    int src_h = bitmap.getHeight();
    float scale_w = ((float) dst_w) / src_w;
    float scale_h = ((float) dst_h) / src_h;
    Matrix matrix = new Matrix();
    matrix.postScale(scale_w, scale_h);
    Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, src_w, src_h, matrix,
            true);
    return dstbmp;
}

/**
 * 按比例缩放图片,正方形
 *
 * @param origin 原图
 * @param ratio  比例
 * @return 新的bitmap
 */
public static Bitmap scaleBitmapWithSquare(Bitmap origin, float ratio) {
    if (origin == null) {
        return null;
    }
    int width = origin.getWidth();
    int height = origin.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(ratio, ratio);

    if (width>height){
        width=height;
    }else {
        height=width;
    }
    Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
    if (newBM.equals(origin)) {
        return newBM;
    }
    origin.recycle();
    return newBM;
}

/**
 * 按比例缩放图片
 *
 * @param origin 原图
 * @param ratio  比例
 * @return 新的bitmap
 */
public static Bitmap scaleBitmap(Bitmap origin, float ratio) {
    if (origin == null) {
        return null;
    }
    int width = origin.getWidth();
    int height = origin.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(ratio, ratio);

    Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
    if (newBM.equals(origin)) {
        return newBM;
    }
    origin.recycle();
    return newBM;
}

/**
 * 裁剪
 *
 * @param bitmap 原图
 * @return 裁剪后的图像
 */
public static Bitmap cropBitmap(Bitmap bitmap) {
    int w = bitmap.getWidth(); // 得到图片的宽,高
    int h = bitmap.getHeight();
    int cropWidth = w >= h ? h : w;// 裁切后所取的正方形区域边长
    cropWidth /= 2;
    int cropHeight = (int) (cropWidth / 1.2);
    return Bitmap.createBitmap(bitmap, w / 3, 0, cropWidth, cropHeight, null, false);
}

/**
 * 选择变换
 *
 * @param origin 原图
 * @param alpha  旋转角度,可正可负
 * @return 旋转后的图片
 */
public static Bitmap rotateBitmap(Bitmap origin, float alpha) {
    if (origin == null) {
        return null;
    }
    int width = origin.getWidth();
    int height = origin.getHeight();
    Matrix matrix = new Matrix();
    matrix.setRotate(alpha);
    // 围绕原地进行旋转
    Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
    if (newBM.equals(origin)) {
        return newBM;
    }
    origin.recycle();
    return newBM;
}


/**
 * 偏移效果
 * @param origin 原图
 * @return 偏移后的bitmap
 */
public static Bitmap skewBitmap(Bitmap origin) {
    if (origin == null) {
        return null;
    }
    int width = origin.getWidth();
    int height = origin.getHeight();
    Matrix matrix = new Matrix();
    matrix.postSkew(-0.6f, -0.3f);
    Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
    if (newBM.equals(origin)) {
        return newBM;
    }
    origin.recycle();
    return newBM;
}

/**
    设置圆形bitmap
*/
public static Bitmap transform(Bitmap bitmap) {
        int size = Math.min(bitmap.getWidth(), bitmap.getHeight());

        int x = (bitmap.getWidth() - size) / 2;
        int y = (bitmap.getHeight() - size) / 2;

        Bitmap squaredBitmap = Bitmap.createBitmap(bitmap, x, y, size, size);
        if (squaredBitmap != bitmap) {
            bitmap.recycle();
        }

        Bitmap bitmap2 = Bitmap.createBitmap(size, size, bitmap.getConfig());

        Canvas canvas = new Canvas(bitmap2);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);

        float r = size/2f;
        canvas.drawCircle(r, r, r, paint);

        squaredBitmap.recycle();
        return bitmap2;
    }

    /**
     * 设置圆角
     */
    public static Bitmap GetRoundedCornerBitmap(Bitmap bitmap) {
        try {
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(),
                    bitmap.getHeight());
            final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(),
                    bitmap.getHeight()));
            final float roundPx = 14;
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(Color.BLACK);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

            final Rect src = new Rect(0, 0, bitmap.getWidth(),
                    bitmap.getHeight());

            canvas.drawBitmap(bitmap, src, rect, paint);
            return output;
        } catch (Exception e) {
            return bitmap;
        }
    }
  • 作者:jian11058
  • 原文链接:https://blog.csdn.net/jian11058/article/details/103140215
    更新时间:2023-01-08 16:45:30