Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Image fit X and scale Y

Tags:

android

xml

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?

like image 410
TMH Avatar asked Sep 19 '13 14:09

TMH


2 Answers

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

like image 53
ben75 Avatar answered Oct 23 '22 17:10

ben75


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

like image 39
Sven Niehus Avatar answered Oct 23 '22 19:10

Sven Niehus