0

My goal is simple : have a 16/9 ratio image in an imageView. I use a png image which dimensions are 16x9. 2 possibilities for my app :

  • portrait : width fixed, has to scale height
  • landscape : height fixed, has to scale width

The first one works fine with this layout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/welcomeRootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/welcomePilotingFrame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:contentDescription="@null"
            android:scaleType="fitXY"
            android:src="@drawable/black_16_9e" />
    </FrameLayout>

    <!-- Other stuff that aren't relevant -->

</RelativeLayout>

This gives me what i want : my red cornered image is resized

enter image description here

Now the fun part begins : landscape: Here's my layout (symetric off portrait) :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/welcomeLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2" >

        <FrameLayout
            android:id="@+id/welcomePilotingFrame"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#ff00ff00" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:contentDescription="@null"
                android:scaleType="fitXY"
                android:src="@drawable/black_16_9e_border" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/welcomePilotingFrame" />

    </RelativeLayout>

<!-- Other stuff that aren't relevant -->

</LinearLayout>

And now, the imageView understands it has to be all its parent high, but does't resize its width and so doesn't keep my 16/9 ratio !

enter image description here

Am I missing something ?

shinyuX
  • 1,519
  • 1
  • 9
  • 20

1 Answers1

0

Change the layout_width of second framelayout to match_parent. Also scale-type = fitxy changes aspect ratio of the image. Change it to FIT_START

AnswerBot
  • 447
  • 2
  • 7
  • No I can't, there's an element that has to be at the right of the image. And if I go and put fitStart, the image keeps it original size 16x9, and I want it to be as high as the relativeLayout parent – shinyuX Mar 21 '14 at 12:55
  • I said change the width of that second layout(one that is right of image) to match_parent not the image's width – AnswerBot Mar 21 '14 at 13:33
  • Same result : image's height is ok, not resized width. Even if I remove that right frame, imageView doesn't fill width – shinyuX Mar 21 '14 at 13:35
  • 1
    I think this http://stackoverflow.com/questions/18077325/scale-image-to-fill-imageview-width-and-keep-aspect-ratio should help you – AnswerBot Mar 21 '14 at 15:20
  • Indeed i was thinking one layer to high. I changed to resize my image in the onMeasure. Thanks ! – shinyuX Mar 24 '14 at 13:37