Android - move an ImageView on screen (Like dragging)

前端 未结 6 1198
灰色年华
灰色年华 2021-02-05 15:34

I\'m trying to create an app that can move an ImageView on your device like dragging and when I put like 75% of the ImageView out of the screen show a

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-05 16:08

    I'm using this method to drag an ImageView , I hope that this can help you : So I defined those attributes of the class :

     private float xCoOrdinate, yCoOrdinate;
     private double screenCenterX, screenCenterY;
    

    Then I implement this code under the OnCreate() method of the activity :

     mRrootLayout.getBackground().setAlpha(255);
    
        /**
         * Calculate max hypo value and center of screen.
         */
        final DisplayMetrics display = getResources().getDisplayMetrics();
        screenCenterX = display.widthPixels / 2;
        screenCenterY = (display.heightPixels - getStatusBarHeight()) / 2;
        final double maxHypo = Math.hypot(screenCenterX, screenCenterY);
    
        mImageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                /**
                 * Calculate hypo value of current imageview position according to center
                 */
                double centerYPos = mImageView.getY() + (mImageView.getHeight() / 2);
                double centerXPos = mImageView.getX() + (mImageView.getWidth() / 2);
                double a = screenCenterX - centerXPos;
                double b = screenCenterY - centerYPos;
                double hypo = Math.hypot(a, b);
    
                /**
                 * change alpha of background of layout
                 */
                alpha = (int) (hypo * 255) / (int) maxHypo;
                if (alpha < 255)
                    mRrootLayout.getBackground().setAlpha(255 - alpha);
    
                switch (event.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        xCoOrdinate = mImageView.getX() - event.getRawX();
                        yCoOrdinate = mImageView.getY() - event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mImageView.animate().x(event.getRawX() + xCoOrdinate).y(event.getRawY() + yCoOrdinate).setDuration(0).start();
                        break;
                    case MotionEvent.ACTION_UP:
                         if (alpha > 50) {
                            Toast.makeText(ImageViewerActivity.this, "Out", Toast.LENGTH_SHORT).show();
                            return false;
                        } else {
                            Toast.makeText(ImageViewerActivity.this, "In", Toast.LENGTH_SHORT).show();
                            mImageView.animate().x(0).y((float) screenCenterY - mImageView.getHeight() / 2).setDuration(100).start();
                            mRrootLayout.getBackground().setAlpha(255);
                        }
                    default:
                        return false;
                }
                return true;
            }
        });
    

提交回复
热议问题