touch - Android: How identify an ImageView by Tag or ID -
i have on activity several imageview's user can move around screen. how can identify imageview selected user? in xcode can assign unique tag each imageview , ontouch tag number. there similar way on android?
this xcode:
uitouch *touch = [[event alltouches] anyobject]; if ([touch view].tag == 901) { itouchobject = 901; [self.view bringsubviewtofront:imgrojo1]; }
in android activity have far done: in oncreate can assign id each imageview
imageview selimage = (imageview)findviewbyid(r.id.i904); selimage.setid(904);
and in ontouch.moved can use imageview directly assigned id. in case image moves smooth below finger on screen.
iid = 904; imageview selimage = (imageview)findviewbyid(iid);
but, how can id imageview user has touched?
i have tried use setontouchlistener:
imageview selimage = (imageview)findviewbyid(r.id.i904); selimage.setid(904); selimage.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { iid = v.getid(); imageview selimage = (imageview)findviewbyid(iid); int eventaction = event.getaction(); int x = (int)event.getx(); int y = (int)event.gety(); switch (eventaction ) { case motionevent.action_down: // touch down check if finger on ball break; case motionevent.action_move: // touch drag ball // move balls same finger selimage.setx(x-25); selimage.sety(y-25); break; case motionevent.action_up: // touch drop - things here after dropping break; } // redraw canvas return true; }});
by can select each imageview , moves, but, doesn't move smooth , imageview duplicated on screen.
is possible in first case in ontouch.moved view , use id with: iid = v.getid();
you can check id current view has , compare actual resource id.
for example:
switch (v.getid()) { case r.id.image1: // break; case r.id.image2: // .. break; case r.id.image3: // .. break; }
you may check out this page read bit more drag-and-drop choicedraglistener
instead.
Comments
Post a Comment