0

I have one download path. from that same source, I need to place the downloaded image into two image views. one for normal image view, and another is for blurred image view. those two image views overlap each other.

those image views is located as item in the recycler view.

I use the code below to download the image, I use BlurTransformation from wasabeef library in the seconde picasso code below:

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

    val currentEvent = getItem(position)
    val recommendedEventViewHolder = holder as RecommendedEventViewHolder


    Picasso.get()
        .load(currentEvent.posterDownloadPath)
        .into(recommendedEventViewHolder.posterImageView)


    Picasso.get()
        .load(currentEvent.posterDownloadPath)
        .transform(BlurTransformation(mContext,25,3))
        .into(recommendedEventViewHolder.blurryImageView)


}

these code can work, but I am worried. because it seems slow to download those 2 image. I assume it will download 2 images even though the source is only one. so maybe it is not efficient way.

I have tried some way. like using Target interface like below

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

    val currentEvent = getItem(position)
    val recommendedEventViewHolder = holder as RecommendedEventViewHolder



    Picasso.get()
        .load(currentEvent.posterDownloadPath)
        .into(object: Target {
            override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

            }

            override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {

            }

            override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
                recommendedEventViewHolder.posterImageView.setImageBitmap(bitmap)
                recommendedEventViewHolder.blurryImageView.setImageBitmap(bitmap)
            }

        })


}

but if I use this code, sometimes the bitmap from onBitmapLoaded is null, so the image sometimes will not appear.

I have tried the third way using onSuccess listener like the code below:

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        val currentEvent = getItem(position)
        val recommendedEventViewHolder = holder as RecommendedEventViewHolder

        Picasso.get()
            .load(currentEvent.posterDownloadPath)
            .into(recommendedEventViewHolder.posterImageView, object : com.squareup.picasso.Callback {
                override fun onSuccess() {

                    val downloadedBitmap =
                        (recommendedEventViewHolder.posterImageView.drawable as BitmapDrawable).bitmap
                    recommendedEventViewHolder.posterImageView.setImageBitmap(downloadedBitmap)
                    recommendedEventViewHolder.blurryImageView.setImageBitmap(downloadedBitmap)

                }

                override fun onError(e: Exception?) {

                }

            })

    }

but the problem is, the image seems inconsistent, I mean, for a few seconds, the image that should appear in item 1 will appear in item 5 or item 7 if I scroll down the recycler view, even though it eventually located in the right order, If I scroll to the bottom of recycler view

so how to efficiently download image from one path to be used into 2 image views in the same fragment in recycler view using picasso ?

Java is Okay.

sarah
  • 3,819
  • 4
  • 38
  • 80
  • I know this isn't Picasso, but you could try [Glide](https://github.com/bumptech/glide), it caches the image at different levels depending on your situation, so in this case, you can request the image with the same code at different places (same source of course), and it will cache it so that the second request will use the cached version. Everything should work fine out of the box – Jack Apr 22 '19 at 02:34
  • @jackz314 picasso is not cached ? – sarah Apr 22 '19 at 02:42
  • I think you can set it up to cache (maybe by default as well), so if that works for you then great, but I see you are having inconsistency issues, so if it doesn't work well you can try Glide. – Jack Apr 22 '19 at 02:45

0 Answers0