-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleProfilePicture.kt
More file actions
47 lines (41 loc) · 1.04 KB
/
GoogleProfilePicture.kt
File metadata and controls
47 lines (41 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class CustomImageView(context: Context, attrs: AttributeSet) : AppCompatImageView(context, attrs) {
private var rnd = Random()
private val shape = GradientDrawable()
private var letter = "A"
var noPic = false
companion object {
private val paint = Paint()
init {
paint.textAlign = Paint.Align.CENTER
paint.color = Color.WHITE
paint.textSize = 66F
paint.isAntiAlias = true
paint.isDither = true
}
}
init {
letter = DatabaseHelper.firstName?.first().toString()
shape.shape = GradientDrawable.OVAL
shape.setColor(Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)))
}
override fun onDraw(canvas: Canvas?) {
if (!noPic) {
super.onDraw(canvas)
return
}
background = shape
val x = (canvas!!.width / 2)
val y = ((canvas!!.height / 2) - ((paint.descent() + paint.ascent()) / 2))
canvas!!.drawText(letter, x.toFloat(), y, paint)
}
fun clear() {
paint.color = Color.TRANSPARENT
shape.setColor(Color.TRANSPARENT)
invalidate()
}
fun updateText(firstLetter: String) {
letter = firstLetter
noPic = true
invalidate()
}
}