[Android] Picasso ładowanie obrazków w tle.

0

Hej, mam taki przypadek: ładuję zdjęcia z sieci w porcjach po 10 (dochodzi z serwera lista 10 urli do jakiś zdjęć). Na ekranie mam 1 image view i chciałbym, żeby to działało tak, że przy pobraniu ich są one już jakoś cachowane. Apka działa tak, że po kliknięciu na button, obrazek aktualnie wyświetlony ma zniknąć i ma się pojawić kolejny. Potem jeśli zostało do załadowania 5 obrazków to jest znowu doładowywane 10 i ma się to w tle scachować. Chodzi o to, żeby nie było przerw w ładowaniu tylko po kliknięciu buttona od razu ma wskoczyć nowy obrazek.

Da się to jakoś ograć z Picasso ?

Pozdrawiam.

1

Ładujesz obrazek i potem możesz odpalić na kolejnych w pętli coś w tym stylu.

Picasso.get()
    .load("adres obrazka")
    .resize(imageView.width, imageView.height) // Opcjonalne, patrz niżej.
    .fetch()

Tylko to jest trochę podchwytliwe. Ważne jest, żeby podać odpowiednie wymiary do resize(), jeżeli korzystasz czegoś w rodzaju fit() przy ładowaniu do ImageView, ze względu na to jak Picasso działa. Jeżeli wymiary tego co załadujesz do pamięci podręcznej i ImageView będą różne, to Picasso wykona kolejne zapytanie i nie skorzysta z pamięci podręcznej.

0

Kurczę zrobiłem tak dla każdego itemka z listy

Picasso.with(requireActivity())
                    .load(url)
                    .centerCrop()
                    .resize(top_picture.width, top_picture.height)
                    .fetch()

i tak potem ładuję:

Picasso.with(requireActivity())
            .load(match.user1.photoUrl)
            .centerCrop()
            .resize(top_picture.width, top_picture.height)
            .into(top_picture)

i niestety nie działa ..

1

Hmmm… U mnie działa.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
  <ImageView
      android:id="@+id/imageView"
      android:layout_width="150dp"
      android:layout_height="150dp"
      />
  <Button
      android:id="@+id/initButton"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="init"
      />
  <Button
      android:id="@+id/switchButton"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="switch"
      />
</LinearLayout>
class MainActivity : Activity() {
  private val images = listOf(
      "http://placehold.it/400x400&text=image1",
      "http://placehold.it/400x400&text=image2",
      "http://placehold.it/400x400&text=image3",
      "http://placehold.it/400x400&text=image4",
      "http://placehold.it/400x400&text=image5"
  )

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val imageView = findViewById<ImageView>(R.id.imageView)
    val initButton = findViewById<Button>(R.id.initButton)
    val switchButton = findViewById<Button>(R.id.switchButton)
    Picasso.setSingletonInstance(Picasso.Builder(this).loggingEnabled(true).build())
    initButton.setOnClickListener {
      Log.d("LOG_TAG", "!!! ON CLICK INIT !!!")
      Picasso.get()
          .load("https://i.imgur.com/DvpvklR.png")
          .fit()
          .into(imageView)
      for (image in images) {
        Picasso.get()
            .load(image)
            .resize(imageView.width, imageView.height)
            .fetch()
      }
    }
    var counter = 0
    switchButton.setOnClickListener {
      Log.d("LOG_TAG", "!!! ON CLICK SWITCH !!!")
      Picasso.get()
          .load(images[counter])
          .fit()
          .into(imageView)
      counter = (counter + 1) % images.size
    }
  }
}
D: !!! ON CLICK INIT !!!
D: Main        created      [R0] Request{https://i.imgur.com/DvpvklR.png resize(394,394)}
D: Main        created      [R1] Request{http://placehold.it/400x400&text=image1 resize(394,394)}
D: Main        created      [R2] Request{http://placehold.it/400x400&text=image2 resize(394,394)}
D: Main        created      [R3] Request{http://placehold.it/400x400&text=image3 resize(394,394)}
D: Dispatcher  enqueued     [R0]+3ms 
D: Main        created      [R4] Request{http://placehold.it/400x400&text=image4 resize(394,394)}
D: Main        created      [R5] Request{http://placehold.it/400x400&text=image5 resize(394,394)}
D: Hunter      executing    [R0]+5ms 
D: Dispatcher  enqueued     [R1]+4ms 
D: Dispatcher  enqueued     [R2]+5ms 
D: Dispatcher  enqueued     [R3]+6ms 
D: Dispatcher  enqueued     [R4]+6ms 
D: Dispatcher  enqueued     [R5]+5ms 
D: Hunter      executing    [R1]+10ms 
D: Hunter      executing    [R2]+12ms 
D: Hunter      decoded      [R0]+237ms 
D: Hunter      transformed  [R0]+238ms 
D: Hunter      executing    [R3]+236ms 
D: Dispatcher  batched      [R0]+238ms for completion
D: Hunter      decoded      [R1]+342ms 
D: Hunter      transformed  [R1]+344ms 
D: Hunter      executing    [R4]+343ms 
D: Dispatcher  batched      [R1]+344ms for completion
D: Hunter      decoded      [R2]+345ms 
D: Hunter      transformed  [R2]+350ms 
D: Dispatcher  batched      [R2]+351ms for completion
D: Hunter      executing    [R5]+349ms 
D: Dispatcher  delivered    [R0]+441ms, [R1]+439ms, [R2]+439ms 
D: Main        completed    [R0]+441ms from NETWORK
D: Main        completed    [R1]+440ms from NETWORK
D: Main        completed    [R2]+439ms from NETWORK
D: Hunter      decoded      [R4]+502ms 
D: Hunter      transformed  [R4]+504ms 
D: Dispatcher  batched      [R4]+504ms for completion
D: Hunter      decoded      [R5]+508ms 
D: Hunter      transformed  [R5]+510ms 
D: Dispatcher  batched      [R5]+511ms for completion
D: Hunter      decoded      [R3]+555ms 
D: Hunter      transformed  [R3]+556ms 
D: Dispatcher  batched      [R3]+557ms for completion
D: Dispatcher  delivered    [R4]+706ms, [R5]+705ms, [R3]+706ms 
D: Main        completed    [R4]+706ms from NETWORK
D: Main        completed    [R5]+705ms from NETWORK
D: Main        completed    [R3]+707ms from NETWORK
D: !!! ON CLICK SWITCH !!!
D: Main        created      [R6] Request{http://placehold.it/400x400&text=image1 resize(394,394)}
D: Main        completed    [R6] from MEMORY
D: !!! ON CLICK SWITCH !!!
D: Main        created      [R7] Request{http://placehold.it/400x400&text=image2 resize(394,394)}
D: Main        completed    [R7] from MEMORY
D: !!! ON CLICK SWITCH !!!
D: Main        created      [R8] Request{http://placehold.it/400x400&text=image3 resize(394,394)}
D: Main        completed    [R8] from MEMORY
D: !!! ON CLICK SWITCH !!!
D: Main        created      [R9] Request{http://placehold.it/400x400&text=image4 resize(394,394)}
D: Main        completed    [R9] from MEMORY
D: !!! ON CLICK SWITCH !!!
D: Main        created      [R10] Request{http://placehold.it/400x400&text=image5 resize(394,394)}
D: Main        completed    [R10] from MEMORY
D: !!! ON CLICK SWITCH !!!
D: Main        created      [R11] Request{http://placehold.it/400x400&text=image1 resize(394,394)}
D: Main        completed    [R11] from MEMORY

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0