Skip to content

Commit a0ec3c3

Browse files
authored
Merge pull request #5 from abdularis/kotlin
Convert to kotlin
2 parents c9a15eb + b447892 commit a0ec3c3

File tree

15 files changed

+470
-542
lines changed

15 files changed

+470
-542
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Circular%20Image%20View-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/6870)
44

55
This library provides you circle and avatar imageview for android. it automatically scale and center a bitmap based on the size of the view but does not copy the bitmap itself.
6-
> this project was inspired by [hdodenhof CircleImageView](https://github.com/hdodenhof/CircleImageView)
76

87
Read this article:
98
* [https://medium.com/@abdularis/android-custom-view-tutorial-create-circle-image-view-cacdd3e986cb](https://medium.com/@abdularis/android-custom-view-tutorial-create-circle-image-view-cacdd3e986cb)

build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
4+
ext.kotlin_version = "1.3.72"
5+
56
repositories {
67
google()
78
jcenter()
89
}
910
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.0.1'
11-
11+
classpath "com.android.tools.build:gradle:4.0.1"
12+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1213

1314
// NOTE: Do not place your application dependencies here; they belong
1415
// in the individual module build.gradle files

circularimageview/build.gradle

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
24

35
android {
4-
compileSdkVersion 27
6+
compileSdkVersion 29
7+
buildToolsVersion "30.0.0"
58

69
defaultConfig {
710
minSdkVersion 14
8-
targetSdkVersion 27
11+
targetSdkVersion 29
912
versionCode 1
1013
versionName "1.0"
1114

12-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15+
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1316
}
1417

1518
buildTypes {
@@ -23,6 +26,6 @@ android {
2326

2427
dependencies {
2528
implementation fileTree(dir: 'libs', include: ['*.jar'])
26-
27-
implementation 'com.android.support:support-annotations:27.1.0'
29+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
30+
implementation 'androidx.annotation:annotation:1.1.0'
2831
}

circularimageview/src/main/java/com/github/abdularis/civ/AvatarImageView.java

Lines changed: 0 additions & 186 deletions
This file was deleted.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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

Comments
 (0)