Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fit the large image into small imageview with as good quality as the original image?

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 ?

like image 793
Ajay S Avatar asked Oct 09 '12 16:10

Ajay S


People also ask

How do I resize an image in ImageView to keep the aspect ratio?

However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="..." . src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView.

Which attribute is used to set an image in ImageView?

src: src is an attribute used to set a source file or you can say image in your imageview to make your layout attractive.

How do I change the height and width of a picture in Glide?

Add an explicit width or height to the ImageView by setting layout_width=500dp in the layout file. Call . override(width, height) during the Glide load and explicitly set a width or height for the image such as: GlideApp.


1 Answers

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);
like image 166
Bashar Astifan Avatar answered Nov 05 '22 08:11

Bashar Astifan