Skip to content
Snippets Groups Projects
Commit 39b4518e authored by Adrien Béraud's avatar Adrien Béraud Committed by Adrien Béraud
Browse files

AvatarDrawable: mutate placeholder icon

Prevents occasional artifacts

Change-Id: I139e3bb9e0f8034bf11723e6dc37d2caab527671
parent ce24a668
Branches
Tags
No related merge requests found
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
*/ */
package cx.ring.views package cx.ring.views
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.graphics.* import android.graphics.*
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
...@@ -35,6 +36,7 @@ import net.jami.utils.HashUtils ...@@ -35,6 +36,7 @@ import net.jami.utils.HashUtils
import net.jami.utils.toHex import net.jami.utils.toHex
import java.util.* import java.util.*
import kotlin.math.min import kotlin.math.min
import androidx.core.graphics.createBitmap
class AvatarDrawable : Drawable { class AvatarDrawable : Drawable {
private class PresenceIndicatorInfo { private class PresenceIndicatorInfo {
...@@ -149,9 +151,9 @@ class AvatarDrawable : Drawable { ...@@ -149,9 +151,9 @@ class AvatarDrawable : Drawable {
val h: Int val h: Int
if (outfit == a < b) { if (outfit == a < b) {
w = iw w = iw
h = iw * bh / bw h = b / bw
} else { } else {
w = ih * bw / bh w = a / bh
h = ih h = ih
} }
val x = (iw - w) / 2 val x = (iw - w) / 2
...@@ -331,6 +333,7 @@ class AvatarDrawable : Drawable { ...@@ -331,6 +333,7 @@ class AvatarDrawable : Drawable {
/** Should only be used in tests */ /** Should only be used in tests */
fun getBitmap(): MutableList<Bitmap>? = bitmaps fun getBitmap(): MutableList<Bitmap>? = bitmaps
@SuppressLint("UseCompatLoadingForDrawables")
private constructor( private constructor(
context: Context, context: Context,
photos: MutableList<Bitmap>?, photos: MutableList<Bitmap>?,
...@@ -347,7 +350,7 @@ class AvatarDrawable : Drawable { ...@@ -347,7 +350,7 @@ class AvatarDrawable : Drawable {
/*if (cropCircle) { /*if (cropCircle) {
inSize = minSize inSize = minSize
}*/ }*/
if (photos != null && photos.size > 0) { if (photos != null && photos.isNotEmpty()) {
avatarText = null avatarText = null
bitmaps = photos bitmaps = photos
if (photos.size == 1) { if (photos.size == 1) {
...@@ -375,8 +378,10 @@ class AvatarDrawable : Drawable { ...@@ -375,8 +378,10 @@ class AvatarDrawable : Drawable {
color = ContextCompat.getColor(context, getAvatarColor(id)) color = ContextCompat.getColor(context, getAvatarColor(id))
clipPaint = if (cropCircle) arrayOf(Paint()) else null clipPaint = if (cropCircle) arrayOf(Paint()) else null
if (avatarText == null) { if (avatarText == null) {
placeholder = placeholder = context.getDrawable(
context.getDrawable(if (isGroup) R.drawable.baseline_group_24 else R.drawable.baseline_account_crop_24) as VectorDrawable? if (isGroup) R.drawable.baseline_group_24
else R.drawable.baseline_account_crop_24
)?.mutate() as VectorDrawable?
} else { } else {
textPaint.color = Color.WHITE textPaint.color = Color.WHITE
textPaint.typeface = Typeface.SANS_SERIF textPaint.typeface = Typeface.SANS_SERIF
...@@ -518,9 +523,11 @@ class AvatarDrawable : Drawable { ...@@ -518,9 +523,11 @@ class AvatarDrawable : Drawable {
canvas.drawColor(color) canvas.drawColor(color)
if (avatarText != null) { if (avatarText != null) {
canvas.drawText(avatarText!!, textStartXPoint, textStartYPoint, textPaint) canvas.drawText(avatarText!!, textStartXPoint, textStartYPoint, textPaint)
} else if (placeholder != null) { } else {
placeholder!!.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN) placeholder?.let {
placeholder!!.draw(canvas) it.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN)
it.draw(canvas)
}
} }
} }
} }
...@@ -591,13 +598,12 @@ class AvatarDrawable : Drawable { ...@@ -591,13 +598,12 @@ class AvatarDrawable : Drawable {
} }
if (cropCircle) { if (cropCircle) {
for (i in workspace.indices) { for (i in workspace.indices) {
val workspacei = Bitmap.createBitmap(iw, ih, Bitmap.Config.ARGB_8888) val workspacei = createBitmap(iw, ih)
workspace[i] = workspacei workspace[i] = workspacei
clipPaint!![i].shader = BitmapShader(workspacei, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) clipPaint!![i].shader = BitmapShader(workspacei, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
} }
} else { } else {
workspace[0] = workspace[0] = createBitmap(bounds.width(), bounds.height())
Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888)
} }
if (bitmaps != null) { if (bitmaps != null) {
if (bitmaps.size == 1 || (cropCircle && groupCircle)) { if (bitmaps.size == 1 || (cropCircle && groupCircle)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment