Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ft border #22

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ Generate first letter avatar Image like gmail's contact avatar. It generates an
<table>
<tr>
<td>
<img width="200" height="400" src="Screen2.jpg"/>
<img width="200" height="400" src="art/screen2.jpg"/>
</td>
<td>

<img width="200" height="400" src="screen.jpeg"/>
<img width="200" height="400" src="art/screen.jpeg"/>
</td>
</tr>
</table>
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/com/avatarfirst/avatagen/UsersAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ class UsersAdapter(
AvatarGenerator.AvatarBuilder(context)
.setLabel(users[position].login)
.setAvatarSize(120)
.setTextSize(30)
.toSquare()
.setTextSize(20)
.toCircle()
.setBackgroundColor(Color.RED)
.toLowerCase()
.build()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ class AvatarConstants {
val COLOR900=900
val COLOR400=400
val COLOR700=700

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ class AvatarGenerator(private val builder: AvatarBuilder) {

private var textSize = 100
private var size = 14
private var borderWidth = 10
private var name = " "
private var backgroundColor: Int? = null
private var borderColor: Int? = null
private var isWithBorder = false
private var shapeType = AvatarConstants.CIRCLE
private var isUpperCase = false


fun setTextSize(textSize: Int) = apply {
Expand All @@ -37,6 +41,26 @@ class AvatarGenerator(private val builder: AvatarBuilder) {
this.backgroundColor = color
}

fun setBorderColor(color: Int) = apply {
this.borderColor = color
}

fun setBorderWidth(width: Int) = apply {
this.borderWidth= width
}

fun toUpperCase() =apply{
isUpperCase = true
}

fun toLowerCase()=apply {
isUpperCase = false
}

fun setWithBorder(borderEnabled: Boolean) = apply {
isWithBorder = borderEnabled
}

fun toSquare() = apply {
this.shapeType = AvatarConstants.RECTANGLE
}
Expand All @@ -53,6 +77,7 @@ class AvatarGenerator(private val builder: AvatarBuilder) {
shapeType,
name,
textSize,
borderWidth,
AvatarConstants.COLOR700
)
}
Expand All @@ -64,20 +89,21 @@ class AvatarGenerator(private val builder: AvatarBuilder) {
shape: Int,
name: String,
textSize: Int,
borderWidth: 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]
if (shape == AvatarConstants.RECTANGLE) {
painter.color = backgroundColor ?: RandomColors(colorModel).getColor()
} else {
painter.color = Color.TRANSPARENT
Expand All @@ -87,12 +113,11 @@ class AvatarGenerator(private val builder: AvatarBuilder) {
val canvas = Canvas(bitmap)
canvas.drawRect(areaRect, painter)


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

Expand All @@ -104,16 +129,92 @@ class AvatarGenerator(private val builder: AvatarBuilder) {
bounds.top += (areaRect.height() - bounds.bottom) / 2.0f

canvas.drawCircle(size.toFloat() / 2, size.toFloat() / 2, size.toFloat() / 2, painter)

if (isWithBorder) {
when (shape) {
AvatarConstants.RECTANGLE -> {
drawBorder(canvas, areaRect, painter)

}
AvatarConstants.CIRCLE -> {
drawCircleBorder(canvas, size, borderWidth, painter)
}
}
}



canvas.drawText(label, bounds.left, bounds.top - textPaint.ascent(), textPaint)

return BitmapDrawable(uiContext.resources, bitmap)

}

private fun drawCircleBorder(
canvas: Canvas,
originalSize: Int,
borderWidth: Int,
painter: Paint
) {
val size = originalSize - borderWidth
painter.color = borderColor ?: Color.BLACK
painter.style = Paint.Style.STROKE
painter.strokeWidth = borderWidth.toFloat()
canvas.drawCircle(
originalSize.toFloat() / 2,
originalSize.toFloat() / 2,
size.toFloat() / 2,
painter
)
}

private fun drawBorder(canvas: Canvas, shape: Rect, painter: Paint) {
painter.color = borderColor ?: Color.BLACK
painter.style = Paint.Style.STROKE
painter.strokeWidth = borderWidth.toFloat()
canvas.drawRect(shape, painter)
}

private fun drawBorder(
canvas: Canvas,
shape: Int,
originalSize: Int,
painter: Paint,
borderColor: Int?
) {
val size = borderWidth + originalSize
painter.style = Paint.Style.STROKE
painter.strokeWidth = borderWidth.toFloat()
painter.color = borderColor ?: RandomColors(700).getColor()


painter.color = borderColor ?: Color.BLACK
painter.style = Paint.Style.STROKE
painter.strokeWidth = borderWidth.toFloat()
canvas.drawRect(Rect(0, 0, size, size), painter)
if (shape == AvatarConstants.RECTANGLE) {

// canvas.drawRect(Rect(0, 0, size, size), painter)

} else {
canvas.drawCircle(
size.toFloat() / 2,
size.toFloat() / 2,
size.toFloat() / 2,
painter
)
}
}

private fun firstCharacter(name: String): String {
if (name.isEmpty()) {
return "-"
}
return name.first().toString()
return if (isUpperCase) {
name.first().toString().toUpperCase(Locale.ROOT)
} else {
name.first().toString().toLowerCase(Locale.ROOT)
}
}

private fun textPainter(): TextPaint {
Expand Down Expand Up @@ -175,8 +276,6 @@ class AvatarGenerator(private val builder: AvatarBuilder) {
val areaRect = Rect(0, 0, size, size)

if (shape == 0) {
val firstLetter = firstCharacter(name)
val r = firstLetter[0]
painter.color = RandomColors(colorModel).getColor()
} else {
painter.color = Color.TRANSPARENT
Expand All @@ -190,8 +289,6 @@ class AvatarGenerator(private val builder: AvatarBuilder) {
if (shape == 0) {
painter.color = Color.TRANSPARENT
} else {
val firstLetter = firstCharacter(name)
val r = firstLetter[0]
painter.color = RandomColors(colorModel).getColor()
}

Expand Down