An android library that helps in creating a letter avatar as bitmap to use as a placeholder for profile. Includes customisation like setting colors to Background and letter , you can also set your custom color pairs to choose randomly
To include LetterAvatarGenerator dependency you need to add JitPack as repository in settings.gradle.kts
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven {
setUrl("https://jitpack.io")
}
}
}
Add the following dependency to your gradle file.
implementation 'com.github.Pranathi-pellakuru:LetterAvatarGenerator:VERSION'
NOTE: Here Color class is from android.graphics library
Avatar creator class
val image = AvatarCreator(this).setLetter('U')
.setTextSize(25)
.setAvatarSize(180)
.setLetterColor(Color.GRAY)
.setBackgroundColor(Color.BLACK)
.build()
If you have color pairs
val randomColors = RandomColors()
randomColors.setColorPairs(listOf(Pair(Color.WHITE,Color.CYAN), Pair(Color.MAGENTA,Color.RED), Pair(Color.GRAY,Color.BLACK)))
val colorPair = randomColors.getColorPair()
val image = AvatarCreator(this).setLetter('P')
.setLetterColor(colorPair.first)
.setBackgroundColor(colorPair.second)
.build()
different set of colors for background and letter
val randomColors = RandomColors()
randomColors.setLetterColors(listOf(Color.WHITE,Color.MAGENTA,Color.GRAY))
randomColors.setBackgroundColors(listOf(Color.CYAN,Color.RED,Color.BLACK))
val image1 = AvatarCreator(this).setLetter('P')
.setLetterColor(randomColors.getLetterColor())
.setBackgroundColor(randomColors.getBackgroundColor())
.build()
use the function setFont(typeface:Typeface) to which you can pass the font in your resource folder as below
val image = AvatarCreator(this)
.setLetter('P')
.setLetterColor(randomColors.getLetterColor())
.setFont(this.resources.getFont(R.font.micro_extend_flf_bold))
.setBackgroundColor(randomColors.getBackgroundColor())
.build()
Jetpack compose
Image(
bitmap = AvatarCreator(this).setLetter('U')
.setLetterColor(Color.GRAY)
.setBackgroundColor(Color.BLACK)
.build()
.asImageBitmap(),
contentDescription = "",
modifier = Modifier.size(200.dp),
contentScale = ContentScale.FillWidth
)
ImageView
imageView.setImageDrawable(
AvatarCreator(this).setLetter('U')
.setLetterColor(Color.GRAY)
.setBackgroundColor(Color.BLACK)
.build()
)