From f2bc8efde05c376cf984b3f22a7125797dc05ffb Mon Sep 17 00:00:00 2001 From: VishnuSanal Date: Wed, 16 Aug 2023 13:10:13 +0530 Subject: [PATCH] set card bounds whilst moving --- .../quotes/fragment/CustomiseFragment.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/phone/vishnu/quotes/fragment/CustomiseFragment.java b/app/src/main/java/phone/vishnu/quotes/fragment/CustomiseFragment.java index 78da1dd..a67fa07 100644 --- a/app/src/main/java/phone/vishnu/quotes/fragment/CustomiseFragment.java +++ b/app/src/main/java/phone/vishnu/quotes/fragment/CustomiseFragment.java @@ -141,6 +141,11 @@ public void onViewCreated( moveIV.setOnTouchListener( (v, event) -> { + int constraintLayoutWidth = constraintLayout.getWidth(); + int constraintLayoutHeight = constraintLayout.getHeight(); + int cardViewWidth = cardView.getWidth(); + int cardViewHeight = cardView.getHeight(); + switch (event.getAction()) { case MotionEvent.ACTION_DOWN: dX.set(cardView.getX() + v.getX() - event.getRawX()); @@ -148,8 +153,18 @@ public void onViewCreated( break; case MotionEvent.ACTION_MOVE: - cardView.setX(event.getRawX() + dX.get()); - cardView.setY(event.getRawY() + dY.get()); + float finalX = Math.max(0, event.getRawX() + dX.get()); // left bound + float finalY = Math.max(0, event.getRawY() + dY.get()); // top bound + + cardView.setX( + finalX + cardViewWidth > constraintLayoutWidth + ? constraintLayoutWidth - cardViewWidth + : finalX); // right bound + + cardView.setY( + finalY + cardViewHeight > constraintLayoutHeight + ? constraintLayoutHeight - cardViewHeight + : finalY); // bottom bound break; @@ -158,9 +173,9 @@ public void onViewCreated( cardView.getLocationOnScreen(array); sharedPreferenceHelper.setCardX( - constraintLayout.getWidth() / (float) array[0]); + constraintLayoutWidth / (float) array[0]); sharedPreferenceHelper.setCardY( - constraintLayout.getHeight() / (float) array[1]); + constraintLayoutHeight / (float) array[1]); break;