LetterAvatarGenerator
1.2.0indexedGenerates letter avatars as ImageBitmap with precise text measurement, percentage-based sizing, custom font support, pure-canvas rendering, and random color-palette helpers.
Generates letter avatars as ImageBitmap with precise text measurement, percentage-based sizing, custom font support, pure-canvas rendering, and random color-palette helpers.
A Kotlin Multiplatform library that helps in creating letter avatars as ImageBitmap objects for profile placeholders. Built with Compose Multiplatform, it provides a consistent, high-performance rendering engine that works identically across Android, iOS, and other Compose targets.
The library handles text measuring, centering, and scaling automatically using a shared implementation in commonMain, ensuring your avatars look the same on every device.
CanvasDrawScope and TextMeasurer for platform-agnostic drawing.The library is available on Maven Central.
Add the dependency to your module's build.gradle.kts:
repositories {
mavenCentral()
google()
}
dependencies {
implementation("io.github.pranathi-pellakuru:Letter-Avatar-Generator:1.2.0")
}
The library provides a simple API to build avatars directly in your Composable functions.
import androidx.compose.ui.graphics.Color
import com.pranathicodes.letteravatar.rememberAvatarCreator
@Composable
{
avatarCreator = rememberAvatarCreator()
avatarBitmap = remember(avatarCreator) {
avatarCreator
.setLetter()
.setAvatarSize()
.setTextSize()
.setLetterColor(Color.White)
.setBackgroundColor(Color())
.build()
}
Image(
bitmap = avatarBitmap,
contentDescription =
)
}
You can use custom fonts from your resources seamlessly:
val myFontFamily = FontFamily(Font(Res.font.my_custom_font))
avatarCreator.setFontFamily(myFontFamily)
The library includes a RandomColors helper to manage color palettes using Compose Color:
val randomColors = RandomColors().apply {
setColorPairs(listOf(Color.White to Color.Blue, Color.Yellow to Color.Black))
}
// Pick a random pair
val pair = randomColors.getColorPair()
avatarCreator
.setLetterColor(pair.first)
.setBackgroundColor(pair.second)
rememberAvatarCreator(): Composable function that provides a pre-configured creator using the current LocalDensity and TextMeasurer.https://github.com/AmosKorir/AvatarImageGenerator
This project is licensed under the MIT License.
ImageBitmap directly.composeResources via FontFamily.| Method | Description |
|---|
setLetter(letter: Char) | Sets the letter to display |
setTextSize(percentage: Int) | Sets text height as a % of avatar size (0-100, default: 40) |
setAvatarSize(size: Int) | Sets output image size in pixels (default: 180) |
setLetterColor(color: Color) | Sets the letter color |
setBackgroundColor(color: Color) | Sets the background color |
setFontFamily(family: FontFamily) | Sets a custom FontFamily |
build() | Returns ImageBitmap |
Surfaced from shared tags and platforms — no rankings paid for.