android - Image does not maintain x,y coordinates during drag and drop -
i trying set drag , drop functionality imageview onlongclick. issue noticing dragevent.action_drag_exited
, received x , y cords event change last dragevent.action_drag_location
coordinates. causes draggable imageview move (or jump) after have dropped it. here implementation.
first setup onlongclicklistener on object (imageview) want drag around:
ivdrag.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { clipdata.item item = new clipdata.item((charsequence)v.gettag()); string[] mimetypes = {clipdescription.mimetype_text_plain}; clipdata dragdata = new clipdata(v.gettag().tostring(), mimetypes, item); dragshadowbuilder mshadow = new dragshadowbuilder(v); v.startdrag(dragdata, mshadow, v, 0); dragview.setondraglistener(draglistener); return true; } });
i set ondraglistener on object (imageview) want drag around:
ivdrag.setondraglistener(new ondraglistener() { @override public boolean ondrag(view v, dragevent event) { // todo auto-generated method stub switch (event.getaction()) { case dragevent.action_drag_started: layoutparams = (relativelayout.layoutparams) v.getlayoutparams(); logger.i(tag, "action dragevent.action_drag_started"); ivdrag.setvisibility(view.invisible); break; case dragevent.action_drag_entered: containdragable = true; logger.i(tag, "action dragevent.action_drag_entered"); x_cord = (int) event.getx(); y_cord = (int) event.gety(); logger.i(tag, "x_cord: " + x_cord + " //y_cord: " + y_cord); break; case dragevent.action_drag_exited: containdragable = false; logger.i(tag, "action dragevent.action_drag_exited"); x_cord = (int) event.getx(); // removing line allows correct cords y_cord = (int) event.gety(); // removing line allows correct cords logger.d(tag, "x_cord: " + x_cord + " //y_cord: " + y_cord); // below re-positions drag object layoutparams.leftmargin = x_cord; layoutparams.topmargin = y_cord; v.setlayoutparams(layoutparams); break; case dragevent.action_drag_location: logger.i(tag, "action dragevent.action_drag_location"); x_cord = (int) event.getx(); y_cord = (int) event.gety(); logger.i(tag, "x_cord: " + x_cord + " //y_cord: " + y_cord); break; case dragevent.action_drag_ended : logger.i(tag, "action dragevent.action_drag_ended"); // below makes sure image not disappear ivdrag.post(new runnable() { @override public void run() { // todo auto-generated method stub logger.e(tag, "inside runnable"); if (ivdrag.getvisibility() != view.visible) { ivdrag.setvisibility(view.visible); } } }); break; case dragevent.action_drop: logger.i(tag, "action_drop event"); // nothing break; default: break; } return true; } }); }
note: commented above if remove update x,y cords dragevent.action_drag_exited
, logs indicate correct coordinates. however, unable re-drag object after that.
lastly, create listener when have dragged object container.
private view.ondraglistener draglistener = new view.ondraglistener() { @override public boolean ondrag(view v, dragevent event) { switch (event.getaction()) { case dragevent.action_drag_started: logger.v(tag, "drag started (ondrag)"); break; case dragevent.action_drag_entered: logger.v(tag, "drag entered (ondrag)"); break; case dragevent.action_drag_exited: logger.v(tag, "drag exited (ondrag)"); break; case dragevent.action_drop: logger.e(tag, "drop (ondrag)"); // handle drop break; case dragevent.action_drag_ended: logger.v(tag, "drag ended (ondrag)"); break; default: return false; } return true; } };
what missing make work expected? in advance!
Comments
Post a Comment