googlemaps / googlemaps/android-maps-compose
How to show images downloaded from network efficiently?
- Lingua principale
- Kotlin
- Stelle
- 1.3k
- Fork
- 181
- Merge medio
- 2g 23h
- PR unite (30g)
- 18
Descrizione
Basically what I am doing is downloading the images from server and making an icon with the icon generator. After all the icons has been fully generated then I am putting the markers in the map. But from large list of markers it will take too much time to draw the markers. So how to do this in an optimized way?
```@Composable
fun CustomMarker(
blipsList: List,
iconMap: MutableMap,
hasImageLoaded: Boolean,
event: (HomeEvents) -> Unit,
markerClick: (blipId: String) -> Unit
) {
val context = LocalContext.current
val iconGenerator = IconGenerator(context)
val inflatedView = View.inflate(context, R.layout.custom_marker, null)
val userImage = inflatedView.findViewById(R.id.userImageView)
iconGenerator.setBackground(null)
iconGenerator.setContentView(
inflatedView
)
for (blip in blipsList) {
Glide.with(LocalContext.current)
.asDrawable()
.circleCrop()
.load("${Constants.BASE_URL}${blip.user.photo}")
.into(object : CustomTarget() {
override fun onLoadFailed(errorDrawable: Drawable?) {
super.onLoadFailed(errorDrawable)
val resource =
AppCompatResources.getDrawable(context, R.drawable.person)?.toBitmap()
val circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.resources, resource)
circularBitmapDrawable.isCircular = true
userImage.setImageDrawable(circularBitmapDrawable)
iconMap[blip.id] = iconGenerator.makeIcon()
if (iconMap.size == blipsList.size) {
event(
HomeEvents.UserImageLoadedEvent(
true
)
)
}
}
override fun onResourceReady(
resource: Drawable,
transition: Transition?
) {
userImage.setImageDrawable(resource)
iconMap[blip.id] = iconGenerator.makeIcon()
if (iconMap.size == blipsList.size) {
event(
HomeEvents.UserImageLoadedEvent(
true
)
)
}
}
override fun onLoadCleared(placeholder: Drawable?) {
}
})
}
if (hasImageLoaded) {
for ((i, blip) in blipsList.withIndex()) {
Marker(
state = MarkerState(
LatLng(blip.lat, blip.lng)
),
icon = BitmapDescriptorFactory.fromBitmap(
iconMap[blip.id] ?: iconGenerator.makeIcon()
),
tag = i,
onClick = { mark ->
Log.v("Blip", blipsList[mark.tag as Int].id)
markerClick.invoke(blipsList[mark.tag as Int].id)
return@Marker true
}
)
}
}
}
```
I tried to recompose the markers each time the image is downloaded but it got stuck in infinite recompose.
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Inizia dal composable CustomMarker e traccia il modo in cui i callback delle immagini aggiornano iconMap, hasImageLoaded e la composizione dei marker. Riproduci la ricomposizione infinita e il rendering lento con una blipsList di grandi dimensioni, quindi considera completato il lavoro quando i marker vengono caricati e visualizzati senza il ciclo di ricomposizione né ridisegni eccessivi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- android, kotlin
- Ambito
- mobile-dev, performance
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100