SoFunction
Updated on 2025-03-11

Android implements view dragging to any position

This article implements: drag the picture at will, and if it is dragged to the correct position, it will be successful. If you raise your hand, it will automatically return to its original position.

definition

private ImageView img;
private ImageView imageView;

//The width and height of the container need to be obtained after the screen is drawnprivate int containerWidth;
    private int containerHeight;
    
    private float lastX, lastY;

//Get the correct position you need to drag to private int[] imgPosition = new int[2];
    private int height;
    private int width;

Get width and height after the screen is drawn

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        (hasFocus);
        // Here we get the width and height of the container        if (hasFocus) {
            containerHeight = ();
            containerWidth = ();
        }
        
        //Get the correct position you need to drag to, img        (imgPosition);
        height = ();
        width = ();
    }

Gesture Events

(new () {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (()) {
                    case MotionEvent.ACTION_DOWN:
                     //Finger at screen position getRawX() getRawY()                        lastX = ();
                        lastY = ();
                        break;
                    case MotionEvent.ACTION_MOVE:
                    // Do not use getX and getY directly. The data obtained by these two have been processed and are prone to image jitter            float distanceX = xx - ();
            float distanceY = yy - ();

            float nextY = () - distanceY;
            float nextX = () - distanceX;

            // Cannot move out of the screen            if (nextY < 0) {
                nextY = 0;
            } else if (nextY > containerHeight - ()) {
                nextY = containerHeight - ();
            }
            if (nextX < 0)
                nextX = 0;
            else if (nextX > containerWidth - ())
                nextX = containerWidth - ();

            // Attribute animation move            ObjectAnimator y = (imageView, "y", (), nextY);
            ObjectAnimator x = (imageView, "x", (), nextX);

            (x, y);
            (0);
            ();
                        lastX = ();
                        lastY = ();
                        if (correct(lastX, lastY, imgPosition, m)) {
                            //What's right                        }
                        break;
                    case MotionEvent.ACTION_UP:
                     // After raising your hand, you will automatically bounce back to the original place and listen to the animatorSet                        ();
                        break;
                }
                return true;
            }
        });

Determine whether the drag is correct. m is the radius of img. You can adjust the algorithm yourself if you combine the drag.

//Judge whether the drag is correct. m is the radius of img. You can adjust the algorithm yourself to combine the dragging method.private boolean correct(float x, float y, int[] a, int m) {
        float s = (float) ((x - a[0]) * (x - a[0]) + (y - a[1]) * (y - a[1]) - (x - a[0]) * (y - a[1]));
        if (s <= ActivityUtils.dip2px(, m)) {
            return true;
        }
        return false;
    }

Listen to animatorSet, imageview returns to original location when cancle

(new () {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                (0);
          (0);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
 });

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.