ontouchlistener - Slide Up an image using EventMotion in Android -
i trying slide layout in android using touch listener. expecting motion_up should intercepted when user touches screen , drags finger screen. , motion_down other way. when debug, both , down called.
i trying slide layout when drag using animation. don't understand doing wrong. did had situation before? hints helpful, thank you.
this code:
@targetapi(build.version_codes.honeycomb) public class mainactivity extends activity implements animationlistener, ondraglistener { // animation private animation animslideup; private imageview img; private rect rect; @targetapi(build.version_codes.honeycomb) @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); img = (imageview) findviewbyid(r.id.interstitialimg); img.setondraglistener(this); img.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if (event.getactionmasked() == motionevent.action_up) { system.out.println("up"); rect = new rect(v.getleft(), v.gettop(), v.getright(), v .getbottom()); v.gethitrect(rect); if (rect.contains(math.round(v.getx() + event.getx()), math.round(v.gety() + event.gety()))) { system.out.println("inside"); // animation slide_up = animationutils.loadanimation( // getapplicationcontext(), r.anim.slide_up); // img.startanimation(slide_up); } else { // outside system.out.println("outside"); } } if (event.getaction() == motionevent.action_down) { system.out.println("down"); // construct rect of view's bounds rect = new rect(v.getleft(), v.gettop(), v.getright(), v .getbottom()); return true; } if (event.getaction() == motionevent.action_move) { // if (!rect.contains(v.getleft() + (int) event.getx(), // v.gettop() + (int) event.gety())) { // // user moved outside bounds // system.out.println("moved outside bounds " + rect); // } } return true; } }); } @override public void onanimationend(animation animation) { // check zoom in animation if (animation == animslideup) { system.out.println("animation: " + animation); overridependingtransition(r.anim.slide_up, r.anim.slide_up); finish(); } } @override public void onanimationrepeat(animation animation) { // todo auto-generated method stub } @override public void onanimationstart(animation animation) { // todo auto-generated method stub } @suppresslint("newapi") @override public boolean ondrag(view v, dragevent e) { if (e.getaction() == dragevent.action_drop) { system.out.println("action drop"); view view = (view) e.getlocalstate(); viewgroup = (viewgroup) view.getparent(); from.removeview(view); relativelayout = (relativelayout) v; to.addview(view); view.setvisibility(view.visible); } return true; } @override public void onbackpressed() { super.onbackpressed(); overridependingtransition(r.anim.slide_up, r.anim.slide_up); } }
i expecting motion_up should intercepted when user touches screen , drags finger screen.
uh, no.
docs rescue:
public static final int action_down pressed gesture has started, motion contains initial starting location.
think of action_down start of user interaction. down
not indicate direction. in crude sense, tells user has put his/her finger(s) down on screen.
similarly:
public static final int action_up pressed gesture has finished, motion contains final release location intermediate points since last down or move event.
again, up
not you're assuming be. means user has lifted his/her finger(s) off screen.
let's @ code now:
rect = new rect(v.getleft(), v.gettop(), v.getright(), v .getbottom()); v.gethitrect(rect); if (rect.contains(math.round(v.getx() + event.getx()), math.round(v.gety() + event.gety()))) { system.out.println("inside"); // animation slide_up = animationutils.loadanimation( // getapplicationcontext(), r.anim.slide_up); // img.startanimation(slide_up); } else { // outside system.out.println("outside"); }
first, gethitrect(rect)
fills rect
supply argument. so, initializing v.getleft(), v.gettop(), v.getright(), v.getbottom()
not required.
second, since setting ontouchlistener
on img
, why need check if event.getx()
, event.gety()
inside view's bounds? in fact, don't. because, ontouch(...)
will not triggered if user touches outside.
third, may not need implement ontouchlistener
want.. or @ least, not in way are. example, if looking if user has flinged
view or down, can rely on simpleongesturelistener
. accepted answer on question show how use it: android: how handle right left swipe gestures.
so these action_down , action_up events used for? well, work them when need finer control: reacting user input immediately. example, cannot afford basic functionality of moving view around screen using simpleongesturelistener
.
Comments
Post a Comment