android - 拍摄非常宽的图像并将其裁剪到特定尺寸
问题描述
我的 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
}
这应该是一件非常简单的事情,但我一直在努力解决这个问题。
有人有想法吗?
解决方案
推荐阅读
- azure - 如何创建 Spot 实例 - 使用 Azure 数据工厂 (ADF) 的作业集群 - 链接服务
- javascript - Firestore 管理员未在 Electron App 上进行身份验证
- c++ - 覆盖受保护的变量c ++
- php - 如何适当地划分属于多关系的范围
- python - 如何在 Pywinauto 中检查进度条是否完成
- node.js - 错误“req.body.fileToString 不是函数”
- c# - 无法在 C#“richtextbox”上显示从串口接收的数据
- yocto - 如何在现有的 Yocto 构建中添加层?
- java - Redisson 用于 WebSphere-Liberty 上的 JCache 会话持久性:如何传递 redisson-jcache.yaml?
- swift - 在 IBAction 中设置值时不保存“self”变量