【问题标题】:How to make an image support different screens sizes or densities in Android?如何使图像在 Android 中支持不同的屏幕尺寸或密度?
【发布时间】:2016-09-27 05:21:20
【问题描述】:

我制作了一个包含 3 个图像的 XML 文件。首先,即使我使用“dp”作为图像尺寸,我也不清楚为什么不同屏幕尺寸上的图像尺寸不同。 所以接下来我所做的是,我为不同的屏幕尺寸制作了不同的布局文件。我看到的还有一个问题是,从 3.5 英寸到 5 英寸的大多数屏幕都是由 normal\something.xml 处理的。并且3.5英寸屏幕出现的图像尺寸与5英寸屏幕不同。

这是正常的 XML 文件\something.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#add8e6"
android:orientation="vertical"
android:id="@+id/mood">

<TextView
    android:layout_width="match_parent"
    android:layout_height="15dp"
    />


<TextView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textSize="33sp"
    android:text="How's your Mood ?"
    android:gravity="center_horizontal"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="30dp"
    />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="15dp"
    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal"
        android:id="@+id/happyLayout"
        android:background="#85a9b4">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:id="@+id/happyRadio"
            android:textSize="30sp"
            />
        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/happy"
            android:layout_marginLeft="30dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal"
        android:id="@+id/sickLayout">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:id="@+id/sickRadio"/>
        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/sick"
            android:layout_marginLeft="30dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal"
        android:id="@+id/sadLayout">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:id="@+id/sadRadio"/>
        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/sad"
            android:layout_marginLeft="30dp"/>
    </LinearLayout>




    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="#f4880d"
        android:text="ADD MOOD"
        android:textColor="#ffffff"
        android:textSize="30sp"/>

</LinearLayout>

以上文件适用于普通屏幕尺寸。对于大屏幕和小屏幕,我只是将图像的尺寸更改为更低或更高的“dp”值(如果这种方法不正确,请纠正我)。

如果您能建议我应该遵循的方法来处理不同屏幕尺寸和密度的图像尺寸,那就太好了。

【问题讨论】:

  • 是否已按密度添加图片,即mdpi、hdpi或xhdpi?
  • 根据屏幕大小将图像放置在不同密度的文件夹中,即drawable(mdpi)、drawable(hdpi)、drawable(xhdpi)。而密度是这样的 mdpi(1.0 Baseline), hdpi(1.5), xhdpi(2.0)
  • 所以如果我像你说的那样使用不同尺寸的图像,那么不同的图像将用于屏幕尺寸 3.7 和 5.7"?我问是因为这两种屏幕尺寸在 Android 开发中都被认为是正常的这两种屏幕尺寸都使用工具和相同的布局文件 normal/something.xml。
  • 有关不同密度的 Android 设备的信息。点击这里-stackoverflow.com/a/44924525/4082923

标签: android image size screen


【解决方案1】:

使用this 之类的工具,您可以在其中为所有屏幕生成图像。另请参阅official docs 以获得更多帮助。

对于图片,相应地放置您的图片

res/drawable-mdpi/graphic.png         // bitmap for medium-density
res/drawable-ldpi/graphic.png         // bitmap for large-density
res/drawable-hdpi/graphic.png         // bitmap for high-density
res/drawable-xhdpi/graphic.png        // bitmap for extra-high-density
res/drawable-xxhdpi/graphic.png       // bitmap for extra-extra-high-density

还要记住

xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp

【讨论】:

  • 我试过这个。屏幕尺寸 3.7" 和 5.7" 正在拍摄相同的图像,即中等密度。同样的图像在一台设备上看起来不错,但在另一台设备上却不行。你能推荐点什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-17
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
相关资源
最近更新 更多