【问题标题】:Query in image size in android studio在android studio中查询图像大小
【发布时间】:2020-06-14 20:53:37
【问题描述】:

你好。有一个 640X480 的球图像。在图像中,如您所见,我将 layout_width 指定为 849px,layout_height 指定为 680 px。我采用了蓝色的背景色。由于指定的 layout_width 远大于 640px(图像的宽度大小),那么为什么图片的左右两边没有被蓝色背景包围。图片的顶部和底部被蓝色背景包围。

XML 代码:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="849px"
        android:layout_height="680px"
        android:background="#2196F3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ball_640x480" />
</androidx.constraintlayout.widget.ConstraintLayout> 

我无法理解的是在图像中,设置 wrap_content 后为什么图像上下仍然有空白?

以下是更新后的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ball_640x480" />
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 请贴出 XML 代码,方便调试
  • 我已经添加了 XML 代码。

标签: android android-studio android-layout imageview pixel


【解决方案1】:

你的情况是因为图片没有ImageView那么大。如果您希望图像完全填充View,您可以使用属性android:scaleType="centerCrop"。您可以关注 this visual guide 了解 ImageView 的不同比例类型,看看它在不同比例类型上的表现。

如果您想在ImageView 上设置边框,我不会使用android:background 属性,因为您的图像将被绘制在背景之上并且可能完全覆盖它。

我的方法是创建一个比图像视图稍大一点的父视图,并且该视图包含背景属性。您可以使用FrameLayout 轻松做到这一点,并将一些android:layout_margin 应用于ImageView,如下例所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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"
    tools:context=".MainActivity">


    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#2196F3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView3"
            android:gravity="center"
            android:layout_margin="16dp"
            android:layout_width="849px"
            android:layout_height="680px"
            app:srcCompat="@drawable/ball_640x480" />
    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout> 

与 cmets 相关的屏幕截图:

有效的代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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"
    tools:context=".MainActivity">


    <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ball_640x480"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout> 

【讨论】:

  • 感谢您的帮助。请看第二张图片并回答我的问题。
  • wrap_content 将使您的ImageView 大小适应正在加载的图像大小。如果您希望您的图片覆盖整个ImageView,请使用android:scaleType="centerCrop"。或者您正在寻找的可能是将父视图的背景设置为 android:background="@drawable/ball_640x480" 如果没有更新的 XML,我无话可说
  • 我已经添加了更新的xml。所以,你的意思是图像上下的空白是我加载的源图像的一部分?因为如果属性设置为wrap_content,那么根本不应该有空白。如果我错了,请纠正我。我认为我们不需要使用 android:scaleType="centerCrop"
  • 通过您更新的布局的最后设置,它应该使图像视图与其源一样大并将其居中;垂直和水平。查看我粘贴的屏幕截图,我有一个类似的用例:它的行为与我描述的完全一样
  • 所以你的意思是在我的形象中你也不知道为什么会出现空白?
猜你喜欢
  • 2020-01-22
  • 2018-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-21
  • 1970-01-01
  • 2023-04-03
  • 2020-11-07
相关资源
最近更新 更多