创建自定义凸路径Android

创建自定义凸路径Android,第1张

概述我希望为我的框架布局设置一个自定义形状(矩形的每个角的半径不同),以便框架布局中的视图将剪切到形状的边界. ViewOutlineProvider provider = new ViewOutlineProvider() { @Override public void getOutline(View view, Outline outline) { 我希望为我的框架布局设置一个自定义形状(矩形的每个角的半径不同),以便框架布局中的视图将剪切到形状的边界.

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,wIDth,height,Path.Direction.ccw);        return;    }    // top left circle    oval.set(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,2 * topRighTradius);    borderPath.arcTo(oval,90,-90);    borderPath.rlineto(0,height - topRighTradius - bottomrighTradius);    // Bottom right circle    oval.set(wIDth - 2 * bottomrighTradius,height - 2 * bottomrighTradius,height);    borderPath.arcTo(oval,-90);    borderPath.rlineto(-wIDth + bottomrighTradius + bottomLefTradius,0);    // Bottom left circle    oval.set(0,height - 2 * bottomLefTradius,2 * bottomLefTradius,-90,-height + bottomLefTradius + topLefTradius);}

当我运行它时,我得到java.lang.IllegalArgumentException:path必须是凸的,我无法进入native_isConvex()并看看它是如何决定路径是否凸出.

那么什么是凸路?为什么congfigurePath()中的路径不是凸的?
 如何创建自定义凸路径?谢谢.

解决方法 我自己想通了.路径不凸,因为我没有正确绘制路径.实现我想要的多角半径效果的正确路径应该是:

// top left circle    oval.set(0,-180,90);    borderPath.rlineto(wIDth - topLefTradius - topRighTradius,90);    borderPath.rlineto(0,90);    borderPath.rlineto(-wIDth + bottomrighTradius + bottomLefTradius,-height + bottomLefTradius + topLefTradius);

更新:虽然现在路径正确,但它仍然不是凸路径,似乎自定义路径不会被视为凸路径.

总结

以上是内存溢出为你收集整理的创建自定义凸路径Android全部内容,希望文章能够帮你解决创建自定义凸路径Android所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1126028.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存