11

Every thing is fine in first time when I move ImageView on the screen, but in second time ImageView doesn't move properly.

This is what I have done so 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;
    }
});
Renjith
  • 5,783
  • 9
  • 31
  • 42
user2380151
  • 151
  • 1
  • 1
  • 10
  • Here is an example http://thegeekyland.blogspot.com/2015/12/android-animations-explained.html – Arlind Hajredinaj Dec 10 '15 at 23:25
  • A good example to this would be this article. It also handles scale gesture and multi touch https://android-developers.googleblog.com/2010/06/making-sense-of-multitouch.html – Saran Sankaran Dec 02 '17 at 09:28

2 Answers2

43

The following works for me.

I saw you're using img.getX(), img.getY(), so I assume you're using API Level 11 or above.

And I assume your img is the instance of ImageView. ( The usage of FrameLayout.LayoutParams for ImageView is wierd though... )

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)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_MOVE :
                img.setX((int)(StartPT.x + event.getX() - DownPT.x));
                img.setY((int)(StartPT.y + event.getY() - DownPT.y));
                StartPT.set( img.getX(), img.getY() );
                break;
            case MotionEvent.ACTION_DOWN :
                DownPT.set( event.getX(), event.getY() );
                StartPT.set( img.getX(), img.getY() );
                break;
            case MotionEvent.ACTION_UP :
                // Nothing have to do
                break;
            default :
                break;
        }
        return true;
    }
});

===========================================================
====================== [2013/05/15 Added ] =======================
===========================================================

The new object presented here is PointF. Please use the following code to import PointF object :

import android.graphics.PointF;

And actually, this is just an object for recording float x and float y. If you really can not import that object, write one yourself like the following :

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

}
IEBasara
  • 501
  • 3
  • 14
  • PointF DownPT = new PointF(); // Record Mouse Position When Pressed Down PointF StartPT = new PointF(); // Record Start Position of 'img' Please provide me details of above codes. how could i declare these objects. – user2380151 May 15 '13 at 10:43
  • @user2380151 : Sorry, my bad. Please use the following code to import 'PointF' object : import android.graphics.PointF; And actually, this is just an object to record float x and float y. If you really can not import that object, write one yourself like the following : public class PointF { public float x = 0; public float y = 0; public PointF(){}; public PointF( float _x, float _y ){ x = _x; y = _y; } } – IEBasara May 15 '13 at 12:20
  • Nice solution, but how can I prevent the image to go under the virtual buttons section. it makes my image to disappear?? – BoazGarty Feb 11 '14 at 14:54
  • @IEBasara i want to rotate image view on touch without using matrix in it.... what can i do next? – Gorgeous_DroidVirus Mar 10 '14 at 10:42
  • 1
    How to make the view draggable within particular bounds? – DroidLearner Jan 07 '16 at 09:57
1

This code helps you to drag the imageview within the boundry of the screen. Hope this will help.

 @Override
public boolean onTouch(View v, MotionEvent event) {

int X = (int) event.getRawX();
        int Y = (int) event.getRawY();
        RelativeLayout.LayoutParams paramsnew = (RelativeLayout.LayoutParams) v.getLayoutParams();

        switch(event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                _xDelta = X - paramsnew.leftMargin;
                _yDelta = Y - paramsnew.topMargin;
                imgposx = img.getX();
                imgposy = img.getY();
                break;
            case MotionEvent.ACTION_UP:
                img.setX(diyoposx);
                img.setY( diyoposy );
                break;
            case MotionEvent.ACTION_MOVE:
                RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) v.getLayoutParams();
                param.leftMargin = X - _xDelta;
                param.topMargin = Y - _yDelta;
                param.rightMargin=(250*-1);
                param.bottomMargin=(250*-1);
                v.setLayoutParams(param);
                break;
        }
        mViewGroup.invalidate(); }

Here mViewGroup is the ViewGroup Instance of Relative layout that bounds the ImageView

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

Also, imgposx, imgposy are the global varaible

float imgposx, imgposy;
Tripathee Gaurav
  • 371
  • 3
  • 11