android - 如何处理不同分辨率和旋转的图像纵横比

标签 android android-layout android-image

处理图像宽高比的正确且简洁的方法是什么?

在我的应用程序中,如果我旋转屏幕,我会丢失宽高比并且我的图像看起来很难看 - 查看这些比较我的应用程序和 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/

相关文章:

java - 搜索栏监听器

android - getExtras 和 getBundleExtras 有什么区别?

android-layout - TextView 文本在左侧被截断,而不是被省略号缩短

android - 如何访问内部存储的图像

android - 如何将图像上传到 Firebase 存储?

java - 获取 Facebook 好友性别

Android 'Couldn' t expand remoteViews for ...' 以编程方式创建布局时

android - 播放钛金属声音未找到错误功能

android - 从android存储器中选择图像进入android-crop库时,图像会旋转?

android - 应用程序在 android 中显示图像很慢