Android - How to use DragShadowBuilder? -
i using dragshadowbuilder.java
, class sample.
i don't know how can use in activity
, parameters should send constructor
of drawabledragshadowbuilder
:
public class drawabledragshadowbuilder extends dragshadowbuilder { private drawable mdrawable; public drawabledragshadowbuilder(view view, drawable drawable) { super(view); // set drawable , apply green filter mdrawable = drawable; mdrawable.setcolorfilter(new porterduffcolorfilter(color.green, porterduff.mode.multiply)); } @override public void onprovideshadowmetrics(point shadowsize, point touchpoint) { // fill in size shadowsize.x = mdrawable.getintrinsicwidth(); shadowsize.y = mdrawable.getintrinsicheight(); // fill in location of shadow relative touch. // here center shadow under finger. touchpoint.x = mdrawable.getintrinsicwidth() / 2; touchpoint.y = mdrawable.getintrinsicheight() / 2; mdrawable.setbounds(new rect(0, 0, shadowsize.x, shadowsize.y)); } @override public void ondrawshadow(canvas canvas) { //draw shadow view onto provided canvas mdrawable.draw(canvas); } }
thank's lot.
you have pass view
want drag, in constructor of dragshadowbuilder
dragshadowbuilder shadowbuilder = new view.dragshadowbuilder(view);
and start drag using:
view.startdrag(data, shadowbuilder, view, 0);
here complete example following excellent tutorial:
import android.app.activity; import android.content.clipdata; import android.graphics.drawable.drawable; import android.os.bundle; import android.view.dragevent; import android.view.motionevent; import android.view.view; import android.view.view.dragshadowbuilder; import android.view.view.ondraglistener; import android.view.view.ontouchlistener; import android.view.viewgroup; import android.widget.linearlayout; public class myactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); findviewbyid(r.id.myimage1).setontouchlistener(new mytouchlistener()); findviewbyid(r.id.myimage2).setontouchlistener(new mytouchlistener()); findviewbyid(r.id.myimage3).setontouchlistener(new mytouchlistener()); findviewbyid(r.id.myimage4).setontouchlistener(new mytouchlistener()); findviewbyid(r.id.topleft).setondraglistener(new mydraglistener()); findviewbyid(r.id.topright).setondraglistener(new mydraglistener()); findviewbyid(r.id.bottomleft).setondraglistener(new mydraglistener()); findviewbyid(r.id.bottomright).setondraglistener(new mydraglistener()); } private final class mytouchlistener implements ontouchlistener { public boolean ontouch(view view, motionevent motionevent) { if (motionevent.getaction() == motionevent.action_down) { clipdata data = clipdata.newplaintext("", ""); dragshadowbuilder shadowbuilder = new view.dragshadowbuilder(view); view.startdrag(data, shadowbuilder, view, 0); view.setvisibility(view.invisible); return true; } else { return false; } } } class mydraglistener implements ondraglistener { drawable entershape = getresources().getdrawable(r.drawable.shape_droptarget); drawable normalshape = getresources().getdrawable(r.drawable.shape); @override public boolean ondrag(view v, dragevent event) { int action = event.getaction(); switch (event.getaction()) { case dragevent.action_drag_started: // nothing break; case dragevent.action_drag_entered: v.setbackgrounddrawable(entershape); break; case dragevent.action_drag_exited: v.setbackgrounddrawable(normalshape); break; case dragevent.action_drop: // dropped, reassign view viewgroup view view = (view) event.getlocalstate(); viewgroup owner = (viewgroup) view.getparent(); owner.removeview(view); linearlayout container = (linearlayout) v; container.addview(view); view.setvisibility(view.visible); break; case dragevent.action_drag_ended: v.setbackgrounddrawable(normalshape); default: break; } return true; } } }
Comments
Post a Comment