Search code examples
androidtouch-event

ImageView not moving correctly when dragged


Basically i have this code where i can drag an imageView horizontally it currently works fine until i release and try to move it again it dosen't really follow my touch it just goes in front of the touch or just goes somewhere where i didn't drag .

 micImage.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        final int x = (int) motionEvent.getRawX();
                        final int y = (int) motionEvent.getRawY();

                        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {

                            case MotionEvent.ACTION_DOWN:
                                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams)
                                        view.getLayoutParams();


                                xDelta = x - lParams.leftMargin;
                                yDelta = y - lParams.topMargin;

                                startRecording();

                                break;

                            case MotionEvent.ACTION_UP:

                                break;

                            case MotionEvent.ACTION_MOVE:
                                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                                        .getLayoutParams();
                                layoutParams.leftMargin = 0;
                                layoutParams.topMargin = 0;
                                layoutParams.rightMargin =  xDelta - x;
                                layoutParams.bottomMargin =  yDelta - y;
                                view.setLayoutParams(layoutParams);
                                break;

                        }

                        mainLayout.invalidate();
                        return true;
                    }
                });

i want the imageView to follow my finger correctly and thanks !


Solution

  • Here is a much simpler way to achieve the same result:

    micImage.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    xDelta = view.getX() - event.getRawX();
                    yDelta = view.getY() - event.getRawY();
                    startRecording();
                    break;
                case MotionEvent.ACTION_MOVE:
                    view.animate().x(event.getRawX() + xDelta).y(event.getRawY() + yDelta).setDuration(0).start();
                    break;
                default:
                    return false;
                }
           return true;
        }
    });
    

    I'm not 100% sure, but I believe your issue is caused by not setting the default boolean to false.


    Edit: Answering the question in the comments.

    You can achieve this by doing the following: (I have not tested this, but it should work.)

    int originalX = micImage.getX();
    int originalY = micImage.getY();
    
    micImage.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    xDelta = view.getX() - event.getRawX();
                    yDelta = view.getY() - event.getRawY();
                    startRecording();
                    break;
                case MotionEvent.ACTION_MOVE:
                    view.animate().x(event.getRawX() + xDelta).y(event.getRawY() + yDelta).setDuration(0).start();
                    break;
                case MotionEvent.ACTION_UP:
                    view.animate().x(originalX).y(originalY).setDuration(0).start();
                    break;
                default:
                    return false;
                }
               return true;
        }
    });