How to Resize a Bitmap in Android?

后端 未结 16 2055
有刺的猬
有刺的猬 2020-11-22 03:13

I have a bitmap taken of a Base64 String from my remote database, (encodedImage is the string representing the image with Base64):

profileImage          


        
16条回答
  •  遇见更好的自我
    2020-11-22 03:54

    Try this: This function resizes a bitmap proportionally. When the last parameter is set to "X" the newDimensionXorY is treated as s new width and when set to "Y" a new height.

    public Bitmap getProportionalBitmap(Bitmap bitmap, 
                                        int newDimensionXorY, 
                                        String XorY) {
        if (bitmap == null) {
            return null;
        }
    
        float xyRatio = 0;
        int newWidth = 0;
        int newHeight = 0;
    
        if (XorY.toLowerCase().equals("x")) {
            xyRatio = (float) newDimensionXorY / bitmap.getWidth();
            newHeight = (int) (bitmap.getHeight() * xyRatio);
            bitmap = Bitmap.createScaledBitmap(
                bitmap, newDimensionXorY, newHeight, true);
        } else if (XorY.toLowerCase().equals("y")) {
            xyRatio = (float) newDimensionXorY / bitmap.getHeight();
            newWidth = (int) (bitmap.getWidth() * xyRatio);
            bitmap = Bitmap.createScaledBitmap(
                bitmap, newWidth, newDimensionXorY, true);
        }
        return bitmap;
    }
    

提交回复
热议问题