AlphabetBitmapGenerator

Additional

Language
Kotlin
Version
v1.0.0 (Jul 30, 2022)
Created
Jul 20, 2022
Updated
Dec 14, 2022
Owner
IO DevBlue (IODevBlue)
Contributor
IO DevBlue (IODevBlue)
1
Activity
N/A
Badge
Generate
Download
Source code

Alphabet Bitmap Generator

Alphabet Bitmap Generator is a simple library that generates a rectangular or circular bitmap image containing the first alphabet from a provided String name.

Dependencies

This library uses DesignColors to provide default colors. This is already bundled in the library.

Uses

  • You can use Alphabet Bitmap Generator when you need single alphabet circular bitmap images for applications that display contacts.
  • It can also be used in custom Material Chip implementations.

Usage

Initialize an Alphabet Image Generator instance using a Context:

private var alphabetBitmapGenerator = AlphabetBitmapGenerator(context)

Apply your configurations to the Alphabet Image Generator Instance:

alphabetBitmapGenerator.apply {
  setDimension(100)
  alphabetTextColor = Color.WHITE
}

NOTE:

  • Apply the configurations before generating a bitmap.
  • The Dimension should be the same as that of the ImageView for conformity. It is best to set the ImageView's height and width to wrap_content.

Generate a rectangular bitmap by passing in a name string:

  • The first character of the provided name string must be a valid alphabet character for a bitmap to be generated. The default image would be used if the first character is not a valid alphabet character. (See sample images and project for details).
  • The second parameter takes in an optional background color integer. If null is passed, a random Material Blue is chosen from the DesignColors library.
val bitmap = alphabetBitmapGenerator.generateAlphabetBitmap(name, null)

Or generate a circular bitmap also passing in a name string:

  • This method takes in an optional float radius parameter. If 0F is passed, the radius would be calculated using the width or height parameter set by either setDimension(), setSpecificDimension() or the default width and height.
val bitmap = alphabetBitmapGenerator.generateCircularAlphabetBitmap(name, 0F, null)

Set the bitmap as the ImageView's bitmap:

imageView.setImageBitmap(bitmap)

Java Interoperability

This library works perfectly with Java projects.

Initialize an Alphabet Image Generator instance using a Context:

AlphabetBitmapGenerator alphabetBitmapGenerator = new AlphabetBitmapGenerator(context);

Apply your configurations to the Alphabet Image Generator Instance:

alphabetBitmapGenerator.setDimension(100);
alphabetBitmapGenerator.setAlphabetTextColor(Color.WHITE);

NOTE:

  • Apply the configurations before generating a bitmap.
  • The Dimension should be the same as that of the ImageView for conformity. It is best to set the ImageView's height and width to wrap_content.

Generate a rectangular bitmap by passing in a name string:

  • The first character of the provided name string must be a valid alphabet character for a bitmap to be generated. The default image would be used if the first character is not a valid alphabet character. (See sample images and project for details).
  • The second parameter takes in an optional background color integer. If null is passed, a random Material Blue is chosen from the DesignColors library.
Bitmap bitmap = alphabetBitmapGenerator.generateAlphabetBitmap(name, null);

Or generate a circular bitmap also passing in a name string:

  • This method takes in an optional float radius parameter. If 0F is passed, the radius would be calculated using the width or height parameter set by either setDimension(), setSpecificDimension() or the default width and height.
Bitmap bitmap = alphabetBitmapGenerator.generateCircularAlphabetBitmap(name, 0F, null);

Set the bitmap as the ImageView's bitmap:

imageView.setImageBitmap(bitmap);

See sample project for more details.

Configurations:

Variable Default Use
alphabetTextColor Black The text color of the alphabet.
alphabetTypeface Gotham Medium The font of the alphabet.
fontSize 17F The font size of alphabet (Float).
defaultDrawable The default image to use if the name provided is not a String.
Method Returns Use
setDimension(dimension: Int) Unit Sets an equal dimension for the generated Alphabet Image.
setSpecificDimension(width: Int, height: Int) Unit Sets a specific dimension for the generated Alphabet Image.
generateAlphabetBitmap(displayName: String, @ColorInt backgroundColor: Int?) A bitmap containing either an alphabet or the default bitmap. Generates a rectangular alphabet bitmap using the first character from the displayName String parameter.
generateCircularAlphabetBitmap(displayName: String, radius: Float, @ColorInt backgroundColor: Int?) A circular bitmap containing either an alphabet or the default bitmap. Generates a circular alphabet bitmap using the first character from the displayName String parameter.

Changelog

  • 1.0.0
    • Initial release