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 ?
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.
src: src is an attribute used to set a source file or you can say image in your imageview to make your layout attractive.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With