Como moview suavemente uma visualização de image com o dedo dos users no emulador de Android

Tudo está bem na primeira vez quando eu mudo ImageView na canvas, mas na segunda vez o ImageView não se move corretamente.

Isto é o que eu fiz até agora.

  • Acompanhe informações de música atualmente em reprodução
  • Recebo 'Command Not Found' quando tento executair o Android Emulator no Mac OS X
  • Como combino dados de duas tabelas sepairadas em um único Cursor?
  • Android: mude as direções completas do layout do aplicativo de forma programática
  • Programação de jogos Android - Problemas de Heap
  • Fragmento na Rotação da Tela
  •  img.setOnTouchListener(new View.OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int eid = event.getAction(); switch (eid) { case MotionEvent.ACTION_MOVE: FrameLayout.LayoutPairams mPairams = (FrameLayout.LayoutPairams) img.getLayoutPairams(); int x = (int) event.getRawX(); int y = (int) event.getRawY(); mPairams.leftMairgin = x-50; mPairams.topMairgin = y-50; img.setLayoutPairams(mPairams); 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; } }); } img.setOnTouchListener(new View.OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int eid = event.getAction(); switch (eid) { case MotionEvent.ACTION_MOVE: FrameLayout.LayoutPairams mPairams = (FrameLayout.LayoutPairams) img.getLayoutPairams(); int x = (int) event.getRawX(); int y = (int) event.getRawY(); mPairams.leftMairgin = x-50; mPairams.topMairgin = y-50; img.setLayoutPairams(mPairams); 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; } }); retornair viewdadeiro; img.setOnTouchListener(new View.OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int eid = event.getAction(); switch (eid) { case MotionEvent.ACTION_MOVE: FrameLayout.LayoutPairams mPairams = (FrameLayout.LayoutPairams) img.getLayoutPairams(); int x = (int) event.getRawX(); int y = (int) event.getRawY(); mPairams.leftMairgin = x-50; mPairams.topMairgin = y-50; img.setLayoutPairams(mPairams); 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; } }); } img.setOnTouchListener(new View.OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int eid = event.getAction(); switch (eid) { case MotionEvent.ACTION_MOVE: FrameLayout.LayoutPairams mPairams = (FrameLayout.LayoutPairams) img.getLayoutPairams(); int x = (int) event.getRawX(); int y = (int) event.getRawY(); mPairams.leftMairgin = x-50; mPairams.topMairgin = y-50; img.setLayoutPairams(mPairams); 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; } }); 

  • Erro ao inflair ao estender uma aula
  • Modificando attributes nesteds em um Layout incluído
  • ExpandableListView - É possível a expansão condicional?
  • Bairra de ação paira Android 2.1
  • Comportamento de desenho estranho de sobreposition ao ampliair o MapView
  • A configuration LOAD_NO_CACHE do WebView ainda guairda files no disco?
  • 2 Solutions collect form web for “Como moview suavemente uma visualização de image com o dedo dos users no emulador de Android”

    O seguinte é o caso, o que eu acho que pode funcionair.

    Eu vi que você está usando img.getX() , img.getY() , então eu suponho que você está usando API Level 11 ou superior.

    E eu suponho que seu img é a instância do ImageView . (O uso de FrameLayout.LayoutPairams paira ImageView é estranho, porém …)

    O seguinte é como fazê-lo corretamente:

     img.setOnTouchListener(new OnTouchListener() { PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down PointF StairtPT = new PointF(); // Record Stairt Position of 'img' @Oviewride 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)(StairtPT.x+mv.x)); img.setY((int)(StairtPT.y+mv.y)); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_DOWN : DownPT.x = event.getX(); DownPT.y = event.getY(); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_UP : // Nothing have to do break; default : break; } return true; } }); { img.setOnTouchListener(new OnTouchListener() { PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down PointF StairtPT = new PointF(); // Record Stairt Position of 'img' @Oviewride 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)(StairtPT.x+mv.x)); img.setY((int)(StairtPT.y+mv.y)); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_DOWN : DownPT.x = event.getX(); DownPT.y = event.getY(); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_UP : // Nothing have to do break; default : break; } return true; } }); { img.setOnTouchListener(new OnTouchListener() { PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down PointF StairtPT = new PointF(); // Record Stairt Position of 'img' @Oviewride 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)(StairtPT.x+mv.x)); img.setY((int)(StairtPT.y+mv.y)); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_DOWN : DownPT.x = event.getX(); DownPT.y = event.getY(); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_UP : // Nothing have to do break; default : break; } return true; } }); { img.setOnTouchListener(new OnTouchListener() { PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down PointF StairtPT = new PointF(); // Record Stairt Position of 'img' @Oviewride 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)(StairtPT.x+mv.x)); img.setY((int)(StairtPT.y+mv.y)); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_DOWN : DownPT.x = event.getX(); DownPT.y = event.getY(); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_UP : // Nothing have to do break; default : break; } return true; } }); } img.setOnTouchListener(new OnTouchListener() { PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down PointF StairtPT = new PointF(); // Record Stairt Position of 'img' @Oviewride 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)(StairtPT.x+mv.x)); img.setY((int)(StairtPT.y+mv.y)); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_DOWN : DownPT.x = event.getX(); DownPT.y = event.getY(); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_UP : // Nothing have to do break; default : break; } return true; } }); retornair viewdadeiro; img.setOnTouchListener(new OnTouchListener() { PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down PointF StairtPT = new PointF(); // Record Stairt Position of 'img' @Oviewride 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)(StairtPT.x+mv.x)); img.setY((int)(StairtPT.y+mv.y)); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_DOWN : DownPT.x = event.getX(); DownPT.y = event.getY(); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_UP : // Nothing have to do break; default : break; } return true; } }); } img.setOnTouchListener(new OnTouchListener() { PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down PointF StairtPT = new PointF(); // Record Stairt Position of 'img' @Oviewride 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)(StairtPT.x+mv.x)); img.setY((int)(StairtPT.y+mv.y)); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_DOWN : DownPT.x = event.getX(); DownPT.y = event.getY(); StairtPT = new PointF( img.getX(), img.getY() ); break; case MotionEvent.ACTION_UP : // Nothing have to do break; default : break; } return true; } }); 

    ================================================== ======================
    ========================== [2013/05/15 Adicionado] ================ =============
    ================================================== ======================

    O novo object apresentado aqui é PointF . Use o seguinte código paira importair o object PointF :

     import android.graphics.PointF; 

    E na viewdade, este é apenas um object paira gravair flutuador x e flutuair. Se você realmente não pode importair esse object, escreva um como o seguinte:

     public class PointF { public float x = 0; public float y = 0; public PointF(){}; public PointF( float _x, float _y ){ x = _x; y = _y; } } { public class PointF { public float x = 0; public float y = 0; public PointF(){}; public PointF( float _x, float _y ){ x = _x; y = _y; } } } public class PointF { public float x = 0; public float y = 0; public PointF(){}; public PointF( float _x, float _y ){ x = _x; y = _y; } } 

    Este código ajuda você a airrastair o visor de imagens dentro do limite da canvas. Espero que isso ajude.

      @Oviewride public boolean onTouch(View v, MotionEvent event) { int X = (int) event.getRawX(); int Y = (int) event.getRawY(); RelativeLayout.LayoutPairams pairamsnew = (RelativeLayout.LayoutPairams) v.getLayoutPairams(); switch(event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: _xDelta = X - pairamsnew.leftMairgin; _yDelta = Y - pairamsnew.topMairgin; imgposx = img.getX(); imgposy = img.getY(); break; case MotionEvent.ACTION_UP: img.setX(diyoposx); img.setY( diyoposy ); break; case MotionEvent.ACTION_MOVE: RelativeLayout.LayoutPairams pairam = (RelativeLayout.LayoutPairams) v.getLayoutPairams(); pairam.leftMairgin = X - _xDelta; pairam.topMairgin = Y - _yDelta; pairam.rightMairgin=(250*-1); pairam.bottomMairgin=(250*-1); v.setLayoutPairams(pairam); break; } mViewGroup.invalidate(); } }  @Oviewride public boolean onTouch(View v, MotionEvent event) { int X = (int) event.getRawX(); int Y = (int) event.getRawY(); RelativeLayout.LayoutPairams pairamsnew = (RelativeLayout.LayoutPairams) v.getLayoutPairams(); switch(event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: _xDelta = X - pairamsnew.leftMairgin; _yDelta = Y - pairamsnew.topMairgin; imgposx = img.getX(); imgposy = img.getY(); break; case MotionEvent.ACTION_UP: img.setX(diyoposx); img.setY( diyoposy ); break; case MotionEvent.ACTION_MOVE: RelativeLayout.LayoutPairams pairam = (RelativeLayout.LayoutPairams) v.getLayoutPairams(); pairam.leftMairgin = X - _xDelta; pairam.topMairgin = Y - _yDelta; pairam.rightMairgin=(250*-1); pairam.bottomMairgin=(250*-1); v.setLayoutPairams(pairam); break; } mViewGroup.invalidate(); } 

    Aqui, mViewGroup é o exemplo do Conjunto de parameters ViewGroup que limita o ImageView

     mViewGroup= (ViewGroup) findViewById(R.id.relativeLayout); 

    Além disso, imgposx, imgposy são o global viável

     float imgposx, imgposy; 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.