如何在屏幕触摸上移动图像


final ImageView imageView = (ImageView) findViewById(R.id.imageView);
            if (imageView != null) {
                imageView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent event) {
                        int eid = event.getAction();
                        switch (eid) {
                            case MotionEvent.ACTION_MOVE :
                                ConstraintLayout.LayoutParams mParams = (ConstraintLayout.LayoutParams) imageView.getLayoutParams();
                                int x = (int) event.getRawX();
                                int y = (int) event.getRawY();
                                mParams.leftMargin = x - 50;
                                mParams.topMargin = y - 50;
                                imageView.setLayoutParams(mParams);
                                break;
                            default :
                                break;
                        }
                        return true;
                    }
                });
            }

这段代码工作:选择图像并在屏幕上移动图像,但我想在屏幕上移动图像触摸我想要在屏幕上移动图像触摸:

如何使用 onTouch 移动 RelativeLayout 中包含的所有视图的示例

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
    private RelativeLayout mRelLay;
    private float mInitialX, mInitialY;
    private int mInitialLeft, mInitialTop;
    private View mMovingView = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRelLay = (RelativeLayout) findViewById(R.id.relativeLayout);
        for (int i = 0; i < mRelLay.getChildCount(); i++)
            mRelLay.getChildAt(i).setOnTouchListener(this);
    }
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        RelativeLayout.LayoutParams mLayoutParams;
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mMovingView = view;
                mLayoutParams = (RelativeLayout.LayoutParams) mMovingView.getLayoutParams();
                mInitialX = motionEvent.getRawX();
                mInitialY = motionEvent.getRawY();
                mInitialLeft = mLayoutParams.leftMargin;
                mInitialTop = mLayoutParams.topMargin;
                break;
            case MotionEvent.ACTION_MOVE:
                if (mMovingView != null) {
                    mLayoutParams = (RelativeLayout.LayoutParams) mMovingView.getLayoutParams();
                    mLayoutParams.leftMargin = (int) (mInitialLeft + motionEvent.getRawX() - mInitialX);
                    mLayoutParams.topMargin = (int) (mInitialTop + motionEvent.getRawY() - mInitialY);
                    mMovingView.setLayoutParams(mLayoutParams);
                }
                break;
            case MotionEvent.ACTION_UP:
                mMovingView = null;
                break;
        }
        return true;
    }
}

最新更新