处理图像宽高比的正确且简洁的方法是什么?
在我的应用程序中,如果我旋转屏幕,我会丢失宽高比并且我的图像看起来很难看 - 查看这些比较我的应用程序和 Play 商店的图像 - http://imgur.com/a/GriwP#0
我是这样的:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center" >
<Button
android:id="@+id/btnStark"
android:layout_marginLeft="4dp"
android:layout_marginRight="2dp"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@drawable/stark"
android:gravity="center|bottom"
android:layout_gravity="top" />
<Button
android:id="@+id/btnLannister"
android:layout_marginLeft="2dp"
android:layout_marginRight="4dp"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@drawable/stark"
android:gravity="center|bottom"
android:layout_gravity="top" />
</LinearLayout>
我应该如何处理高度?我应该设置一个固定值吗?或者有什么方法可以自动保持纵横比?
我使用的图像是 400x300 像素
最佳答案
考虑使用 ImageViews
而不是按钮 - 它们可以像按钮一样可点击。您可以使用 scaleType
属性设置图像的缩放方式。
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
例如,对于 CENTER_INSIDE:
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).
所以,像这样:
<ImageView
android:id="@+id/btnStark"
android:layout_marginLeft="4dp"
android:layout_marginRight="2dp"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:src="@drawable/stark"
android:scaleType="centerInside"
android:gravity="center|bottom"
android:layout_gravity="top" />
兰尼斯特有债必还。
关于android - 如何处理不同分辨率和旋转的图像纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17301060/