2

我正在尝试在 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);
}    
4

2 回答 2

8

您可以像这样使用另一个重载方法 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);

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

于 2015-11-12T07:49:27.167 回答
5

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

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

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

于 2013-03-27T22:43:22.940 回答