我想在Recyclerview项中显示图像并动态更改图像的大小。图片是由Glide加载的。
ImageView的layout_width和layout_height为match_parent。
第一次。我通过setLayoutParams更改ImageView的父视图(RelativeLayout)的大小,以更改ImageView的大小。同时,我使用centerCrop将位图加载到ImageView。当我滚动recyclerview时,图像将按比例缩放。
我知道如何解决它。第一个解决方案不是使用centerCrop。第二个解决方案是在onBindViewHolder()上动态设置imageView的layoutParam的宽度和高度。第三种解决方案是为imageView设置setLayoutChangeListener,并在onLayoutChange之后加载url或位图。
但是我找不到这种行为的原因。
第三项的图像应与屏幕水平放置
xml布局代码是
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fl_video_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp">
<RelativeLayout
android:id="@+id/rl_video_body"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img_video_cover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#677655"
android:src="@mipmap/ic_launcher"/>
</RelativeLayout>
</FrameLayout>
下面的代码是onBindViewHolder()函数
if (image.isHorizontal){
holder.rlVideoBody.layoutParams = FrameLayout.LayoutParams(1000,400)
}else{
holder.rlVideoBody.layoutParams = FrameLayout.LayoutParams(400,1000)
}
Glide.with(context).load(image.imageUrl)
.apply(RequestOptions().centerCrop())
.into(holder.imgVideoCover)
if (image.isHorizontal){
holder.rlVideoBody.layoutParams = FrameLayout.LayoutParams(1000,400)
holder.imgVideoCover.layoutParams.width = 1000
holder.imgVideoCover.layoutParams.height = 400
}else{
holder.rlVideoBody.layoutParams = FrameLayout.LayoutParams(400,1000)
holder.imgVideoCover.layoutParams.width = 400
holder.imgVideoCover.layoutParams.height = 1000
}
Glide.with(context).load(image.imageUrl)
.into(holder.imgVideoCover)
if (image.isHorizontal){
holder.rlVideoBody.layoutParams = FrameLayout.LayoutParams(1000,400)
holder.imgVideoCover.layoutParams.width = 1000
holder.imgVideoCover.layoutParams.height = 400
}else{
holder.rlVideoBody.layoutParams = FrameLayout.LayoutParams(400,1000)
holder.imgVideoCover.layoutParams.width = 400
holder.imgVideoCover.layoutParams.height = 1000
}
Glide.with(context).load(image.imageUrl)
.apply(RequestOptions().centerCrop())
.into(holder.imgVideoCover)
if (image.isHorizontal){
holder.rlVideoBody.layoutParams = FrameLayout.LayoutParams(1000,400)
holder.imgVideoCover.layoutParams.width = 1000
holder.imgVideoCover.layoutParams.height = 400
}else{
holder.rlVideoBody.layoutParams = FrameLayout.LayoutParams(400,1000)
holder.imgVideoCover.layoutParams.width = 400
holder.imgVideoCover.layoutParams.height = 1000
}
holder.imgVideoCover.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
Glide.with(context).load(image.imageUrl)
.apply(RequestOptions().centerCrop())
.into(holder.imgVideoCover)
}