2017-04-19 110 views
2

我希望为自己的框架布局设置自定义形状(矩形每个角的不同半径),以便框架布局中的视图将剪裁到形状的边界。创建自定义凸形路径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()的路径不是凸的? 如何创建自定义凸形路径?谢谢。

+0

面对同样的问题。现在,我知道当我只使用'lineTo'方法时,'isConvex'可以返回'true'。使用'quadTo'或'arcTo'会导致'isConvex'返回'false'。也许这可能是一些小费... – pxsx

+0

@pxsxYeah,'arcTo'是我认为会导致问题。但是我必须使用它,因为我打算为视图组创建自定义形状。无论如何,我用clipPath解决了自定义形状问题。 –

回答

1

我自己想通了。路径不是凸的,因为我没有正确绘制路径。这实现了多个圆角半径的影响,我想正确的路径应该是:

// 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); 

更新:虽然道路现在是正确的,但还不是凸的路径,似乎是自定义的路径将不会被视为凸路径。

0

不错,你得到它的工作,但有内置的工具。
最简单的就是以可抽拉的xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <corners 
     android:bottomLeftRadius="8dp" 
     android:topLeftRadius="8dp" 
     android:topRightRadius="8dp" 
     android:bottomRightRadius="0dp" /> 

    <solid android:color="#fff" /> 

</shape> 

通过编程可以使用RoundRectShape类。这也是从上面的XML充气。
在操纵杆中,您可以传递外角半径,线宽和内角半径。您还可以查看源代码以了解它们如何创建路径(提示:path.addRoundRect())。

+1

谢谢你让我知道'addRoundRect(RectF rect,float [] radii,Direction dir)'。但根据'Outline'的源代码:'public boolean canClip(){ return mMode!= MODE_CONVEX_PATH; }'大纲不支持剪裁自定义形状 –