【问题标题】:ImageView aspect ratio and adjustViewBounds not working in ConstraintLayoutImageView 纵横比和 adjustViewBounds 在 ConstraintLayout 中不起作用
【发布时间】:2020-06-30 15:27:15
【问题描述】:

我正在回收站视图中的网格布局图像库中工作。 回收站查看项目代码为

    <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:background="@android:color/holo_red_light"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/imageView"
        android:adjustViewBounds="true"
        app:layout_constraintDimensionRatio="9:16"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:src="@drawable/test"
        android:layout_width="0dp"
        android:layout_height="0dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我将 adjustViewBounds 设置为 true、false、缩放类型所有内容,但我无法删除顶部和底部的额外视图。如何做呢。红色是布局中的额外视图。

【问题讨论】:

  • 我也删除了底部约束,但没有工作
  • 您的图像与网格中的单元格的纵横比不同。您将有额外的空间(就像现在一样),或者您将看到图像被裁剪而不调整单元格大小。我猜你会想要调整单元格大小。尝试将 ImageView 的高度设置为wrap_content
  • wrap_content 不工作

标签: android imageview android-constraintlayout


【解决方案1】:

最后我在堆栈溢出中找到了答案,可以根据视图创建可伸缩的位图。代码贴在下面...

fun loadScaledImage(imageView: ImageView, url: String) {
            Glide
                .with(binding.context)
                .asBitmap()
                .load(url)
                .into(object : CustomTarget<Bitmap>() {
                    override fun onLoadCleared(placeholder: Drawable?) {}
                    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                        val width = imageView.measuredWidth
                        val bitmapWidth = resource.width
                        if (bitmapWidth > width){
                            val height = width * resource.height / bitmapWidth
                            val newBitmap = Bitmap.createScaledBitmap(resource, width, height, false)
                            imageView.setImageBitmap(newBitmap)
                        } else {
                            imageView.setImageBitmap(resource)
                        }
                    }
                })
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 2015-10-30
    • 2016-01-02
    • 2018-09-28
    • 1970-01-01
    • 2021-08-20
    相关资源
    最近更新 更多