Luís Filipe Longo Luís Filipe Longo - 11 months ago 68
Android Question

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

and do something like

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.

protected void dispatchDraw(Canvas canvas) {
//do stuff here

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

Answer Source

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)

protected void dispatchDraw(Canvas canvas) {
    Paint p = new Paint();
    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawCircle((float) center.x, (float) center.y, headerSize / 2, p);;
    canvas.translate(padding, padding);