1+ package com.github.abdularis.civ
2+
3+ import android.content.Context
4+ import android.graphics.*
5+ import android.util.AttributeSet
6+ import androidx.annotation.ColorInt
7+ import androidx.annotation.Dimension
8+ import androidx.annotation.IntDef
9+
10+ /* *
11+ * Created by abdularis on 22/03/18.
12+ */
13+ class AvatarImageView @JvmOverloads constructor(
14+ context : Context ,
15+ attrs : AttributeSet ? = null
16+ ) : CircleImageView(context, attrs) {
17+ @IntDef(SHOW_INITIAL , SHOW_IMAGE )
18+ annotation class State
19+
20+ private val mTextPaint: Paint
21+ private val mTextBounds: Rect
22+ private val mBackgroundPaint: Paint
23+ private val mBackgroundBounds: RectF
24+ private var initial: String
25+ private var mText: String
26+ private var mShowState: Int
27+ override fun onSizeChanged (w : Int , h : Int , oldw : Int , oldh : Int ) {
28+ super .onSizeChanged(w, h, oldw, oldh)
29+ updateCircleDrawBounds(mBackgroundBounds)
30+ }
31+
32+ override fun onDraw (canvas : Canvas ) {
33+ if (mShowState == SHOW_INITIAL ) {
34+ val textBottom = mBackgroundBounds.centerY() - mTextBounds.exactCenterY()
35+ canvas.drawOval(mBackgroundBounds, mBackgroundPaint)
36+ canvas.drawText(initial, mBackgroundBounds.centerX(), textBottom, mTextPaint)
37+ drawStroke(canvas)
38+ drawHighlight(canvas)
39+ } else {
40+ super .onDraw(canvas)
41+ }
42+ }
43+
44+ var text: String?
45+ get() = mText
46+ set(text) {
47+ mText = text ? : " "
48+ initial = extractInitial(text)
49+ updateTextBounds()
50+ invalidate()
51+ }
52+
53+ @get:State
54+ var state: Int
55+ get() = mShowState
56+ set(state) {
57+ if (state != SHOW_INITIAL && state != SHOW_IMAGE ) {
58+ val msg =
59+ " Illegal avatar state value: $state , use either SHOW_INITIAL or SHOW_IMAGE constant"
60+ throw IllegalArgumentException (msg)
61+ }
62+ mShowState = state
63+ invalidate()
64+ }
65+
66+ @get:Dimension
67+ var textSize: Float
68+ get() = mTextPaint.textSize
69+ set(size) {
70+ mTextPaint.textSize = size
71+ updateTextBounds()
72+ invalidate()
73+ }
74+
75+ @get:ColorInt
76+ var textColor: Int
77+ get() = mTextPaint.color
78+ set(color) {
79+ mTextPaint.color = color
80+ invalidate()
81+ }
82+
83+ @get:ColorInt
84+ var avatarBackgroundColor: Int
85+ get() = mBackgroundPaint.color
86+ set(color) {
87+ mBackgroundPaint.color = color
88+ invalidate()
89+ }
90+
91+ private fun extractInitial (letter : String? ): String {
92+ return if (letter == null || letter.trim { it <= ' ' }.isEmpty()) " ?" else letter[0 ].toString()
93+ }
94+
95+ private fun updateTextBounds () {
96+ mTextPaint.getTextBounds(initial, 0 , initial.length, mTextBounds)
97+ }
98+
99+ companion object {
100+ const val SHOW_INITIAL = 1
101+ const val SHOW_IMAGE = 2
102+ private const val DEF_INITIAL = " A"
103+ private const val DEF_TEXT_SIZE = 90
104+ private const val DEF_BACKGROUND_COLOR = 0xE53935
105+
106+ @State
107+ private val DEF_STATE = SHOW_INITIAL
108+ }
109+
110+ init {
111+ var text = DEF_INITIAL
112+ var textColor = Color .WHITE
113+ var textSize = DEF_TEXT_SIZE
114+ var backgroundColor = DEF_BACKGROUND_COLOR
115+ var showState = DEF_STATE
116+ attrs?.let {
117+ val a =
118+ context.obtainStyledAttributes(attrs, R .styleable.AvatarImageView , 0 , 0 )
119+ text = a.getString(R .styleable.AvatarImageView_text ) ? : " "
120+ textColor = a.getColor(R .styleable.AvatarImageView_textColor , textColor)
121+ textSize = a.getDimensionPixelSize(R .styleable.AvatarImageView_textSize , textSize)
122+ backgroundColor =
123+ a.getColor(R .styleable.AvatarImageView_avatarBackgroundColor , backgroundColor)
124+ showState = a.getInt(R .styleable.AvatarImageView_view_state , showState)
125+ a.recycle()
126+ }
127+ mShowState = showState
128+ mTextPaint = Paint (Paint .ANTI_ALIAS_FLAG )
129+ mTextPaint.textAlign = Paint .Align .CENTER
130+ mTextPaint.color = textColor
131+ mTextPaint.textSize = textSize.toFloat()
132+ mTextBounds = Rect ()
133+ mText = text
134+ initial = extractInitial(text)
135+ updateTextBounds()
136+ mBackgroundPaint = Paint (Paint .ANTI_ALIAS_FLAG )
137+ mBackgroundPaint.color = backgroundColor
138+ mBackgroundPaint.style = Paint .Style .FILL
139+ mBackgroundBounds = RectF ()
140+ }
141+ }
0 commit comments