How to Resize a Bitmap in Android?
Change:
profileImage.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
To:
Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));
How to resize Image in Android?
Try:
Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);
or:
resized = Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.8), (int)(yourBitmap.getHeight()*0.8), true);
Android resize bitmap keeping aspect ratio
You should try using a transformation matrix built for ScaleToFit.CENTER
. For example:
Matrix m = new Matrix();
m.setRectToRect(new RectF(0, 0, b.getWidth(), b.getHeight()), new RectF(0, 0, reqWidth, reqHeight), Matrix.ScaleToFit.CENTER);
return Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
How to resize image (Bitmap) to a given size?
Bitmap scaledBitmap = scaleDown(realImage, MAX_IMAGE_SIZE, true);
Scale down method:
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize,
boolean filter) {
float ratio = Math.min(
(float) maxImageSize / realImage.getWidth(),
(float) maxImageSize / realImage.getHeight());
int width = Math.round((float) ratio * realImage.getWidth());
int height = Math.round((float) ratio * realImage.getHeight());
Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
height, filter);
return newBitmap;
}
Android: Resizing Bitmaps without losing quality
Try Bitmap.createScaledBitmap.
It also has an option to filter the source.
Scaled Bitmap maintaining aspect ratio
What about this:
Bitmap background = Bitmap.createBitmap((int)width, (int)height, Config.ARGB_8888);
float originalWidth = originalImage.getWidth();
float originalHeight = originalImage.getHeight();
Canvas canvas = new Canvas(background);
float scale = width / originalWidth;
float xTranslation = 0.0f;
float yTranslation = (height - originalHeight * scale) / 2.0f;
Matrix transformation = new Matrix();
transformation.postTranslate(xTranslation, yTranslation);
transformation.preScale(scale, scale);
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(originalImage, transformation, paint);
return background;
I added a paint
to filter the scaled bitmap.
Bitmap scale destroy quality
I have found a solution here and it works great.
Here is the part of the code for scaling the image:
public Bitmap getResizedBitmap(Bitmap bitmap, int newWidth, int newHeight) {
Bitmap resizedBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
float scaleX = newWidth / (float) bitmap.getWidth();
float scaleY = newHeight / (float) bitmap.getHeight();
float pivotX = 0;
float pivotY = 0;
Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);
Canvas canvas = new Canvas(resizedBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG));
return resizedBitmap;
}
Related Topics
How to Load an Imageview by Url in Android
How to Dynamically Add Suggestions to Autocompletetextview With Preserving Character Status
How to Implement Onbackpressed() in Fragments
How to Determine When Fragment Becomes Visible in Viewpager
How to Obtain Crash-Data from My Android Application
Multiple Dex Files Define Landroid/Support/V4/Accessibilityservice/Accessibilityserviceinfocompat
Android Toolbar Center Title and Custom Font
Android Studio Installation on Windows 7 Fails, No Jdk Found
How to Support Different Screen Size in Android
Android "Only the Original Thread That Created a View Hierarchy Can Touch Its Views."
Creating a System Overlay Window (Always on Top)
Failed to Resolve: Com.Android.Support:Cardview-V7:26.0.0 Android
How to Pick an Image from Gallery (Sd Card) For My App
Saving and Reading Bitmaps/Images from Internal Memory in Android
How to Disable Orientation Change on Android