5
votes

i am getting bitmap from gallery / camera and trying to fit in to image view of size 200 dp * 200dp

I had to scale the bitmap to get out of memory issues. Now after scaling the image is not exactly fitting imageview.

The width is fine but height is only half. Is there a way to make the bitmap fit imageview without changing imageview size and without strtching image.

fitxy will do but the image is stretched. i followed this answer

2
try to set scaleType as centrecrop.amit singh
setting a image to a fixed width and height will always affect the aspect ratio.You can set either width or height so that it will maintain the aspect ratio.Spring Breaker

2 Answers

0
votes

I think you need to create new Bitmap to fit ImageView sizes, and you must need to create new Bitmap proportioanlly, like whatever height it is, width must be in same ratio with height and then you need to set that bitmap there. Another way is give,

android:adjustViewBounds="true"

It will automatically scale the image proportionally.

0
votes

If you are looking for a solution using xml, I created a selector and assigned my imageView background to that:

android:background="@drawable/image_selector"

In my selector I setup a bitmap item and set its gravity to "fill":

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item>
       <bitmap android:gravity="fill"
               android:src="@drawable/kitchen_background"/>
   </item>


</selector>