Displaying images in a mobile application seems simple until you encounter real-world mobile constraints: decoding a 48-megapixel camera photo requires over 190 MB of uncompressed RAM, instantly causing an OutOfMemoryError. Modern image libraries handle asynchronous network fetching, background bitmap decoding, downsampling, and two-tier caching (Memory + Disk).
Coil (Coroutine Image Loader) is built by Instacart specifically for Kotlin and Jetpack Compose. Unlike legacy libraries like Glide and Picasso that relied on heavy reflection, Coil is lightweight (~2,000 methods), uses Kotlin Coroutines, integrates natively with OkHttp, and provides first-class Compose components.
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import coil.request.ImageRequest
@Composable
fun ProfileAvatar(imageUrl: String) {
AsyncImage(
model = ImageRequest.Builder(LocalContext.current)
.data(imageUrl)
.crossfade(true) // Smooth fade-in transition
.placeholder(R.drawable.placeholder_avatar) // Shimmer/Placeholder while downloading
.error(R.drawable.error_image) // Fallback on network failure
.build(),
contentDescription = "User Avatar",
contentScale = ContentScale.Crop,
modifier = Modifier
.size(80.dp)
.clip(CircleShape)
)
}
Broaden your foundational and advanced Android application development skills across our curriculum:
Render async images inside scrollable lists with AsyncImage.
Optimize memory when scrolling thousands of images in lists.
Apply rounded corner shapes and placeholder shimmer to images.
Analyze memory footprint of decoded ARGB_8888 bitmap buffers.