Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a circular image in Android Jetpack Compose?

Let's say I have a rectangular avatar image like the one below, how can I force it to be drawn as a circle in Jetpack Compose?

enter image description here

like image 710
Valeriy Katkov Avatar asked Feb 02 '21 17:02

Valeriy Katkov


People also ask

How do I make an image round Android?

Simply put a circular_crop. png in your drawable folder which is in the shape of your image dimensions (a square in my case) with a white background and a transparent circle in the center. You can use this image if you have want a square imageview. Just download the picture above.

Does jetpack compose use Recyclerview?

Directly in jetpack compose does not support recycler view like the android app.

What is Androidx compose?

Build better apps faster with. Jetpack Compose. Jetpack Compose is Android's modern toolkit for building native UI. It simplifies and accelerates UI development on Android. Quickly bring your app to life with less code, powerful tools, and intuitive Kotlin APIs.

How do you load a drawable image in jetpack compose?

To work with images in Jetpack Compose we will use Image Composable, which contains differnt properties to load images from drawable folder, load bitmaps and also we can load images from network or Url. ImageView is also commonly used to apply tints to an image and handle image scaling.


2 Answers

There's a clip modifier which can be applied to any composable as well as the Image, just pass a CircleShape into it:

Image(
    painter = painterResource(R.drawable.sample_avatar),
    contentDescription = "avatar",
    contentScale = ContentScale.Crop,            // crop the image if it's not a square
    modifier = Modifier
        .size(64.dp)
        .clip(CircleShape)                       // clip to the circle shape
        .border(2.dp, Color.Gray, CircleShape)   // add a border (optional)
)

enter image description here

You can use any other shape to clip the image, for example CircleShape it's just RoundedCornerShape(percent = 50). Let's try RoundedCornerShape(percent = 10):

enter image description here

like image 69
Valeriy Katkov Avatar answered Sep 28 '22 02:09

Valeriy Katkov


Also, you may try a

implementation "com.github.skydoves:landscapist-glide:1.3.6"

By using Modifier.clip(CircleShape)

GlideImage(
            modifier = Modifier
                    .width(50.dp)
                    .height(50.dp)
                    .clip(CircleShape)
                    .clickable(enabled = true, onClick = onClick),
            imageModel = "https://avatars.githubusercontent.com/u/27887884?v=4",
                // Crop, Fit, Inside, FillHeight, FillWidth, None
            contentScale = ContentScale.Crop,
                // shows an image with a circular revealed animation.
            circularReveal = CircularReveal(duration = 250),
                // shows a placeholder ImageBitmap when loading.
            placeHolder = ImageBitmap.imageResource(R.drawable.avater),
                // shows an error ImageBitmap when the request failed.
            error = ImageBitmap.imageResource(id = R.drawable.avater)
            )

For more components visit LandScapist

like image 38
James Christian Kaguo Avatar answered Sep 28 '22 00:09

James Christian Kaguo