Android實現view拖動到任意位置

本文實現:將圖片任意拖動,如果拖動到正確位置則成功,若抬起手時時錯誤位置則自動回到原位。

定義

private ImageView img;
private ImageView imageView;

//容器的寬高,需要在屏幕繪制好之後才能獲取
private int containerWidth;
    private int containerHeight;
    
    private float lastX, lastY;

//獲取需要拖動到的正確位置
 private int[] imgPosition = new int[2];
    private int height;
    private int width;

在屏幕繪制好之後獲取寬高和位置

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        // 這裡來獲取容器的寬和高
        if (hasFocus) {
            containerHeight = sunPage.getHeight();
            containerWidth = sunPage.getWidth();
        }
        
        //獲取需要拖動到的正確位置,img處
        img.getLocationInWindow(imgPosition);
        height = img.getMeasuredHeight();
        width = img.getMeasuredWidth();
    }

手勢事件

imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                     //手指在屏幕位置getRawX() getRawY()
                        lastX = event.getRawX();
                        lastY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                    // 不要直接用getX和getY,這兩個獲取的數據已經是經過處理的,容易出現圖片抖動的情況
            float distanceX = xx - event.getRawX();
            float distanceY = yy - event.getRawY();

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

            // 不能移出屏幕
            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();

            // 屬性動畫移動
            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)) {
                            //正確後的事情
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                     //抬起手後自動彈回原處,監聽animatorSet
                        animatorSet.cancel();
                        break;
                }
                return true;
            }
        });

判斷拖動是否正確,m是img的半徑,拖動結合的難易可以自己調劑算法

//判斷拖動是否正確,m是img的半徑,拖動結合的難易可以自己調劑算法
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;
    }

監聽animatorSet,cancle時imageview返回原處

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) {

            }
 });

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀:

    None Found