简体   繁体   English

在GridView上保持背景的长宽比

[英]Keep aspect ratio of background on GridView

I have a image as background on my GridView and I want it to keep its aspect ratio. 我在GridView上有一个图像作为背景,我希望它保持其纵横比。 I want it to match the height of the phone and use the width that matches the aspect ratio. 我希望它与手机的高度匹配,并使用与宽高比匹配的宽度。

Here is the current code: 这是当前代码:

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview"
android:scaleType="centerInside"
android:background="@drawable/gytbg"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:columnWidth="10dp"
android:numColumns="2"
android:verticalSpacing="20dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
android:scaleType="centerCrop"

This should fit the image to the height of the GridView, maintaining the aspect ratio, and crop the remaining width (unless the image's width is smaller than that of the GridView when the height is matched). 这应该使图像适合GridView的高度,保持宽高比,并裁剪剩余的宽度(除非高度匹配时图像的宽度小于GridView的宽度)。

Eg 例如

If you have a GridView that is 400dp in width and 800dp in height 如果您的GridView的宽度为400dp,高度为800dp

An image of 600 x 600 will fit the height and then crop the remaining width. 600 x 600的图像将适合高度,然后裁剪剩余的宽度。

If however the GridView was landscape (400dp in width and 300dp in height) then a 600 x 600 image would fit the width and then crop the height. 但是,如果GridView是横向的(宽度为400dp,高度为300dp),则600 x 600的图像将适合宽度,然后裁剪高度。

If you only ever want it to fit the height (assuming that the height might change programmatically or due to different device's screens), I would suggest making the image considerably wider so that even when presented with a GridView with a very wide aspect ratio, it will choose to fit the height, and crop the width. 如果您只希望它适合高度(假设高度可能会以编程方式更改或由于设备的屏幕不同而改变),我建议您将图像变得更宽,以便即使使用纵横比非常宽的GridView呈现时也可以将选择适合的高度,并裁剪宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM