Arunkumar
Arunkumar

Reputation: 3841

Set background with custom shape and a png image

I have a view to which I want to set a drawable image which is very easy. the images is set properly but the corners are square. If I try to make the corners rounded by using shape attribute in an xml drawable then the corners are round but I cannot find a way to set the image background as the background attribute of the view is used to set the xml drawable for corner.

Is there a way to have the background as an image as well as rounded corners. I am aware of solutions like using an image with rounded corners but I don't want to bother the graphics team for such smaller issues and I want to have a generic solution.

Thanks a lot for your help.

Upvotes: 2

Views: 873

Answers (1)

asenovm
asenovm

Reputation: 6517

Override the dispatchDraw method of your view(or onDraw in case it's not a view hierarchy)

private final Path mClipPath;
.......
mClipPath = new Path();
mClipPath.addRoundRect(new RectF(x,y,z,t),radius,radius,Direction.CW);
........
@Override
protected void dispatchDraw(final Canvas canvas){
    canvas.clipPath(mClipPath); // clip the canvas so that we can draw only in the boundaries specified by mClipPath
    super.dispatchDraw(canvas); //now draw the view on the clipped canvas
    .....  //do other drawing
}

Upvotes: 1

Related Questions