首页 > 解决方案 > 拍摄非常宽的图像并将其裁剪到特定尺寸

问题描述

我的 ViewHolder 中有一个非常宽的图像。使用的默认 ImageView 行为centerCrop应该是这样的图像 -

在此处输入图像描述

但我想裁剪它,所以它会像这样集中在左侧 -

在此处输入图像描述

需要考虑的事情是这个 ViewHolder 经常被调整大小。

这是我的 ViewHolder XML & Adapter。请注意,我fitXY仅用于显示目的,这不是我真正想要的,因此请随意忽略该行。-

<?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:id="@+id/vendors_row_item_root_layout"
    android:layout_width="152dp"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:id="@+id/search_image_contact_cardview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        app:cardCornerRadius="8dp"
        tools:layout_height="160dp">

        <ImageView
            android:id="@+id/vendors_row_item_imageview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            tools:src="@drawable/kikiandggcover" />


    </androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>

适配器 -

class VendorAdapter(private val miniVendorModels: List<MiniVendorModel>, private val context: Context) : RecyclerView.Adapter<VendorsHolder>() {

    companion object {
        const val EXTRA_VENDOR_MODEL = "EVM"
    }

    private val vendorsHoldersList = mutableListOf<VendorsHolder>()

    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): VendorsHolder {
        val view = LayoutInflater.from(viewGroup.context).inflate(R.layout.fragment_marketplace_vendor_row_item, viewGroup, false)
        val vendorsHolder = VendorsHolder(view)
        vendorsHoldersList.add(vendorsHolder)
        vendorsHolder.rootLayout.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
            vendorsHolder.rootLayout.updateLayoutParams {
                this.width = 250 + bottom
            }
        }
        vendorsHolder.rootLayout.pivotX = 0f
        vendorsHolder.rootLayout.pivotY = vendorsHolder.rootLayout.measuredHeight.toFloat()
        return vendorsHolder
    }

    override fun onBindViewHolder(vendorsHolder: VendorsHolder, i: Int) {
        val model = miniVendorModels[i]
        Picasso.get().load(model.bannerPicture).into(vendorsHolder.vendorImageView)
        vendorsHolder.vendorImageView.setOnClickListener { v: View? ->
            try {
                val intent = Intent(context, VendorPageActivity::class.java)
                intent.putExtra(EXTRA_VENDOR_MODEL, model)
                context.startActivity(intent)
            } catch (e: Exception) {
                e.printStackTrace()
                Toast.makeText(context, ResourceHelper.getString(R.string.marketplace_vendor_unavailable), Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun getItemCount(): Int = miniVendorModels.size

}

这应该是一件非常简单的事情,但我一直在努力解决这个问题。

有人有想法吗?

标签: androidimageviewandroid-imageviewcrop

解决方案


推荐阅读