Android Crop Center of Bitmap
This can be achieved with: Bitmap.createBitmap(source, x, y, width, height)
if (srcBmp.getWidth() >= srcBmp.getHeight()){
dstBmp = Bitmap.createBitmap(
srcBmp,
srcBmp.getWidth()/2 - srcBmp.getHeight()/2,
0,
srcBmp.getHeight(),
srcBmp.getHeight()
);
}else{
dstBmp = Bitmap.createBitmap(
srcBmp,
0,
srcBmp.getHeight()/2 - srcBmp.getWidth()/2,
srcBmp.getWidth(),
srcBmp.getWidth()
);
}
How to crop Bitmap Center like imageview?
Your question is a bit short of information on what you want to accomplish, but I guess you have a Bitmap and want to scale that to a new size and that the scaling should be done as "centerCrop" works for ImageViews.
From Docs
Scale the image uniformly (maintain the image's aspect ratio) so that
both dimensions (width and height) of the image will be equal to or
larger than the corresponding dimension of the view (minus padding).
As far as I know, there is no one-liner to do this (please correct me, if I'm wrong), but you could write your own method to do it. The following method calculates how to scale the original bitmap to the new size and draw it centered in the resulting Bitmap.
Hope it helps!
public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;
// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
return dest;
}
Android crop bitmap
It's just like below:
// x refers to width, y refers to height
//first find startx, starty, endx, endy
Bitmap blueArea = Bitmap.createBitmap(bitmap, startx, starty, endx, endy);
Android: How to center crop images without scaling
You should just use ScaleType.CENTER. This will perform no scaling, it will just center the image behind the image frame. Anything falling outside the ImageView will be cropped.
Crop image to square - Android
You can use
Bitmap dst = Bitmap.createBitmap(src, startX, startY, dstWidth, dstHeight);
from the documentation:
Returns an immutable bitmap from the specified subset of the source
bitmap. The new bitmap may be the same object as source, or a copy may
have been made. It is initialized with the same density as the
original bitmap.
Here you can find the documentation
Related Topics
Importing Google-Play-Service Library Showing a Red X Next to This Reference Android
Android Sample Bluetooth Code to Send a Simple String via Bluetooth
Get Content Uri from File Path in Android
Android Service Stops When App Is Closed
Formula Px to Dp, Dp to Px Android
Onconfigurationchanged Not Getting Called
Meaning of Choreographer Messages in Logcat
Zxing Camera in Portrait Mode on Android
Meaning of Top, Ascent, Baseline, Descent, Bottom, and Leading in Android's Fontmetrics
How Set Spannable Object Font with Custom Font
Custom Filtering in Android Using Arrayadapter
Calculate Compass Bearing/Heading to Location in Android
Android Canvas: Drawing Too Large Bitmap
Android Linearlayout:Add Border with Shadow Around a Linearlayout
Pass Data from Activity to Service Using an Intent