Skip to content

Commit

Permalink
set card bounds whilst moving
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Aug 16, 2023
1 parent d91e619 commit f2bc8ef
Showing 1 changed file with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,30 @@ 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());
dY.set(cardView.getY() + v.getY() - event.getRawY());
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;

Expand All @@ -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;

Expand Down

0 comments on commit f2bc8ef

Please sign in to comment.