3

I am developing a sort of ebook application and I am trying to display an image in the following two ways. When the screen is in portrait, I have the image horizontally fitted and vertically centered. When the screen is in landscape, I want the image to fill the screen horizontally with the aspect ratio maintained. Since the images that I am using are longer than they are wide, I have a ScrollView to allow viewing of the entire image.

How should I be scaling my ImageView to achieve this? I know that FIT_CENTER comes close to what I want to do, but since the image is narrower than it is tall, it does not fill the width. I tried FIT_XY with setAdjustViewBounds set to true but that still doesnt maintain my aspect ratio. Does anyone have any ideas?

Here is my code so far:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" 
            android:isScrollContainer="true"
            android:layout_height="wrap_content" 
            android:scrollbars="vertical"
            android:layout_gravity="center_vertical|clip_horizontal"
            android:scrollbarAlwaysDrawVerticalTrack="true" 
            android:id="@+id/Root">
<ScrollView android:id="@+id/ScrollView01" 
            android:tag="scroller"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|clip_horizontal">
<ImageView  android:scaleType="fitCenter" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:id="@+id/imgView" 
            android:adjustViewBounds="true"></ImageView>
</ScrollView>
</LinearLayout>

https://i.stack.imgur.com/9J8IW.png This is in portrait. It is exactly as I want it. The image fits the width of the screen and aspect ratio is maintained.

https://i.stack.imgur.com/Kw68o.png This is in landscape. The image's aspect ratio is maintained and the scrollview works as needed, but I need the imageview to stretch the width of the screen

0 Answers0