首页 > 解决方案 > Android 图像缩放与约束布局中的背景图像相同

问题描述

在我的应用程序中,我有一个地图图像和包含地图片段的图像视图。在任何设备屏幕中,地图片段的大小应与其在地图图像中的相应部分相同。地图图像也包含在约束内的图像视图中布局。

在此处输入图像描述

如何使地图图像周围的图像与主图像中的图像大小相同/相等。我使用“Batch Drawable Import”插件创建了图像集。当我将图像放入 drawable-xxhdpi drawable-xhdpi drawable-hdpi drawable-ldpi 文件夹时,它们在设备上运行时会显示更大或更小的部分。这些图像片段将被拖放到最终创建地图的地图视图中。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:id="@+id/parentcl"
    tools:context=".Act2_1">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/x2"
        app:layout_constraintBottom_toTopOf="@+id/imageView35"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/imageView35"
        app:layout_constraintVertical_bias="0.498" />

    <ImageView
        android:id="@+id/imageView33"
        android:layout_width="163dp"
        android:layout_height="0dp"
        android:layout_marginBottom="38dp"
        android:layout_marginEnd="368dp"
        android:layout_marginTop="92dp"
        app:layout_constraintBottom_toTopOf="@+id/imageView35"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/x1" />

</android.support.constraint.ConstraintLayout>

以上是我正在尝试的测试代码。BG 图像大小为 2048px X 1496px,其中红色框大小为 400px X 300px。外部图像视图中的图像块为 400px X 300px。我希望外部图像大小与背景图像中的图像大小相同。实现上述要求的最佳解决方案是什么。我尝试将图像放在应用程序内的所有可绘制文件夹中,但没有成功。然后我只在 drawable 和 drawable-nodpi-v4 文件夹中添加了一个大型图像,这也没有给出好的结果。

在此处输入图像描述

标签: androidandroid-drawableimage-resizingandroid-constraintlayout

解决方案


推荐阅读