Created
October 17, 2021 00:54
Circular avatar placeholder with initials in Jetpack Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.annotation.ColorInt | |
import androidx.core.graphics.ColorUtils | |
import kotlin.math.absoluteValue | |
@ColorInt | |
fun String.toHslColor(saturation: Float = 0.5f, lightness: Float = 0.4f): Int { | |
val hue = fold(0) { acc, char -> char.code + acc * 37 } % 360 | |
return ColorUtils.HSLToColor(floatArrayOf(hue.absoluteValue.toFloat(), saturation, lightness)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.foundation.Canvas | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.size | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.remember | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.SolidColor | |
import androidx.compose.ui.text.TextStyle | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.Dp | |
import androidx.compose.ui.unit.dp | |
@Composable | |
fun UserHead( | |
id: String, | |
firstName: String, | |
lastName: String, | |
modifier: Modifier = Modifier, | |
size: Dp = 40.dp, | |
textStyle: TextStyle = MaterialTheme.typography.subtitle1, | |
) { | |
Box(modifier.size(size), contentAlignment = Alignment.Center) { | |
val color = remember(id, firstName, lastName) { | |
val name = listOf(firstName, lastName) | |
.joinToString(separator = "") | |
.uppercase() | |
Color("$id / $name".toHslColor()) | |
} | |
val initials = (firstName.take(1) + lastName.take(1)).uppercase() | |
Canvas(modifier = Modifier.fillMaxSize()) { | |
drawCircle(SolidColor(color)) | |
} | |
Text(text = initials, style = textStyle, color = Color.White) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment