how to smoothly move a image- view with users finger on android emulator -
every thing fine in first time when move imageview
on screen, in second time imageview
doesn't move properly.
this have done far.
img.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub int eid = event.getaction(); switch (eid) { case motionevent.action_move: framelayout.layoutparams mparams = (framelayout.layoutparams) img.getlayoutparams(); int x = (int) event.getrawx(); int y = (int) event.getrawy(); mparams.leftmargin = x-50; mparams.topmargin = y-50; img.setlayoutparams(mparams); break; case motionevent.action_down: x1=img.getx(); y1=img.gety(); break; case motionevent.action_up: img.setx(x1); img.sety(y1); break; default: break; } return true; } });
the following case think might work.
i saw you're using img.getx()
, img.gety()
, assume you're using api level 11 or above.
and assume img
instance of imageview
. ( usage of framelayout.layoutparams
imageview wierd though... )
the following how make properly:
img.setontouchlistener(new ontouchlistener() { pointf downpt = new pointf(); // record mouse position when pressed down pointf startpt = new pointf(); // record start position of 'img' @override public boolean ontouch(view v, motionevent event) { int eid = event.getaction(); switch (eid) { case motionevent.action_move : pointf mv = new pointf( event.getx() - downpt.x, event.gety() - downpt.y); img.setx((int)(startpt.x+mv.x)); img.sety((int)(startpt.y+mv.y)); startpt = new pointf( img.getx(), img.gety() ); break; case motionevent.action_down : downpt.x = event.getx(); downpt.y = event.gety(); startpt = new pointf( img.getx(), img.gety() ); break; case motionevent.action_up : // nothing have break; default : break; } return true; } });
========================================================================
========================== [2013/05/15 added ] =============================
========================================================================
the new object presented here pointf
. please use following code import pointf
object :
import android.graphics.pointf;
and actually, object recording float x , float y. if can not import object, write 1 following :
public class pointf { public float x = 0; public float y = 0; public pointf(){}; public pointf( float _x, float _y ){ x = _x; y = _y; } }
Comments
Post a Comment