圆顶角落与路径

时间:2013-03-27 22:06:33

标签: java android clipping

我正试图在java中剪辑Canvas的左上角和右上角。我知道你可以在所有角落使用addRoundRect,但我不确定只对顶角做什么。

这就是我目前所拥有的:

@Override
protected void onDraw(Canvas canvas) {
    float radius = 12f;
    Path clipPath = new Path();
    RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
    //uh...
    //clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);

    canvas.clipPath(clipPath);
    super.onDraw(canvas);
}    

2 个答案:

答案 0 :(得分:4)

你可以破解它。只需将RectF设置为与圆角半径一样多的像素:

RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight() + 12.0f); // draw a larger rect

我想你必须将油漆颜色设置为完全透明(0x00fffffff)。

答案 1 :(得分:2)

您可以使用另一种重载方法addRoundRect(),如下所示:

int width  = view.getWidth();
int height = view.getHeight();      
float[] radii = {0, 0, 0, 0, 0, 0, 0, 0};
if( mRadiusTop ) {
    radii[0] = mRadius;
    radii[1] = mRadius;
    radii[2] = mRadius;
    radii[3] = mRadius; 
}
if( mRadiusBottom ) {
    radii[4] = mRadius;
    radii[5] = mRadius;
    radii[6] = mRadius;
    radii[7] = mRadius;
}

clipPath.addRoundRect(new RectF(0, 0, width, height), radii, Path.Direction.CW);
canvas.clipPath(clipPath);

我通过上面的代码解决了这个问题。