Fitting ImageView Horizontally

耗尽温柔 提交于 2020-01-02 07:01:17

问题


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>

http://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.

http://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

来源:https://stackoverflow.com/questions/8276728/fitting-imageview-horizontally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!