Fit the large image into small imageview with as g

2019-03-20 14:03发布

问题:

Hello I am working on one good android app where I need to fit the large bitmap into small size imageview. I am using imageview of size 300dp X 300dp to display large image 1024 X 780

How can I make the large image into small with as good quality as the original image ?

回答1:

Try this :

public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);
    // RECREATE THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;

}

Easy to use :

Bitmap bmResized=getResizedBitmap(yourBitmap,newHeight,newWidth);

And you get Your Resized Image

Note : if you want to resize Resources Image Use this to convert to Bitmap :

Bitmap bmOrginal=BitmapFactory.decodeResource(this.getResources(), R.drawble.yourRes);
Bitmap bmResized=getResizedBitmap(bmOrginal,newHeight,newWidth);

Then set the Resized Image :

image.setImageBitmap(bmResized);


回答2:

Did you try im.setScaleType(ScaleType.FIT_XY)



回答3:

@Nikola comment answered to my question. I am writing this in the answer so that it would good for future readers

You should make a Thumbnail image of size 300x300 but preserving its aspect ratio. You can draw larger images on a canvas centered with some color filling the rest of the space.