Android View outline - using a custom Path
I know it is not helping in clipping but still, we can take the advantage of outline.setConvexPath(path);
in elevation shadow since it's working fine as an outline for the elevation shadow around the path.
I attached an image to make it even more clear in which:
First image: ImageView with [Image with shadow]
android:outlineProvider="bounds"
android:elevation="4dp"
Second image: CustomImageView with canvas.clipPath(getShapePath())
in onDraw()
method [we will get the clipped image but shadow still show as a rectangle bound]
android:outlineProvider="bounds"
android:elevation="4dp"
Third image: CustomImageView with canvas.clipPath(getShapePath())
in onDraw()
method and outline?.setConvexPath(getShapePath())
in ViewOutlineProvider
. [we will get the clipped image and shadow will be aligned with the clipped image]
android:outlineProvider="bounds" // this will have no effect since it will be override by setConvexPath
android:elevation="4dp"
Download Source Code
It seems that a View
can only be clipped to a rectangle, a rounded rectangle, or an oval Outline
. Rather, a View
can only be clipped to an Outline
that is defined specifically with a setRect()
, setRoundRect()
, or setOval()
method. An Outline
set with a Path
will not work, even though the Path
itself may be one of the aforementioned shapes.
This is documented in several places, though not particularly well, in my opinion.
View#setClipToOutline()
:
Note that this flag will only be respected if the View's Outline returns true from
canClip()
.
Outline#canClip()
:
Currently, only Outlines that can be represented as a rectangle, circle, or round rect support clipping.
Defining Shadows and Clipping Views - Clip Views:
Only rectangle, circle, and round rectangle outlines support clipping, as determined by the
Outline.canClip()
method.
At first look, this didn't seem consistent with Outline
's own inner workings, as a convex Path
is, in fact, used internally for non-circular ovals. However, it does make sense to simply disallow all externally defined Path
s, rather than expend the resources necessary to determine if an arbitrary Path
is valid.