我希望为自己的框架布局设置自定义形状(矩形每个角的不同半径),以便框架布局中的视图将剪裁到形状的边界。创建自定义凸形路径Android
ViewOutlineProvider provider = new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
configurePath(getWidth(), getHeight());
outline.setConvexPath(borderPath);
}
}
};
setOutlineProvider(provider);
setClipToOutline(true);
而且configurePath()
看起来是这样的:
private void configurePath (int width, int height) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return;
}
borderPath.rewind();
float minSize = Math.min(width, height);
float maxRadiusWidth = 2 * Math.max(Math.max(topLeftRadius, topRightRadius),
Math.max(bottomLeftRadius, bottomRightRadius));
if (minSize < maxRadiusWidth) {
borderPath.addRect(0, 0, width, height, Path.Direction.CCW);
return;
}
// Top left circle
oval.set(0, 0, 2 * topLeftRadius, 2 * topLeftRadius);
borderPath.moveTo(0, topLeftRadius);
borderPath.arcTo(oval, 180, -90);
borderPath.rLineTo(width - topLeftRadius - topRightRadius, 0);
// Top right circle
oval.set(width - 2 * topRightRadius, 0, width, 2 * topRightRadius);
borderPath.arcTo(oval, 90, -90);
borderPath.rLineTo(0, height - topRightRadius - bottomRightRadius);
// Bottom right circle
oval.set(width - 2 * bottomRightRadius, height - 2 * bottomRightRadius, width, height);
borderPath.arcTo(oval, 0, -90);
borderPath.rLineTo(-width + bottomRightRadius + bottomLeftRadius, 0);
// Bottom left circle
oval.set(0, height - 2 * bottomLeftRadius, 2 * bottomLeftRadius, height);
borderPath.arcTo(oval, -90, -90);
borderPath.rLineTo(0, -height + bottomLeftRadius + topLeftRadius);
}
当我运行它,我得到了java.lang.IllegalArgumentException: path must be convex
,我不能进入native_isConvex()
,看看它是如何决定如果路径是凸的。
那么什么是凸路径?为什么congfigurePath()
的路径不是凸的? 如何创建自定义凸形路径?谢谢。
面对同样的问题。现在,我知道当我只使用'lineTo'方法时,'isConvex'可以返回'true'。使用'quadTo'或'arcTo'会导致'isConvex'返回'false'。也许这可能是一些小费... – pxsx
@pxsxYeah,'arcTo'是我认为会导致问题。但是我必须使用它,因为我打算为视图组创建自定义形状。无论如何,我用clipPath解决了自定义形状问题。 –