Android Implementation view Drag to Any Location

  • 2021-12-12 09:38:35
  • OfStack

This article realizes: Drag the picture arbitrarily, if dragging to the correct position, it will be successful, and if the hand is lifted at the wrong position, it will automatically return to its original position.

Definition


private ImageView img;
private ImageView imageView;

// The width and height of the container can only be obtained after the screen is drawn 
private int containerWidth;
    private int containerHeight;
    
    private float lastX, lastY;

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

Get the width, height and position after the screen is drawn


@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        //  Here to get the width and height of the container 
        if (hasFocus) {
            containerHeight = sunPage.getHeight();
            containerWidth = sunPage.getWidth();
        }
        
        // Gets the correct location to drag to, img Department 
        img.getLocationInWindow(imgPosition);
        height = img.getMeasuredHeight();
        width = img.getMeasuredWidth();
    }

Gesture event


imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                     // Finger in screen position getRawX() getRawY()
                        lastX = event.getRawX();
                        lastY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                    //  Don't use it directly getX And getY, These two acquired data have been processed , It is easy to shake the picture 
            float distanceX = xx - event.getRawX();
            float distanceY = yy - event.getRawY();

            float nextY = imageView.getY() - distanceY;
            float nextX = imageView.getX() - distanceX;

            //  Cannot move off screen 
            if (nextY < 0) {
                nextY = 0;
            } else if (nextY > containerHeight - imageView.getHeight()) {
                nextY = containerHeight - imageView.getHeight();
            }
            if (nextX < 0)
                nextX = 0;
            else if (nextX > containerWidth - imageView.getWidth())
                nextX = containerWidth - imageView.getWidth();

            //  Attribute animation movement 
            ObjectAnimator y = ObjectAnimator.ofFloat(imageView, "y", imageView.getY(), nextY);
            ObjectAnimator x = ObjectAnimator.ofFloat(imageView, "x", imageView.getX(), nextX);

            animatorSet.playTogether(x, y);
            animatorSet.setDuration(0);
            animatorSet.start();
                        lastX = event.getRawX();
                        lastY = event.getRawY();
                        if (correct(lastX, lastY, imgPosition, m)) {
                            // Things after being right 
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                     // After raising your hand, it automatically bounces back to its original place and monitors animatorSet
                        animatorSet.cancel();
                        break;
                }
                return true;
            }
        });

Judge whether the drag is correct, m is the radius of img, and the difficulty of dragging combination can be adjusted by itself


// Determine whether the drag is correct, m Yes img The radius, the difficulty of dragging combination can adjust the algorithm by itself 
private boolean correct(float x, float y, int[] a, int m) {
        float s = (float) Math.sqrt((x - a[0]) * (x - a[0]) + (y - a[1]) * (y - a[1]) - (x - a[0]) * (y - a[1]));
        if (s <= ActivityUtils.dip2px(HomeGameActivity.this, m)) {
            return true;
        }
        return false;
    }

When listening to animatorSet and cancle, imageview returns to its original place


animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                imageView.setTranslationY(0);
          imageView.setTranslationX(0);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
 });

Related articles: