Android、XML:只显示布局容器中的部分图像而不改变纵横比

Android, XML: Show only portion of image in layout container without altering aspect ratio

我想为任何 layout 设置背景。 通常,我会继续这样下去:

<LinearLayout
    android:orientation="vertical"
    android:background="#@drawale/somedrawable"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="100dp"/>

但这将始终使背景适合 LinearLayout。但是,如果布局的高度小于我设置为背景的图像怎么办?我 不想 想要破坏图像的宽高比,只是将图像置于布局的中心并让高度重叠,这样它就不再可见了。

澄清一下:

左边是现在的样子,右边是我想成为的样子。由于布局容器小于 imageview 或背景图像,它应该保持居中但只显示适合的内容而不改变纵横比。

你不能在linearlayout中应用scaletype 属性最好使用imageview来实现下面是link如何使用scaleType attribue, to avoid the stratching behaviour use the 9 patch image, follwoing is the link to convert your image into 9patch

在宽度和高度为 match_parent 的线性布局内使用 ImageView 并设置 scaleType="centerCrop"

<ImageView
    android:src="@drawable/somedrawable"
    android:scaleType="centerCrop"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>