Reputation: 11608
Starting point: in my app I let the user pick an image that will be used as the app's background. In case the image's width/height is larger than the device's screen width/height, I want to crop the image so it matches the device dimensions (see picture below).
I tried the method below, but with no luck at all, the resulting image is messed up.
private Bitmap crop(Bitmap bmp) {
int centerW = bmp.getWidth() / 2;
int centerH = bmp.getHeight() / 2;
int startX = centerW
//returns screen width
- GetSettings.getScreenDimensions(getActivity())[0] / 2;
int startY = centerH
//returns screen height
- GetSettings.getScreenDimensions(getActivity())[1] / 2;
return Bitmap.createBitmap(bmp, startX, startY,
GetSettings.getScreenDimensions(getActivity())[0],
GetSettings.getScreenDimensions(getActivity())[1]);
}
I guess I'm misunderstanding the parameters I need to pass to the createBitmap()
method to get a proper result.
So how can I crop a Bitmap
to make it's width/height to match the screen's width/height?
Upvotes: 0
Views: 1611
Reputation: 378
I'm not an android developer, but I would imagine your startX and startY variables are incorrect.
I would think you would want something like this:
startX = (bmp.getWidth() - screenWidth) / 2;
startY = (bmp.getHeight() - screenHeight) / 2;
Then your draw X and Y lengths would just be the screen width and height.
Upvotes: 2