Skip to content

Instantly share code, notes, and snippets.

@DipaliShah
Created August 19, 2019 05:08
Show Gist options
  • Save DipaliShah/3e8058dc24656fad61d2d712b3f9256f to your computer and use it in GitHub Desktop.
Save DipaliShah/3e8058dc24656fad61d2d712b3f9256f to your computer and use it in GitHub Desktop.
Rounded Corner ImageView - using Android Clipping, written in kotlin
class RoundedImageView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatImageView(
context, attrs, defStyleAttr
) {
private var rectF: RectF? = null
private val path = Path()
private val cornerRadius = 20f
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
rectF = RectF(0f, 0f, w.toFloat(), h.toFloat())
resetPath()
}
override fun draw(canvas: Canvas) {
val save = canvas.save()
canvas.clipPath(path)
super.draw(canvas)
canvas.restoreToCount(save)
}
override fun dispatchDraw(canvas: Canvas) {
val save = canvas.save()
canvas.clipPath(path)
super.dispatchDraw(canvas)
canvas.restoreToCount(save)
}
private fun resetPath() {
path.reset()
path.addRoundRect(rectF, cornerRadius, cornerRadius, Path.Direction.CW)
path.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment