简体   繁体   English

圆角与路径

[英]Round top corners with Path

I'm trying to clip the top-left and the top-right corners of Canvas in java. 我正在尝试在Java中裁剪Canvas的左上角和右上角。 I understand you can just use addRoundRect for all the corners, but I'm unsure what to do for only the top corners. 我了解您可以仅对所有拐角使用addRoundRect ,但是我不确定仅对顶部拐角做什么。

This is what I currently have: 这是我目前拥有的:

@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);
}    

You can hack it. 您可以破解它。 Just set the RectF larger by as many pixels as the radius of the rounded corners like this: 只需将RectF设置为与圆角半径一样多的像素即可,如下所示:

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

I guess that you would have to set the paint color to full transparency (0x00fffffff). 我猜您将不得不将绘画颜色设置为完全透明(0x00fffffff)。

You can use another overloading method addRoundRect() like this : 您可以使用另一个重载方法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);

I solved the problem by above code. 我通过上面的代码解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM