【发布时间】:2020-10-08 13:31:46
【问题描述】:
我在image-view 中有一张图像,当我们单击相应的按钮时,我想以不同的比例裁剪,例如 4:3、1:1、9:11 等。
我是新手,不知道如何使用这种比例裁剪图像。
感谢您的帮助。
【问题讨论】:
标签: java android image bitmap imageview
我在image-view 中有一张图像,当我们单击相应的按钮时,我想以不同的比例裁剪,例如 4:3、1:1、9:11 等。
我是新手,不知道如何使用这种比例裁剪图像。
感谢您的帮助。
【问题讨论】:
标签: java android image bitmap imageview
将您的ImageView 放在ConstraintLayout 中。这将使 ImageView 可以访问app:layout_constraintDimensionRatio,您可以在其中指定所需的比率。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="183:124"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
您也可以通过编程方式执行此操作:
imageView.updateLayoutParams<ConstraintLayout.LayoutParams> {
dimensionRatio = "183:70"
}
【讨论】:
updateLayoutParams 来自 androidx 核心 kotlin 扩展。请将implementation "androidx.core:core-ktx:1.3.0" 添加到您的 build.gradle