Rishabh Agrawal
Rishabh Agrawal

Reputation: 901

How To Resize Bitmap Without Aspect Ratio in Android

I am new in bitmap.I know how can i resize or scale bitmap in android.But problem is suppose my image is 100x500 or any height & width.Now i want to resize it in square like 100x100.How it is possible

Kindly help me.

Upvotes: 0

Views: 3179

Answers (3)

A B Vijay Kumar
A B Vijay Kumar

Reputation: 869

Made a few modifications to the code from wsanville..and it worked for me Note that I am using the minimum scale (taking what is the least, so that the whole bitmap can be rendered in the screen..if I take max, then it might go beyond the screen

        int sourceWidth = mBitmap.getWidth();
        int sourceHeight = mBitmap.getHeight();
        float xScale = (float) newWidth / sourceWidth;
        float yScale = (float) newHeight / sourceHeight;

        float scale = Math.min(xScale, yScale);

        //get the resulting size after scaling
        float scaledWidth = scale * sourceWidth;
        float scaledHeight = scale * sourceHeight;

        //figure out where we should translate to
        float dx = (newWidth - scaledWidth) / 2;
        float dy = (newHeight - scaledHeight) / 2;

        Matrix defToScreenMatrix = new Matrix();


        defToScreenMatrix.postScale(scale, scale);
        defToScreenMatrix.postTranslate(dx, dy);

        mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, sourceWidth, sourceHeight, defToScreenMatrix, false);

Upvotes: 0

Grimmace
Grimmace

Reputation: 4041

int dstWidth = 100;
int dstHeight = 100;
boolean doFilter = true;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, doFilter);

Upvotes: 1

wsanville
wsanville

Reputation: 37516

For this simple case, the most reasonable thing would be to translate your source image down to the middle, and draw your Bitmap again on a new Canvas. This type of resize is called a center crop in Android. The idea of a center crop is to result in the largest image that fills the entire bounds, and does not change the aspect ratio.

You can implement this yourself, along with other types of resizing and scaling. Basically, you use a Matrix to post your changes, like scaling and moving (translating), and then draw your original Bitmap on a Canvas that takes the Matrix into account.

Here's a method I adopted from another answer here (can't find the original post to properly give credit):

public static Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth)
{
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();
    float xScale = (float) newWidth / sourceWidth;
    float yScale = (float) newHeight / sourceHeight;
    float scale = Math.max(xScale, yScale);

    //get the resulting size after scaling
    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;

    //figure out where we should translate to
    float dx = (newWidth - scaledWidth) / 2;
    float dy = (newHeight - scaledHeight) / 2;

    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
    Canvas canvas = new Canvas(dest);
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    matrix.postTranslate(dx, dy);
    canvas.drawBitmap(source, matrix, null);
    return dest;
}

Upvotes: 7

Related Questions