【问题标题】:How to keep same image size on different devices in android如何在android的不同设备上保持相同的图像大小
【发布时间】:2016-08-20 02:07:54
【问题描述】:

我使用imageView 显示活动图像。此图像大小不应根据设备屏幕而改变。但这不起作用。我想为每个设备使用图像的实际大小。以下是我尝试过的。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/circle"
        android:id="@+id/imageView"
        android:scaleType="center"/>

</RelativeLayout>

如何在不缩放每个设备屏幕的情况下使用相同的图像尺寸? 有什么想法。

谢谢。

【问题讨论】:

    标签: android android-imageview image-scaling


    【解决方案1】:

    使用英寸而不是像素大小

    例如,android:layout_width=".5in"

    <ImageView
            android:layout_width="1in"
            android:layout_height="1in"
            android:src="@drawable/circle"
            android:id="@+id/imageView"
            android:scaleType="center"/>
    

    https://developer.android.com/guide/practices/screens_support.html

    【讨论】:

    • 你能举个例子吗?我对您的共享链接感到困惑。
    • @Barrier ,所以我不明白我想看到哪些变化。这段代码说,每个设备中的这个拾取器将有 1 英寸宽和 1 英寸高。你想要这个还是别的?
    【解决方案2】:

    您必须为每个屏幕尺寸设计布局,或者您应该在不同的 dimes 文件夹中声明您的 dimen,而不是 wrap_content,因为它会根据屏幕密度缩放您的图像,

    您必须使用尺寸创建的值文件夹是:

    values-hdpi/dimens.xml------for hd handsets
    values-xhdpi/dimens.xml-----for 5" screens xHD
    values-xxhdpi/dimens.xml----for nexus 5X
    values-xxxhdpi/dimens.xml----for nexus 6 and 6P devices
    values-sw720dp/dimens.xml-----for 9" tablets
    values-sw800dp-hdpi/dimens.xml--------for 10" tablets
    values-sw600dp/dimens.xml------for 7" tablets
    

    【讨论】:

    • 使用大量 xml 非常不舒服和烦人
    • 不,这不是我所期望的。
    • 我不想缩放图像或 imageView 。我只想在每台设备上保持其实际大小。
    • 好的,那么你能做的就是利用这个库:https://github.com/intuit/sdp
    • @Barrier 问题是由于手机屏幕的屏幕密度,您的图像将自动缩放...如果屏幕是 mdpi,那么它将是您的图像的精确尺寸,如果是 hdpi,那么它将是您图像的 1.5 倍,如果是 xxhdpi,则为 2 倍,依此类推..
    【解决方案3】:

    dp 中使用固定宽度和高度。

    DP 是 dp 单位到屏幕像素的转换很简单:px = dp * (dpi / 160)。例如,在 240 dpi 的屏幕上,1 dp 等于 1.5 个物理像素。在定义应用程序的 UI 时,您应该始终使用 dp 单位,以确保您的 UI 在不同密度的屏幕上正确显示。

    <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/circle"
            android:id="@+id/imageView"
            android:scaleType="center"/>
    

    有关 DP、SP、px 的更多信息。请查看this

    【讨论】:

      【解决方案4】:

      为此,您必须为 ImageView 定义静态固定高度和宽度,然后使用以下属性:-

      imgview.setScaleType(ScaleType.FIT_XY);
      

      【讨论】:

        【解决方案5】:

        只需使用 photoshop 或 gimp 等软件检查 jour 图像的实际大小,然后以这种方式在 imageview 中设置实际宽度和高度(例如:50px x 40px):

           <ImageView
            android:layout_width="50px"
            android:layout_height="40px"
            android:src="@drawable/circle"
            android:id="@+id/imageView"
            android:scaleType="center"/>
        

        【讨论】:

          【解决方案6】:

          就这样吧!!高度是固定的,所以你需要提供一个高度值,宽度必须是 match_parent。它对我来说工作得很好。

          <ImageView
              android:id="@+id/img_saving_image"
              android:layout_width="match_parent"
              android:layout_height="90dp"
              android:layout_centerHorizontal="true"
              android:src="@drawable/ic_cross"
              android:scaleType="fitCenter"
                       or
              android:scaleType="fitXY"
              />
          

          【讨论】:

            【解决方案7】:

            将您的图片放入drawabledrawable-nodpi。 Android 不会缩放您的图像大小。您的代码无需更改。

            【讨论】:

            • 那么,如何提供图片src的路径?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-10-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多