Skip to content

Commit

Permalink
Merge pull request #5 from abdularis/kotlin
Browse files Browse the repository at this point in the history
Convert to kotlin
  • Loading branch information
abdularis authored Aug 21, 2020
2 parents c9a15eb + b447892 commit a0ec3c3
Show file tree
Hide file tree
Showing 15 changed files with 470 additions and 542 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Circular%20Image%20View-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/6870)

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.
> this project was inspired by [hdodenhof CircleImageView](https://github.com/hdodenhof/CircleImageView)

Read this article:
* [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)
Expand Down
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

ext.kotlin_version = "1.3.72"

repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'

classpath "com.android.tools.build:gradle:4.0.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
13 changes: 8 additions & 5 deletions circularimageview/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 27
compileSdkVersion 29
buildToolsVersion "30.0.0"

defaultConfig {
minSdkVersion 14
targetSdkVersion 27
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}

buildTypes {
Expand All @@ -23,6 +26,6 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:support-annotations:27.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.annotation:annotation:1.1.0'
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.github.abdularis.civ

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import androidx.annotation.ColorInt
import androidx.annotation.Dimension
import androidx.annotation.IntDef

/**
* Created by abdularis on 22/03/18.
*/
class AvatarImageView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : CircleImageView(context, attrs) {
@IntDef(SHOW_INITIAL, SHOW_IMAGE)
annotation class State

private val mTextPaint: Paint
private val mTextBounds: Rect
private val mBackgroundPaint: Paint
private val mBackgroundBounds: RectF
private var initial: String
private var mText: String
private var mShowState: Int
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
updateCircleDrawBounds(mBackgroundBounds)
}

override fun onDraw(canvas: Canvas) {
if (mShowState == SHOW_INITIAL) {
val textBottom = mBackgroundBounds.centerY() - mTextBounds.exactCenterY()
canvas.drawOval(mBackgroundBounds, mBackgroundPaint)
canvas.drawText(initial, mBackgroundBounds.centerX(), textBottom, mTextPaint)
drawStroke(canvas)
drawHighlight(canvas)
} else {
super.onDraw(canvas)
}
}

var text: String?
get() = mText
set(text) {
mText = text ?: ""
initial = extractInitial(text)
updateTextBounds()
invalidate()
}

@get:State
var state: Int
get() = mShowState
set(state) {
if (state != SHOW_INITIAL && state != SHOW_IMAGE) {
val msg =
"Illegal avatar state value: $state, use either SHOW_INITIAL or SHOW_IMAGE constant"
throw IllegalArgumentException(msg)
}
mShowState = state
invalidate()
}

@get:Dimension
var textSize: Float
get() = mTextPaint.textSize
set(size) {
mTextPaint.textSize = size
updateTextBounds()
invalidate()
}

@get:ColorInt
var textColor: Int
get() = mTextPaint.color
set(color) {
mTextPaint.color = color
invalidate()
}

@get:ColorInt
var avatarBackgroundColor: Int
get() = mBackgroundPaint.color
set(color) {
mBackgroundPaint.color = color
invalidate()
}

private fun extractInitial(letter: String?): String {
return if (letter == null || letter.trim { it <= ' ' }.isEmpty()) "?" else letter[0].toString()
}

private fun updateTextBounds() {
mTextPaint.getTextBounds(initial, 0, initial.length, mTextBounds)
}

companion object {
const val SHOW_INITIAL = 1
const val SHOW_IMAGE = 2
private const val DEF_INITIAL = "A"
private const val DEF_TEXT_SIZE = 90
private const val DEF_BACKGROUND_COLOR = 0xE53935

@State
private val DEF_STATE = SHOW_INITIAL
}

init {
var text = DEF_INITIAL
var textColor = Color.WHITE
var textSize = DEF_TEXT_SIZE
var backgroundColor = DEF_BACKGROUND_COLOR
var showState = DEF_STATE
attrs?.let {
val a =
context.obtainStyledAttributes(attrs, R.styleable.AvatarImageView, 0, 0)
text = a.getString(R.styleable.AvatarImageView_text) ?: ""
textColor = a.getColor(R.styleable.AvatarImageView_textColor, textColor)
textSize = a.getDimensionPixelSize(R.styleable.AvatarImageView_textSize, textSize)
backgroundColor =
a.getColor(R.styleable.AvatarImageView_avatarBackgroundColor, backgroundColor)
showState = a.getInt(R.styleable.AvatarImageView_view_state, showState)
a.recycle()
}
mShowState = showState
mTextPaint = Paint(Paint.ANTI_ALIAS_FLAG)
mTextPaint.textAlign = Paint.Align.CENTER
mTextPaint.color = textColor
mTextPaint.textSize = textSize.toFloat()
mTextBounds = Rect()
mText = text
initial = extractInitial(text)
updateTextBounds()
mBackgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG)
mBackgroundPaint.color = backgroundColor
mBackgroundPaint.style = Paint.Style.FILL
mBackgroundBounds = RectF()
}
}
Loading

0 comments on commit a0ec3c3

Please sign in to comment.