Skip to content

Commit

Permalink
added anti alias for smooth edges
Browse files Browse the repository at this point in the history
  • Loading branch information
AmosKorir committed Jun 3, 2020
1 parent d07cbc8 commit 63327fa
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 75 deletions.
9 changes: 9 additions & 0 deletions .idea/codeStyles/Project.xml

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

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

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

25 changes: 25 additions & 0 deletions .idea/jarRepositories.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/misc.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -10,81 +10,94 @@ import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.BitmapDrawable
import android.text.TextPaint
import java.util.*

/**
* Created by Korir on 1/21/20.
*/
class AvatarGenerator {
companion object {
lateinit var uiContext: Context
var texSize = 0F

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): BitmapDrawable {
return avatarImageGenerate(context ,size ,shape, name,colorModel)
}

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

texSize = calTextSize(size)
val label = firstCharacter(name)
val textPaint = textPainter()
val painter = painter()
val areaRect = Rect(0, 0, width, width)

if (shape == 0) {
painter.color = RandomColors().getColor()
} else {
painter.color = Color.TRANSPARENT
}

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

//reset painter
if (shape == 0) {
painter.color = Color.TRANSPARENT
} else {
painter.color = RandomColors().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(width.toFloat() / 2, hieght.toFloat() / 2, width.toFloat() / 2, painter)
canvas.drawText(label, bounds.left, bounds.top - textPaint.ascent(), textPaint)
return BitmapDrawable(uiContext.resources, bitmap)

}

private fun firstCharacter(name: String): String {
return name.first().toString().toUpperCase()
}

private fun textPainter(): TextPaint {
val textPaint = TextPaint()
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 / 3.125).toFloat()
companion object {
private lateinit var uiContext: Context
private var texSize = 0F

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
): BitmapDrawable {
return avatarImageGenerate(context, size, shape, name, colorModel)
}

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

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

if (shape == 0) {
painter.color = RandomColors().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 {
painter.color = RandomColors().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 {
return name.first().toString().toUpperCase(Locale.ROOT)
}

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 / 3.125).toFloat()
}
}
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:4.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Jan 22 11:42:01 EAT 2020
#Wed Jun 03 22:08:05 EAT 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

0 comments on commit 63327fa

Please sign in to comment.