0

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).

enter image description here

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?

Droidman
  • 10,609
  • 14
  • 85
  • 134
  • This should help you: http://stackoverflow.com/a/9891929/2664466 – Collin Flynn Nov 18 '13 at 17:11
  • What do you mean by "the resulting image is messed up" ? Also as Collin mention, you might need to fit the resulting bitmap in your ImageView – Chewie Nov 18 '13 at 17:17
  • @Chewie it means that the result is just some blurred piece of the original image. I don't use any Views at this point, I create a directory on sd card and save the image there – Droidman Nov 18 '13 at 17:21

1 Answers1

2

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.

Alex311
  • 338
  • 2
  • 10