1

I wanna set rounded corner to view with outline, like this

fun View.setRoundedCorners(radiusTopLeft: Float,
                           radiusTopRight: Float,
                           radiusBottomRight: Float,
                           radiusBottomLeft: Float) {
    outlineProvider = object : ViewOutlineProvider() {
        private val path = Path()
        private val rect = RectF()
        private val radii = floatArrayOf(
                radiusTopLeft,
                radiusTopLeft,
                radiusTopRight,
                radiusTopRight,
                radiusBottomRight,
                radiusBottomRight,
                radiusBottomLeft,
                radiusBottomLeft)

        override fun getOutline(view: View, outline: Outline) {
            rect.set(0F, 0F, view.width.toFloat(), view.height.toFloat())
            path.addRoundRect(rect, radii, Path.Direction.CW)
            outline.setConvexPath(path)
        }
    }
    clipToOutline = true
}

But this code has no effect. I have not idea how fix it.
outline.setRoundRect() - working as expected, but I need set radius to each corner independently

andreich
  • 1,120
  • 1
  • 11
  • 28

1 Answers1

2

because

/**
 * Returns whether the outline can be used to clip a View.
 * <p>
 * Currently, only Outlines that can be represented as a rectangle, circle,
 * or round rect support clipping.
 *
 * @see android.view.View#setClipToOutline(boolean)
 */
public boolean canClip() {
    return mMode != MODE_CONVEX_PATH;
}
andreich
  • 1,120
  • 1
  • 11
  • 28