Question

I'm trying to clip the top-left and the top-right corners of Canvas in java. I understand you can just use addRoundRect for all the corners, but I'm unsure what to do for only the top corners.

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);
}    
Was it helpful?

Solution 2

You can hack it. Just set the RectF larger by as many pixels as the radius of the rounded corners like this:

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).

OTHER TIPS

You can use another overloading method addRoundRect() like this :

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top