【问题标题】:How to use different images for different screen sizes android?如何为不同屏幕尺寸的android使用不同的图像?
【发布时间】:2019-11-21 00:38:44
【问题描述】:

我已将不同尺寸的图片放入 mdpi、hdpi、xhdpi 等。但使用该图像的 My ImageButtons 在每台设备上的大小似乎都相同。

上次我这样做时,它只将图像放在文件夹中。我不记得我到底做了什么。

我想在不同的屏幕上显示不同大小的图像。 我有

  • 5.7寸手机
  • 5.0寸手机
  • 7.0英寸平板电脑

我的应用在所有这些按钮中显示相同大小的图像按钮。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg"
tools:context="test.music.MainActivity">

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dip">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/nowplayingname_txtv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:maxLines="1"
            android:scrollbars="horizontal"
            android:text="Ek Mulaqat-www.songsfarm.ifo"
            android:textColor="@color/title"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/npduration_txtv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:maxLines="1"
            android:text="05:12"
            android:textColor="@android:color/darker_gray" />
    </LinearLayout>

    <ImageButton
        android:id="@+id/settings_imgbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:backgroundTint="@color/bg"
        android:paddingLeft="20dip"
        android:paddingRight="20dip"
        app:srcCompat="@mipmap/menu" />

</LinearLayout>


<ListView
    android:id="@+id/songs_listv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_weight="1"
    android:longClickable="true"
    android:layout_below="@+id/linearLayout3"
    android:layout_above="@+id/song_seekbar" />

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:orientation="horizontal"
    android:paddingBottom="16dp">


    <ImageButton
        android:id="@+id/prev_imgbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:backgroundTint="@color/bg"
        app:srcCompat="@mipmap/prev" />

    <Space
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/pp_imgbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:backgroundTint="@color/bg"
        app:srcCompat="@mipmap/play" />

    <Space
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/next_imgbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:backgroundTint="@color/bg"
        app:srcCompat="@mipmap/next" />


</LinearLayout>

<SeekBar
    android:id="@+id/song_seekbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/linearLayout2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:paddingBottom="16dip"
    android:paddingTop="16dip" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@+id/linearLayout"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/linearLayout3">


    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#D3D3D3" />

</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignTop="@+id/song_seekbar"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">


    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#D3D3D3" />

</LinearLayout>


</RelativeLayout>

注意: 已经很长时间了,但事实证明将文件放在正确的文件夹中是可行的。文件的名称应该完全相同。您可以通过将图像与文件夹中已存在的图像进行比较来验证图像的大小。

【问题讨论】:

  • 你想得到什么效果?以英寸为单位的相同物理尺寸?使用相同百分比的屏幕?还有什么?
  • 我想在不同的屏幕上显示不同尺寸的图像。我有一部 5.7 英寸手机、一部 5.0 英寸手机和一部 7 英寸平板电脑。我的应用在所有这些按钮中显示相同大小的图像按钮
  • 不同的尺寸——不同的物理尺寸,还是逻辑尺寸?例如,您是否希望它们都高 1 英寸,而不管电话如何?或者你想让它们都占据一半的高度?您不太可能只想要随机的不同尺寸。您需要以更详细的方式描述您想要的最终结果,答案因具体内容而异。
  • 正确地说,不是您放入这些文件夹中大小不同的图像,而是更高的分辨率,通常更大只是为了显示更多像素
  • 例如,如果我有一个 1 英寸的屏幕,它显示的图片大小为 0.2 英寸。当屏幕尺寸更改为 10 英寸时,它应该显示 2 英寸的图像。

标签: android imageview android-resources


【解决方案1】:

您可以使用dimenify 插件。它将为不同的屏幕创建标准的尺寸文件夹集,并为这些屏幕转换尺寸值。这样,您的按钮将在每个屏幕上都有自己的大小:)

【讨论】:

  • 如何将此插件添加到android studio??
  • @PapManTwister 只需下载 .jar 文件并将其路径写入文件 --> 设置 --> 磁盘插件。重新启动 IDE 后,单击鼠标右键并选择“创建替代资源文件夹” 注意,我的解决方案将创建替代维度文件夹和值,这意味着您的所有高度、宽度和其他参数必须在标准维度中提取文件夹。
【解决方案2】:

首先要解决的是对“屏幕尺寸”的含义的混淆。您是指物理尺寸(例如 4 英寸宽 vs 7 英寸宽)还是逻辑尺寸(例如 360dp 宽 vs 600dp 宽)?

根据我的经验,很多开发人员关心物理尺寸,没有意识到他们不应该这样做。显示的逻辑尺寸是影响您的应用如何呈现给用户的重要因素。

如果您想为不同的物理尺寸使用不同的图像,使用资源框架没有简单的方法可以做到这一点。您必须使用 DisplayMetrics 对象的 widthPixelsxdpi 字段在 Java 中自己计算屏幕的物理大小,然后编写使用例如R.drawable.my_drawable_four_inchesR.drawable.my_drawable_seven_inches

如果您想为不同的逻辑大小使用不同的图像,资源框架将帮助您。您可以像这样设置您的可绘制资源:

res/
    drawable/
        my_drawable.png
    drawable-sw600dp/
        my_drawable.png

现在,在您的布局和 Java 代码中,您可以简单地引用 @drawable/my_drawableR.drawable.my_drawable,系统会自动为您选择合适的。任何 600dp 或更宽的设备将获得第二个文件,而任何 559dp 或更窄的设备将获得第一个文件。

除此之外,您可能还需要考虑为不同的屏幕分辨率提供同一图像的多个副本。这有点超出了这个答案的范围,但你可以在这里阅读:https://developer.android.com/guide/practices/screens_support.html#density-independence

【讨论】:

    猜你喜欢
    • 2015-02-28
    • 2018-12-19
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多