代码之家  ›  专栏  ›  技术社区  ›  Saurav Ghimire

在回收器中使用CustomTarget时滑翔问题查看

  •  0
  • Saurav Ghimire  · 技术社区  · 3 年前

    我有一个带有GridLayoutManager的回收器视图来列出图像。当要填充的数据较少时,它可以正常工作,但当数据数量较多时,回收器视图的前半部分会重复回收器视图中的后半部分。如果我使用CustomTarget直接将图像加载到imageView中,则不会出现此问题。如果图像处于横向方向,我需要一个自定义目标来将比例类型设置为FIT_CENTER,如果图像处于纵向模式,则需要将比例类型设为CENTER_CROP。

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            with(itemBinding) {
                imageView.fitVideoWithThumb(data.imageUrl)
            }
        }
    
    fun ImageView.fitVideoWithThumb(
        videoUrl: String
    ) {
        val placeHolder = ColorDrawable(
            ContextCompat.getColor(
                context.applicationContext,
                R.color.grey_200
            )
        )
    
          GlideApp.with(context.applicationContext).clear(this)
          GlideApp.with(context.applicationContext)
          .asBitmap()
          .load(videoUrl) 
          .placeholder(placeHolder)
          .diskCacheStrategy(DiskCacheStrategy.ALL)
          .error(placeHolder)
          .into(object : CustomTarget<Bitmap>() {
                        override fun onResourceReady(
                            resource: Bitmap,
                            transition: Transition<in Bitmap>?
                        ) {
                            val width = resource.width.toDouble()
                            val height = resource.height.toDouble()
                            if (height / width < 1.5) {
                                this@fitVideoWithThumb.scaleType = ImageView.ScaleType.FIT_CENTER
                            } else {
                                this@fitVideoWithThumb.scaleType = ImageView.ScaleType.CENTER_CROP
                            }
                            this@fitVideoWithThumb.setImageBitmap(resource)
                        }
    
                        override fun onLoadCleared(placeholder: Drawable?) {
                        }
                    })
            }
        }
    }
    

    问题是当图像是高质量的并且需要时间来获得尺寸时。

    0 回复  |  直到 3 年前