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 936728c
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.concurrent.atomic.AtomicReference;
import phone.vishnu.quotes.R;
import phone.vishnu.quotes.helper.SharedPreferenceHelper;
import phone.vishnu.quotes.helper.Utils;
import phone.vishnu.quotes.model.Quote;

public class CustomiseFragment extends Fragment {
Expand Down Expand Up @@ -141,15 +142,32 @@ public void onViewCreated(

moveIV.setOnTouchListener(
(v, event) -> {
int DP_8 = Utils.Companion.DPtoPX(requireContext(), 8);

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(DP_8, event.getRawX() + dX.get()); // left bound
float finalY = Math.max(DP_8, event.getRawY() + dY.get()); // top bound

cardView.setX(
finalX + cardViewWidth > constraintLayoutWidth - DP_8
? constraintLayoutWidth - cardViewWidth - DP_8
: finalX); // right bound

cardView.setY(
finalY + cardViewHeight > constraintLayoutHeight - DP_8
? constraintLayoutHeight - cardViewHeight - DP_8
: finalY); // bottom bound

break;

Expand All @@ -158,9 +176,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 936728c

Please sign in to comment.