Skip to content

An android library that helps in creating a letter avatar as bitmap to use as a placeholder for profile. Includes customisation to set the colours , change the font style and font size etc.,

License

Pranathi-pellakuru/LetterAvatarGenerator

Repository files navigation

Letter Avatar Generator

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

Installation

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'

How to Use

Avatar creator class

val image = AvatarCreator(this).setLetter('U')
    .setTextSize(25)
    .setAvatarSize(180)
    .setLetterColor(Color.GRAY)
    .setBackgroundColor(Color.BLACK)
    .build()

To customize colors

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()

To customize font

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()

Displaying the generated bitmap

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()
        )

Reference

https://github.com/AmosKorir/AvatarImageGenerator

About

An android library that helps in creating a letter avatar as bitmap to use as a placeholder for profile. Includes customisation to set the colours , change the font style and font size etc.,

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages