https://kotlinlang.org logo
#compose
Title
# compose
j

jonc

02/10/2023, 11:41 PM
Was wondering if anybody has seen scroll jank when loading multiple vector images in a single
LazyColumn
item using
rememberAsyncImagePainter
or even
painterResource
? I tried experimenting with this in a simple project and have seen lots of jank. Will show a snippet of how I structured the Composable. Will note that storing the painters outside of the
LazyColumn
significantly improves scrolling.
Copy code
package com.example.composeperformancetests

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import coil.compose.rememberAsyncImagePainter
import com.example.composeperformancetests.ui.theme.ComposePerformanceTestsTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposePerformanceTestsTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    val resources = listOf(
                        R.drawable.ic_baseline_add_chart_24,
                        R.drawable.ic_baseline_add_circle_24,
                        R.drawable.ic_baseline_align_horizontal_right_24,
                        R.drawable.ic_baseline_all_inbox_24,
                        R.drawable.ic_baseline_add_reaction_24,
                        R.drawable.ic_baseline_card_membership_24,
                        R.drawable.ic_baseline_backup_table_24,
                        R.drawable.ic_baseline_camera_24
                    )
                    val resources2 = resources.reversed()
                    LazyColumn(modifier = Modifier.fillMaxSize()) {
                        repeat(times = 100) {
                            item {
                                Row {
                                    resources.forEach {
                                        Icon(
                                            modifier = Modifier.size(25.dp),
                                            painter = rememberAsyncImagePainter(model = it),
                                            contentDescription = null
                                        )
                                    }
                                }
                            }
                            item {
                                Row {
                                    resources2.forEach {
                                        Icon(
                                            modifier = Modifier.size(25.dp),
                                            painter = rememberAsyncImagePainter(model = it),
                                            contentDescription = null
                                        )
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
screen-20230210-155310.mp4
8 Views