Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
package ru.skillbranch.devintensive.ui.custom
import android.content.Context
import android.content.res.Resources
import android.util.AttributeSet
import android.widget.ImageView
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import ru.skillbranch.devintensive.App
import ru.skillbranch.devintensive.R
class CircleImageView @JvmOverloads constructor (
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
): ImageView(context, attrs, defStyleAttr) {
val Int.dp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
val Int.px: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
companion object {
private const val BORDER_COLOR_DEFAULT: Int = Color.WHITE
private var borderColor = Color.WHITE
private var borderWidth = 2.px
init {
if (attrs != null) {
val attrVal = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView)
borderColor = attrVal.getColor(R.styleable.CircleImageView_cv_borderColor, BORDER_COLOR_DEFAULT)
borderWidth = attrVal.getDimensionPixelSize(R.styleable.CircleImageView_cv_borderWidth, borderWidth)
fun getBorderWidth(): Int = borderWidth.dp
fun setBorderWidth(dp: Int) {
borderWidth = dp.px
fun getBorderColor(): Int = borderColor
fun setBorderColor(hex: String) {
borderColor = Color.parseColor(hex)
fun setBorderColor(@ColorRes colorId: Int) {
borderColor = ContextCompat.getColor(App.applicationContext(), colorId)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.