ali bagheri
ali bagheri

Reputation: 149

How set clip path for children only?

I have a custom view that extends RelativeLayout. this has many children. I want clip children only, but my code clipping my custom view too.

public class LoadingBox extends RelativeLayout
{

    @Override
    protected void dispatchDraw(Canvas canvas)
    {
        save = canvas.save();
          canvas.getClipBounds(clipRect);
          clipRectF.set(clipRect.left, clipRect.top, clipRect.right, 
          clipRect.bottom);
          clipPath.addRoundRect(clipRectF, 40f, 40f, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.dispatchDraw(canvas);
        canvas.restoreToCount(save);
    }

} 

I tested

@Override
protected void onDraw(Canvas canvas){...} 

too ,but i take this result again.

How i clip child only not self view?

Upvotes: 0

Views: 516

Answers (1)

ali bagheri
ali bagheri

Reputation: 149

I found my answer. i used below code

 LayoutInflater layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 ViewGroup layout = (ViewGroup) layoutInflater.inflate(R.layout.loading_box, this);

this is mistake, must create children view dynamic and override drawChild

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime)
{
    boolean result;
    canvas.save();
    clipPath.reset();
    canvas.getClipBounds(clipRect);
    clipRectF.set(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
    clipPath.addRoundRect(clipRectF, radius-2, radius-2, Path.Direction.CW);
    canvas.clipPath(clipPath);
    result = super.drawChild(canvas, child, drawingTime);
    canvas.restore();
    return result;
}

Upvotes: 2

Related Questions