android - Drawing transparent area not updating with drag listener -
i've seen many questions transparent
background on child views
custom viewgroup
on so, no 1 seems have problem.
background:
created custom framelayout
; container has views added dynamically. children should have transparent background, other surface of container must have background color. children views can drag'n'dropped
anywhere container.
what do:
override dispatchdraw()
, create bitmap
, new canvas
, fill new canvas white background.
make loop on children views, create new paint
, rect
child's dimensions , use porterduff.mode.dst_out
clear child's area. each child, add paint , rect new canvas.
finally, use drawbitmap
on main canvas given dispatchdraw()
, passing created bitmap.
problem:
works well: children have transparent background , rest filled white background. however, when add draglistener
children, "cutting" area not updated (while dispatchdraw
correctly recalled): in other words, when drag child view, it's dropped transparent area still @ same place.
code:
custom framelayout
:
@override public void dispatchdraw(canvas canvas) { super.dispatchdraw(canvas); drawcanvas(canvas); } private void drawcanvas(canvas canvas) { // create off-screen bitmap , canvas bitmap bitmap = bitmap.createbitmap(getwidth(), getheight(), bitmap.config.argb_8888); canvas auxcanvas = new canvas(bitmap); // fill canvas desired outside color auxcanvas.drawcolor(color.white); // create paint each child parent (int = 0; < getchildcount(); ++i) { // create transparent area rect child view child = this.getchildat(i); paint childpaint = new paint(); childpaint.setxfermode(new porterduffxfermode(porterduff.mode.dst_out)); rect childrect = new rect(child.getleft(), child.gettop(), child.getright(), child.getbottom()); auxcanvas.drawrect(childrect, childpaint); } // draw bitmap original canvas canvas.drawbitmap(bitmap, 0, 0, null); }
the draglistener
action_drop
's event:
case dragevent.action_drop: x = event.getx(); y = event.gety(); framelayout frame = (framelayout) v; view view = (view) event.getlocalstate(); view.setx(x - (view.getwidth()/2)); view.sety(y - (view.getheight()/2)); frame.invalidate(); break;
screenshots:
i tried many things regarding q&a found, nothing seems work.
appreciate.
finally, found clue: transparent paint
isn't getting right values x , y axis after update.
i guess getleft()
, gettop()
, getright()
, getbottom()
don't change when drop occurs. weirdly, in logs, these values seem updated. instead, tried updated value in dragevent.action_drop
using getx()
, gety()
, , changed correctly coordinates of transparent area.
the solution children in loop:
paint childpaint = new paint(); childpaint.setxfermode(new porterduffxfermode(porterduff.mode.dst_out)); // use x , y axis (plus width , height) rect childrect = new rect( (int) child.getx(), (int) child.gety(), (int) child.getx() + child.getwidth(), (int) child.gety() + child.getheight() ); auxcanvas.drawrect(childrect, childpaint);
Comments
Post a Comment