1

I'm trying to create a scaled bitmap which fits in the Custom View but whenever I perform the operation the width gets cropped while the height of the bitmap fits into the custom view.

bitmapq = Bitmap.createScaledBitmap(bitmap,(custHeight*bitmap.getWidth())/bitmap.getHeight(),custHeight, true);

Custom View height - 1089. Custom View width- 1180. Width of bitmap - 2368. Height of bitmap - 4208

zek54
  • 415
  • 3
  • 20

1 Answers1

0

If you'd like the scaled bitmap to be of the exact size of your custom view regardless of its aspect ratio, you can use this:

bitmapq = Bitmap.createScaledBitmap(bitmap, cusWidth, custHeight, true);

In this way, the scaled bitmap exacly fits your custom view's boundaries.

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • The image gets stretched with this code.I want to maintain the aspect ratio even if it leaves some space on sides of the bitmap. – zek54 May 16 '16 at 06:34
  • @zek54 Which class does your custom view extend? If it extends `ImageView` so you won't need to scale/crop the bitmap manually, it takes care of this work by itself. – frogatto May 16 '16 at 07:01
  • @zek54 Also see [this question](http://stackoverflow.com/questions/24961797/android-resize-bitmap-keeping-aspect-ratio). It may be helpful. – frogatto May 16 '16 at 07:04