Skip to content Skip to sidebar Skip to footer

How To Smoothly Move A Image- View With Users Finger On Android Emulator

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.setOnTouchListene

Solution 1:

The following is the case what I think it might work.

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... )

The following is how to make it 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)
    {
        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 dobreak;
            default :
                break;
        }
        returntrue;
    }
});

======================================================================== ========================== [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 :

publicclassPointF
{
  publicfloat x = 0;
  publicfloat y = 0;
  publicPointF(){};
  publicPointF(float _x, float _y ){ x = _x; y = _y; }
  publicvoidset(float _x, float _y ){ x = _x; y = _y; }

}

Solution 2:

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;

Post a Comment for "How To Smoothly Move A Image- View With Users Finger On Android Emulator"