android - Auto-moving image on drag listener -
i set drag listener imageview
in app, when click it, don't want center image based on pressed. this:
https://gfycat.com/constantdisguisedkudu
basically, if press on bottom right
of image, takes press central point
, moves image's center point
on exact location. don't want that. if press on bottom right
, shouldn't auto move , can drag image point. don't think code necessary in case:
@override public boolean ondrag(view v, dragevent event) { switch (event.getaction()) { // signal start of drag , drop operation case dragevent.action_drag_started: { // nothing break; } // drag point has entered bounding box of view case dragevent.action_drag_entered: { // nothing break; } // user has moved drag shadow outside bounding box of view case dragevent.action_drag_exited: { // nothing break; } // drag shadow has been released, drag point within bounding box of view case dragevent.action_drop: { // image , position imageview view = (imageview) event.getlocalstate(); int position = (int) view.gettag(r.id.piece_position); /** * if dropped on left pane, remove parent , * remove bitmap @ position , notify adapter. * add left pane , set position. */ if (v == puzzlepane) { viewgroup viewgroup = (viewgroup) view.getparent(); viewgroup.removeview(view); if (position != -1) { pieces.remove(position); madapter.notifydatasetchanged(); } framelayout containview = (framelayout) v; containview.addview(view); view.setvisibility(view.visible); view.settag(r.id.piece_state, "left"); view.settag(r.id.piece_position, -1); view.setonlongclicklistener(null); view.setontouchlistener(madapter); } else { view.setvisibility(view.visible); view.settag(r.id.piece_state, "right"); view.setontouchlistener(null); view.setonlongclicklistener(madapter); } log.d(mydraglistener.class.getsimplename(), view.gettag(r.id.piece_state) + ""); view.setx(event.getx() - (view.getwidth() / 2)); view.sety(event.gety() - (view.getheight() / 2)); break; } // drag , drop operation has concluded case dragevent.action_drag_ended: { // nothing break; } } return true; }
you need extend view.dragshadowbuilder , , send location of finger relative continer. that:
//where trigger dragging view yourimage; // yourimage.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { final clipdata dragdata = new clipdata("label", new string[]{clipdescription.mimetype_text_plain}, null); startdrag(dragdata, // data dragged new mydragshadowbuilder(v, event.getx(), event.gety()), // drag shadow builder null, 0 // flags (not used, set 0) ); return true; } });
and class:
private static class mydragshadowbuilder extends view.dragshadowbuilder { int width, height, relativex, relativey; public mydragshadowbuilder(view v, float relativex,float relativey) { super(v); this.relativex = (int) relativex; this.relativey = (int) relativey; width = (v.getwidth()); height = (v.getheight()); } @override public void onprovideshadowmetrics (@nonnull point size, @nonnull point touch){ size.set(width, height); touch.set(relativex, relativey); } }
Comments
Post a Comment