10

ImageView当我在屏幕上移动时,第一次一切都很好,但第二次ImageView移动不正常。

这是我到目前为止所做的。

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;
    }
});
4

2 回答 2

41

以下是我认为可能有效的情况。

我看到您使用的是img.getX(), img.getY(),所以我假设您使用的是 API 级别 11 或更高版本。

我假设你imgImageView. FrameLayout.LayoutParams(虽然 for ImageView的用法很奇怪......)

以下是如何正确制作它:

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 添加]
============================================ ==================================================== =======

这里介绍的新对象是PointF。请使用以下代码导入PointF对象:

import android.graphics.PointF;

实际上,这只是一个记录 float x 和 float 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 void set( float _x, float _y ){ x = _x; y = _y; }

}
于 2013-05-15T10:08:59.410 回答
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;
于 2017-01-24T05:19:30.037 回答