【问题标题】:How to crop image with different ratio in android?如何在android中裁剪不同比例的图像?
【发布时间】:2020-10-08 13:31:46
【问题描述】:

我在image-view 中有一张图像,当我们单击相应的按钮时,我想以不同的比例裁剪,例如 4:3、1:1、9:11 等。 我是新手,不知道如何使用这种比例裁剪图像。

感谢您的帮助。

【问题讨论】:

    标签: java android image bitmap imageview


    【解决方案1】:

    将您的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"
    }
    

    【讨论】:

    • 是否还需要添加其他属性,例如 app:layout_constrainedHeight 和 width?
    • 不,如果您只想明确限制一个或另一个,这是可选的。
    • 它让我无法解析符号 updateLayoutParams
    • 如果我设置 xml 属性,您的代码可以正常工作,但是当我单击按钮时如何更新图像视图布局参数? @Alqueraf
    • updateLayoutParams 来自 androidx 核心 kotlin 扩展。请将implementation "androidx.core:core-ktx:1.3.0" 添加到您的 build.gradle
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 2021-07-24
    • 2011-09-21
    • 1970-01-01
    • 2021-08-17
    • 2015-09-02
    相关资源
    最近更新 更多