Skip to content

Instantly share code, notes, and snippets.

@rovkinmax
Created May 28, 2020 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rovkinmax/ca0eac3b2619ea3373c9ee9cfecdfc3b to your computer and use it in GitHub Desktop.
Save rovkinmax/ca0eac3b2619ea3373c9ee9cfecdfc3b to your computer and use it in GitHub Desktop.
example how round corners for any view
fun View.setRoundedCornersRes(@DimenRes resId: Int) {
setRoundedCorners(resources.getDimension(resId))
}
fun View.setRoundedCorners(@Px radius: Int) {
setRoundedCorners(radius.toFloat())
}
fun View.setRoundedCorners(@Px radius: Float) {
clipToOutline = true
outlineProvider = object : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline) {
val rect = Rect(0, 0, view.width, view.height)
outline.setRoundRect(rect, radius)
}
}
}
fun View.setRoundedCornersTopRes(@DimenRes resId: Int) {
setRoundedCornersTop(resources.getDimension(resId))
}
fun View.setRoundedCornersTop(@Px radius: Float) {
clipToOutline = true
outlineProvider = object : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline) {
val rect = Rect(0, 0, view.width, (view.height + radius).toInt())
outline.setRoundRect(rect, radius)
}
}
}
fun View.setClipCircle() {
clipToOutline = true
outlineProvider = object : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline) {
val rect = Rect(0, 0, view.width, view.height)
outline.setRoundRect(rect, view.width.toFloat() / 2)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment