Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Jetpack Compose does not have a direct built-in CircularImageView
like some Android libraries (e.g., CircleImageView from hdodenhof) for the traditional View system. However, creating a circular image in Compose is straightforward with the help of the Modifier.clip()
function.
Here's how you can achieve a circular ImageView in Jetpack Compose:
Ensure you have the necessary Compose and Accompanist library for loading images. In your build.gradle
file:
implementation 'androidx.compose.ui:ui:YOUR_COMPOSE_VERSION' implementation 'androidx.compose.ui:ui-tooling:YOUR_COMPOSE_VERSION' implementation 'com.google.accompanist:accompanist-coil:YOUR_ACCOMPANIST_VERSION'
Here's how you can display a circular image using Compose:
import androidx.compose.foundation.Image import androidx.compose.foundation.background 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.graphics.Color import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.unit.dp import coil.compose.rememberImagePainter import androidx.compose.ui.tooling.preview.Preview @Composable fun CircularImage(url: String) { Image( painter = rememberImagePainter(data = url), contentDescription = null, // Consider providing a meaningful description contentScale = ContentScale.Crop, // Crop the image if not square modifier = Modifier .size(100.dp) // You can adjust the size as needed .clip(CircleShape) .background(Color.Gray) // Background color when the image is loading or failed ) } @Preview @Composable fun PreviewCircularImage() { CircularImage(url = "https://example.com/path/to/your/image.jpg") }
In this code, the CircularImage
function uses the Coil library via Accompanist to load images. The Modifier.clip(CircleShape)
is what provides the circular clipping, and ContentScale.Crop
ensures the image scales to fill its size while maintaining its aspect ratio, cropping any parts of the image that extend beyond its bounds.
If you use another image loading library (e.g., Glide), you'd adjust the method of obtaining the image accordingly, but the principle of creating a circular image using the .clip(CircleShape)
modifier remains the same.
Creating circular images in Android with Jetpack Compose:
Image
composable. This involves using a circular clip shape or mask.import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.res.painterResource @Composable fun CircularImageExample() { Image( painter = painterResource(id = R.drawable.ic_circular_image), contentDescription = null, // Provide a content description modifier = Modifier .size(100.dp) .clip(CircleShape) ) }
Jetpack Compose Circular ImageView example in Kotlin:
Image
composable with a circular clip shape.import androidx.compose.foundation.Image import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.res.painterResource @Composable fun CircularImageViewExample() { Image( painter = painterResource(id = R.drawable.ic_circular_image), contentDescription = null, // Provide a content description modifier = Modifier.size(100.dp).clip(CircleShape) ) }
Styling circular images with Jetpack Compose in Android:
Modifier
parameter to customize appearance.import androidx.compose.foundation.Image import androidx.compose.foundation.border import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.res.painterResource @Composable fun StyledCircularImage() { Image( painter = painterResource(id = R.drawable.ic_circular_image), contentDescription = null, // Provide a content description modifier = Modifier .size(100.dp) .clip(CircleShape) .border(2.dp, MaterialTheme.colorScheme.primary, CircleShape) ) }
Customizing circular ImageView appearance in Jetpack Compose:
import androidx.compose.foundation.Image import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.painterResource @Composable fun CustomCircularImage() { Box( modifier = Modifier .size(100.dp) .clip(CircleShape) .background(Color.Gray) ) { Image( painter = painterResource(id = R.drawable.ic_circular_image), contentDescription = null, // Provide a content description modifier = Modifier.fillMaxSize(), contentScale = ContentScale.Crop, contentAlignment = Alignment.Center ) } }
Circular ImageView states and interactions in Jetpack Compose:
import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.MaterialTheme import androidx.compose.material3.ripple.rememberRipple import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.painterResource @Composable fun CircularImageWithInteraction() { val interactionSource = remember { MutableInteractionSource() } Image( painter = painterResource(id = R.drawable.ic_circular_image), contentDescription = null, // Provide a content description modifier = Modifier .size(100.dp) .clip(CircleShape) .background( color = if (interactionSource.isPressed) { Color.Gray } else { Color.Transparent } ) .clickable( interactionSource = interactionSource, indication = rememberRipple(bounded = false) ) { /* Handle click event */ } ) }
Using Material Design circular images in Jetpack Compose:
import androidx.compose.foundation.Image import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.CircleShape import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Icon import androidx.compose.material3.icons.Icons import androidx.compose.material3.icons.filled.AccountCircle import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier @Composable fun MaterialDesignCircularImage() { Icon( imageVector = Icons.Default.AccountCircle, contentDescription = null, // Provide a content description modifier = Modifier.size(100.dp).clip(CircleShape), tint = MaterialTheme.colorScheme.primary ) }
Circular ImageView layout and positioning in Jetpack Compose:
Row
, Column
, or Box
.import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.res.painterResource @Composable fun CircularImageLayout() { Box( modifier = Modifier .size(150.dp) .clip(CircleShape) .background(Color.Gray) ) { Image( painter = painterResource(id = R.drawable.ic_circular_image), contentDescription = null, // Provide a content description modifier = Modifier.fillMaxSize(), contentScale = ContentScale.Crop ) } }
Animated circular ImageView in Android with Jetpack Compose:
AnimatedVisibility
. Apply animations to attributes like alpha, scale, or translation.import androidx.compose.animation.core.animateFloatAsState import androidx.compose.foundation.Image import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.painterResource @Composable fun AnimatedCircularImage() { val alpha by animateFloatAsState(targetValue = if (/* Some condition */) 1f else 0.5f) Image( painter = painterResource(id = R.drawable.ic_circular_image), contentDescription = null, // Provide a content description modifier = Modifier .size(100.dp) .clip(CircleShape) .background(Color.Gray.copy(alpha = alpha)) ) }
Masking images to create circular ImageView in Jetpack Compose:
Image
composable.import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.Shape import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp @Composable fun CircularImageWithMask() { val imageBitmap: ImageBitmap = painterResource(id = R.drawable.ic_circular_image) .painter.toBitmap().asImageBitmap() Image( bitmap = imageBitmap, contentDescription = null, // Provide a content description modifier = Modifier .size(100.dp) .mask(CircleShape) .background(Color.Gray) ) }