Skip to content

Instantly share code, notes, and snippets.

@robsenshuu
Last active January 21, 2020 18:00
Show Gist options
  • Save robsenshuu/8751db8eafafc593eb08544a837c3705 to your computer and use it in GitHub Desktop.
Save robsenshuu/8751db8eafafc593eb08544a837c3705 to your computer and use it in GitHub Desktop.
import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.widget.ImageView
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
class CircularImageView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : ImageView(context, attrs, defStyle)
override fun setImageDrawable(drawable: Drawable?) {
val radius = 100f
val bitmap = (drawable as BitmapDrawable).bitmap
val rid = RoundedBitmapDrawableFactory.create(resources, bitmap)
rid.cornerRadius = bitmap.width * radius
super.setImageDrawable(rid)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment