5

I have this ImageView

    <ImageView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/offer_img" />

I'm loading images into this ImageView from a URL and I want the image to always fit X (width of the screen) and readjust the height to keep the aspect ratio. Do I need some Java to do this or can I do it through XML alone?

Gianmarco
  • 2,536
  • 25
  • 57
TMH
  • 6,096
  • 7
  • 51
  • 88
  • and what do you want when the image.height/screen.height > image.width/screen.width (let's say image dimensions are width=10px ; height=1000px) ? (keeping aspect ratio implies cropping the image, but how ?) – ben75 Sep 19 '13 at 15:13
  • The image dimensions wont be that far out, there all around the 8:5 ratio as the website where they are uploaded to crops them to that. – TMH Sep 19 '13 at 15:18

2 Answers2

4

Try this:

<ImageView 
    android:scaleType="centerInside"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/offer_img" />

You will obtain what you expect only if original image ratio is around the 8:5 ratio

ben75
  • 29,217
  • 10
  • 88
  • 134
1

You could try it with android:scaleType

You could use the method that is the answer in this question: Java image resize, maintain aspect ratio

Community
  • 1
  • 1
Sven Niehus
  • 487
  • 5
  • 24
  • Which would I need? There doesn't seem to be any descriptions on the dev page. – TMH Sep 19 '13 at 15:09
  • I don't know either, but I would say that 'fitXY' or 'fitStart' sounds fine – Sven Niehus Sep 19 '13 at 15:13
  • I've gone through each of one of those scale types on none do the job. Maybe because I'm loading them from the web I might have to do some Java to sort this out. – TMH Sep 19 '13 at 15:14
  • You could also set android:scaleType to fitStart and get the new resolution of the picture by your own (calculate it out) [Aspect Ratio Calculator](http://andrew.hedges.name/experiments/aspect_ratio/). I will edited my post to show what i mean. – Sven Niehus Sep 19 '13 at 15:17