如何计算不同设备的图像大小dpi

时间:2016-03-28 11:24:12

标签: android

我有一个带图像的列表视图布局

看起来像这样

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="240dp"
    android:adjustViewBounds="true"
    android:id="@+id/listview_item_imageView"
    android:layout_gravity="center_horizontal|top" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FAC308"
    android:id="@+id/listview_item_title"
    android:text="TITLE"
    android:textSize="20sp"
    android:paddingBottom="40dp"
    android:layout_centerInParent="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#ffffff"
    android:id="@+id/listview_item_subtitle"
    android:paddingTop="40dp"
    android:text="SUBTITLE"
    android:textSize="20sp"
    android:layout_centerInParent="true" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/orangeSeparator"
    android:src="@drawable/orangeseparator"
    android:layout_centerInParent="true" />

如果我在720x1280 240dpi的设备上运行它,它看起来像这样

enter image description here

如果我在720x1280 320dpi的设备上运行它,它看起来像这样

enter image description here

我从互联网上获取图像,因此我无法准备不同的版本。

如何让它在所有设备上看起来相似?

3 个答案:

答案 0 :(得分:0)

在您的容器中

  

机器人:layout_height = “match_parent”

并在图片中:

  

机器人:layout_height = “240dp”

您的容器必须具有相同的高度或“wrap_content”,否则不会有这些空白。

您还可以添加到imageview:

android:scaleType="centerCrop"

另一件事是你可以使用特定的库来处理网络图像的加载。

答案 1 :(得分:0)

您可以使用fitXY Android Developer

android:scaleType="fitXY"

或者您也可以使用Photoshop或任何类似的程序从原始资源创建不同的可绘制资源

答案 2 :(得分:0)

你可以看一下这篇文章。使您的应用支持不同的dpi变体和不同的屏幕类型:Supporting Different Screens

相关问题