Search code examples
androidlayoutdrawandroid-custom-viewviewgroup

Draw in front of certain child views


I have a ViewGroup that consists on a header and a circle of menu items. Basically I have a closing/opening animation where my item views go behind the header view. Since all views have transparencies, when the item views go behind the header view, they are still visible and end up appearing behind the header view through the transparencies.

What I wanted to do is to intersect the item views with the hweader view, erasing the intersection. What I came up with was to override dispatchDraw and do something like PorterDuff.Mode.CLEAR

But I can only do this to all views at once, in the sense. Using the code below, it'll erase everything that's been drawn in the view in that specific area, thus the header as well.

@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    //do stuff here
}

Is there any way i can draw the view again, or even select which views I want to erase?


Solution

  • Just for future reference, this is what I did. Override dispatch draw, erase the given area and draw the child again with child.draw(canvas)

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        Paint p = new Paint();
        p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawCircle((float) center.x, (float) center.y, headerSize / 2, p);
        canvas.save();
        canvas.translate(padding, padding);
        getChildAt(0).draw(canvas);
        canvas.restore();
    }