我试图找到在 RecyclerView 中渲染的 ImageView 的“最终”大小,因此我可以使用 picasso 以 TopCrop 方式调整图像。 但是访问 layoutParams.height/layoutParams.width 我得到 -1(我假设这是“match_parent”的值),只给特定的 dp 值得到一个具体的值,但我需要根据设计得到这个值。 还尝试了 drawable.intrinsicWidth/drawable.intrinsicHeight 但似乎这些值仅在 ImageView 已绘制时才有效。 嘿,我能得到那些“最终”值吗?
ImageView所在的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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:layout_width="match_parent"
android:layout_height="160dp"
app:cardCornerRadius="22dp"
app:cardElevation="10dp"
tools:cardBackgroundColor="@color/teal_200"
android:layout_marginBottom="5dp"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/ivItemSuperHeroSearchResultHeroImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="bottom"
android:background="@color/item_superhero_search_result_name_background"
>
<TextView
android:id="@+id/tvItemSuperHeroSearchResultHeroNameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp"
tools:text="Hero name"
android:textColor="@color/white"
android:textAllCaps="true"
/>
</FrameLayout>
</FrameLayout>
</androidx.cardview.widget.CardView>
渲染 RecyclerView 项目的代码
class SearchSuperHeroResultViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val binding = ItemSuperHeroSearchResultBinding.bind(view)
fun render(item: SearchSuperHeroesResultsItemResponse) {
binding.tvItemSuperHeroSearchResultHeroNameLabel.text = item.name
val w = someHowGetWithOf(binding.ivItemSuperHeroSearchResultHeroImage)
val h = someHowGetheightOf(binding.ivItemSuperHeroSearchResultHeroImage)
Picasso
.get()
.load(item.image.url)
.resize(w, h)
.centerCrop(Gravity.TOP)
.into(binding.ivItemSuperHeroSearchResultHeroImage)
}
}
提前致谢