Created
May 28, 2020 10:44
-
-
Save rovkinmax/ca0eac3b2619ea3373c9ee9cfecdfc3b to your computer and use it in GitHub Desktop.
example how round corners for any view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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