Android View Diagram - Using Custom Path
When I set up the view schema like this, it works just fine:
view.setClipToOutline(true);
view.setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 16);
}
});
However, use is Path
not affected. Replace setRoundRect
with:
Path path = new Path();
path.addRoundRect(
0, 0, view.getWidth(), view.getHeight(), 16, 16, Path.Direction.CW);
outline.setConvexPath(path);
How do I make a view clip available to a path?
It seems like View
can only be cropped by rectangle, rounded rectangle or oval Outline
. Rather, View
you can only bind to Outline
that is specifically defined with a method setRect()
, setRoundRect()
or setOval()
. Set Outline
c Path
will not work even if it Path
may itself be one of the aforementioned shapes.
This is documented in several places, although not very well in my opinion.
View#setClipToOutline()
:
Note that this flag will only be honored if the view structure returns true from
canClip()
.
Outline#canClip()
:
Currently, only contours that can be represented as rectangular, circular or circular rectangular support.
Defining Shadows and Cropping Views - Viewing Clips :
Only the outlines of the rectangle, circle, and round rectangle support clipping as determined by the method
Outline.canClip()
.
At first, it didn't seem to coincide with Outline
its own internal work, as the convex is Path
actually used internally for non-circular ovals. However, it makes sense to simply disallow all outer Path
s, rather than waste the resources required to determine whether an arbitrary is allowed Path
.