android - Dragging objects using ACTION_MOVE -
i trying make drawn sprites dragable little game. should able touch anywhere , sprites should move same distance, finger moves.
with following method move on action_move event, slow, shorter distance , dont: addtox/y adds gap coordinates of sprites
@override public boolean ontouchevent(motionevent evt){ switch(evt.getaction()){ case motionevent.action_down: break; case motionevent.action_move: if(gethistorysize() > 0){ for(int = 1, n = evt.gethistorysize(); < n; i++){ int calcx = (int) gethistoricalx(i) - (int) gethistoricalx(i-1); int calcy = (int) gethistoricaly(i) - (int) gethistoricaly(i-1); for(sprite sprite : spritelist) { sprite.addtox(calcx); sprite.addtoy(calcy); } } } return true; }
any ideas on this?
assuming sprite
class (potentially-indirect) extension of android.view.view
, can use setondraglistener()
define ondrag()
override them. can use startdrag(...)
on them begin drag. typically triggered long-press gesture on view dragged, in case can trigger within ontouchevent()
in action_move
once (or action_down
). see here more details on these methods.
also, respect code posted, 1 issue explains why doesn't work using historical points (which may or may not have accumulated on particular call ontouchevent()
). whether or not gethistorysize()
greater 0, should still use evt.getx()
, evt.gety()
on each call ontouchevent()
. of course, if use drag listener approach suggested instead, won't need worry this.
update per comment
if want move of sprites @ once, can put sprites full-screen framelayout
, attach gesturedetector
uses gesturedetector.simpleongesturelistener
capture onscroll()
callbacks , calls scrollto()
on framelayout
. when parent framelayout
scrolls, of children sprites appear move together.
Comments
Post a Comment