首页 > 解决方案 > 如何在 Android 中缩放视图及其边界?

问题描述

我创建了一个自定义视图,并希望缩放视图及其边界。这可以通过布局实现吗?或者也许我必须在CustomView课堂上改变一些东西?

当我在布局文件中使用scaleXscaleY时,图像可以缩小或放大,但最终边界保持在原来的位置。

这是一个例子。左边是正常大小和正常比例,右边是在和轴上 的CustomView缩放比例。0.5xy在此处输入图像描述

如果它有帮助,它CustomView是由少数人制作ImageViewConstraintLayout。我试过只是改变layout_widthlayout_height确定值,但只有一些视图相应地扩展,而其他视图保持大。

编辑:

这是xml文件CustomView

<?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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clipChildren="false"
android:clipToPadding="false"
android:padding="40dp"
tools:ignore="ContentDescription">


<ImageView
    android:id="@+id/justiceTop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@+id/justiceBase"
    app:layout_constraintEnd_toEndOf="@+id/justiceBase"
    app:layout_constraintStart_toStartOf="@+id/justiceBase"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_justice_scale_top" />

<ImageView
    android:id="@+id/justiceBase"
    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_toBottomOf="@id/justiceTop"
    app:srcCompat="@drawable/ic_justice_scale_base" />

<ImageView
    android:id="@+id/justiceBeam"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:rotation="0"
    app:layout_constraintBottom_toBottomOf="@+id/justiceTop"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/justiceTop"
    app:srcCompat="@drawable/ic_justice_scale_top_beam"
    tools:ignore="ContentDescription" />

<View
    android:id="@+id/leftWeightPivot"
    android:layout_width="1dp"
    android:layout_height="1dp"
    app:layout_constraintCircle="@id/justiceBeam"
    app:layout_constraintCircleAngle="260"
    app:layout_constraintCircleRadius="70dp"
    tools:ignore="MissingConstraints" />

<View
    android:id="@+id/rightWeightPivot"
    android:layout_width="1dp"
    android:layout_height="1dp"
    app:layout_constraintCircle="@id/justiceBeam"
    app:layout_constraintCircleAngle="100"
    app:layout_constraintCircleRadius="70dp"
    tools:ignore="MissingConstraints" />

<ImageView
    android:id="@+id/justiceWeightLeft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="@id/leftWeightPivot"
    app:layout_constraintStart_toStartOf="@id/leftWeightPivot"
    app:layout_constraintTop_toBottomOf="@id/leftWeightPivot"
    app:srcCompat="@drawable/ic_justice_scale_weight" />

<ImageView
    android:id="@+id/justiceWeightRight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="@id/rightWeightPivot"
    app:layout_constraintStart_toStartOf="@id/rightWeightPivot"
    app:layout_constraintTop_toBottomOf="@id/rightWeightPivot"
    app:srcCompat="@drawable/ic_justice_scale_weight" />

如果我将layout_widthand设置layout_height130dp

在此处输入图像描述

标签: androidandroid-studioscaleandroid-custom-view

解决方案


推荐阅读