Skip to content

Commit

Permalink
implemented builder pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Amos Korir authored and Amos Korir committed Oct 25, 2021
1 parent 9d2ab78 commit dbc0c2c
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 63 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

39 changes: 18 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
# AvatarImageGenerator

[![CircleCI](https://circleci.com/gh/circleci/circleci-docs.svg?style=shield)](https://app.circleci.com/pipelines/github/AmosKorir/AvatarImageGenerator/) [ ![Download](https://api.bintray.com/packages/skyways/AvatarGenerator/AvatarGenerator/images/download.svg?version=1.4) ](https://bintray.com/skyways/AvatarGenerator/AvatarGenerator/1.4/link)
[![CircleCI](https://circleci.com/gh/circleci/circleci-docs.svg?style=shield)](https://app.circleci.com/pipelines/github/AmosKorir/AvatarImageGenerator/) [Download](https://api.bintray.com/packages/skyways/AvatarGenerator/AvatarGenerator/images/download.svg?version=1.5) ](https://bintray.com/skyways/AvatarGenerator/AvatarGenerator/1.4/link)
Generate first letter avatar Image like gmail's contact avatar. It generates an drawable that can be be set to an ImageView.
<table>
<tr>
<td>
<img width="200" height="400" src="https://github.com/skyways/AvatarImageGenerator/blob/master/art/Screen2.png"/>
<img width="200" height="400" src="Screen2.jpg"/>
</td>
<td>

<img width="200" height="400" src="https://github.com/skyways/AvatarImageGenerator/blob/master/art/screen.jpeg"/>
<img width="200" height="400" src="screen.jpeg"/>
</td>
</tr>
</table>


**Installation**

Add the Following to your gradle file.
Expand All @@ -23,6 +22,19 @@ Add the Following to your gradle file.
implementation 'com.first.avatargenerator:AvatarImageGenerator:VERSION'
```

New Builder pattern

```kotlin
AvatarGenerator.AvatarBuilder(context)
.setLabel(users[position].login)
.setAvatarSize(120)
.setTextSize(30)
.toSquare()
.toCircle()
.setBackgroundColor(Color.RED)
.build()
```

**Supports**

Using glide or Picasso you can set the drawable to an imageView as a placeholder.
Expand Down Expand Up @@ -61,28 +73,13 @@ imageView.setImageDrawable(
)
```

**Color Model**
The current version supports material colors A 400,700 and 900.

1. COLOR400
2. COLOR700
3. COLOR900

```java
Picasso.get()
.load("https://brokenfortest")
.resize(50, 50)
.placeholder(AvatarGenerator.avatarImage(this, 200, AvatarConstants.CIRCLE, "Android",AvatarConstants.COLOR900))
.into(imageView2)
```

**Shape**

1. Circle
2. Square

```java
AvatarConstants.RECTANGLE,
AvatarConstants.CIRCLE
.toSquare(),
.toCircle()

```
15 changes: 9 additions & 6 deletions app/src/main/java/com/avatarfirst/avatagen/UsersAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.avatarfirst.avatagen

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -32,12 +33,14 @@ class UsersAdapter(
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
holder.itemView.usename.text = users[position].login
holder.itemView.userAvatar.setImageDrawable(
AvatarGenerator.avatarImage(
context,
200,
AvatarConstants.CIRCLE,
users[position].login
)
AvatarGenerator.AvatarBuilder(context)
.setLabel(users[position].login)
.setAvatarSize(120)
.setTextSize(30)
.toSquare()
.toCircle()
.setBackgroundColor(Color.RED)
.build()
)
}

Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
Expand Down
14 changes: 6 additions & 8 deletions app/src/main/res/layout/user_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/item_background">
android:layout_height="wrap_content">

<ImageView
android:id="@+id/userAvatar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
Expand All @@ -21,10 +20,9 @@
android:id="@+id/usename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="67dp"
android:layout_marginLeft="67dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:text="TextView"
android:textColor="@android:color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/userAvatar"
app:layout_constraintStart_toEndOf="@+id/userAvatar"
Expand All @@ -39,7 +37,7 @@
android:layout_marginTop="8dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:background="@android:color/white"
android:background="#F1F0F0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/userAvatar"
app:layout_constraintTop_toBottomOf="@+id/userAvatar" />
Expand Down
Binary file removed art/Screen2.png
Binary file not shown.
Binary file added art/screen2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,165 @@ import android.graphics.*
import android.graphics.Bitmap.Config.ARGB_8888
import android.graphics.drawable.BitmapDrawable
import android.text.TextPaint
import android.util.Log
import java.util.*

/**
* Created by Korir on 1/21/20.
*/
class AvatarGenerator {
class AvatarGenerator(private val builder: AvatarBuilder) {

class AvatarBuilder(private val context: Context) {

private var textSize = 100
private var size = 14
private var name = " "
private var backgroundColor: Int? = null
private var shapeType = AvatarConstants.CIRCLE


fun setTextSize(textSize: Int) = apply {
this.textSize = textSize
}

fun setAvatarSize(int: Int) = apply {
this.size = int
}

fun setLabel(label: String) = apply {
this.name = label
}

fun setBackgroundColor(color: Int) = apply {
this.backgroundColor = color
}

fun toSquare() = apply {
this.shapeType = AvatarConstants.RECTANGLE
}

fun toCircle() = apply {
this.shapeType = AvatarConstants.CIRCLE
}


fun build(): BitmapDrawable {
return avatarImageGenerate(
context,
size,
shapeType,
name,
textSize,
AvatarConstants.COLOR700
)
}


private fun avatarImageGenerate(
context: Context,
size: Int,
shape: Int,
name: String,
textSize: Int,
colorModel: Int
): BitmapDrawable {
uiContext = context

texSize = calTextSize(textSize)
val label = firstCharacter(name)
val textPaint = textPainter()
val painter = painter()
painter.isAntiAlias = true
val areaRect = Rect(0, 0, size, size)

if (shape == 0) {
val firstLetter = firstCharacter(name)
val r = firstLetter[0]
painter.color = backgroundColor ?: RandomColors(colorModel).getColor()
} else {
painter.color = Color.TRANSPARENT
}

val bitmap = Bitmap.createBitmap(size, size, ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawRect(areaRect, painter)

//reset painter
if (shape == 0) {
painter.color = Color.TRANSPARENT
} else {
val firstLetter = firstCharacter(name)
val r = firstLetter[0]
painter.color = backgroundColor ?: RandomColors(colorModel).getColor()
}

val bounds = RectF(areaRect)
bounds.right = textPaint.measureText(label, 0, 1)
bounds.bottom = textPaint.descent() - textPaint.ascent()

bounds.left += (areaRect.width() - bounds.right) / 2.0f
bounds.top += (areaRect.height() - bounds.bottom) / 2.0f

canvas.drawCircle(size.toFloat() / 2, size.toFloat() / 2, size.toFloat() / 2, painter)
canvas.drawText(label, bounds.left, bounds.top - textPaint.ascent(), textPaint)
return BitmapDrawable(uiContext.resources, bitmap)

}

private fun firstCharacter(name: String): String {
if (name.isEmpty()) {
return "-"
}
return name.first().toString()
}

private fun textPainter(): TextPaint {
val textPaint = TextPaint()
textPaint.isAntiAlias = true
textPaint.textSize = texSize * uiContext.resources.displayMetrics.scaledDensity
textPaint.color = Color.WHITE
return textPaint
}

private fun painter(): Paint {
return Paint()
}

private fun calTextSize(size: Int): Float {
return (size).toFloat()
}

}


/**
* Deprecate and will be removed
*/
companion object {
private lateinit var uiContext: Context
private var texSize = 0F

@Deprecated("Switch to using builder method")
fun avatarImage(context: Context, size: Int, shape: Int, name: String): BitmapDrawable {
return avatarImageGenerate(context, size, shape, name, AvatarConstants.COLOR700)
}


fun avatarImage(
context: Context,
size: Int,
shape: Int,
name: String,
colorModel: Int
context: Context,
size: Int,
shape: Int,
name: String,
colorModel: Int
): BitmapDrawable {
return avatarImageGenerate(context, size, shape, name, colorModel)
}

private fun avatarImageGenerate(
context: Context,
size: Int,
shape: Int,
name: String,
colorModel: Int
context: Context,
size: Int,
shape: Int,
name: String,
colorModel: Int
): BitmapDrawable {
uiContext = context

Expand Down Expand Up @@ -99,7 +226,7 @@ class AvatarGenerator {
}

private fun calTextSize(size: Int): Float {
return (size / 3.125).toFloat()
return (size).toFloat()
}
}
}

0 comments on commit dbc0c2c

Please sign in to comment.