This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMovableFloatingActionButton.java
160 lines (127 loc) · 6.35 KB
/
MovableFloatingActionButton.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by matteo on 25/01/2018.
*/
public class MovableFloatingActionButton extends FloatingActionButton implements View.OnTouchListener {
private final static float CLICK_DRAG_TOLERANCE = 10; // Often, there will be a slight, unintentional, drag when the user taps the FAB, so we need to account for this.
private float downRawX, downRawY;
private float dX, dY;
private CoordinatorLayout.LayoutParams coordinatorLayout;
public MovableFloatingActionButton(Context context) {
super(context);
init();
}
public MovableFloatingActionButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MovableFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
View viewParent;
switch (motionEvent.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
Log.d("MovableFAB", "ACTION_DOWN");
downRawX = motionEvent.getRawX();
downRawY = motionEvent.getRawY();
dX = view.getX() - downRawX;
dY = view.getY() - downRawY;
return true; // Consumed
case MotionEvent.ACTION_MOVE:
int viewWidth = view.getWidth();
int viewHeight = view.getHeight();
viewParent = (View) view.getParent();
int parentWidth = viewParent.getWidth();
int parentHeight = viewParent.getHeight();
float newX = motionEvent.getRawX() + dX;
newX = Math.max(0, newX); // Don't allow the FAB past the left hand side of the parent
newX = Math.min(parentWidth - viewWidth, newX); // Don't allow the FAB past the right hand side of the parent
float newY = motionEvent.getRawY() + dY;
newY = Math.max(0, newY); // Don't allow the FAB past the top of the parent
newY = Math.min(parentHeight - viewHeight, newY); // Don't allow the FAB past the bottom of the parent
view.animate()
.x(newX)
.y(newY)
.setDuration(0)
.start();
return true; // Consumed
case MotionEvent.ACTION_UP:
float upRawX = motionEvent.getRawX();
float upRawY = motionEvent.getRawY();
float upDX = upRawX - downRawX;
float upDY = upRawY - downRawY;
if((Math.abs(upDX) < CLICK_DRAG_TOLERANCE && Math.abs(upDY) < CLICK_DRAG_TOLERANCE) && performClick())
return true;
View viewParent2 = (View) view.getParent();
float borderY,borderX;
float oldX=view.getX(), oldY=view.getY();
float finalX,finalY;
borderY = Math.min(view.getY()-viewParent2.getTop(),viewParent2.getBottom()-view.getY());
borderX = Math.min(view.getX()-viewParent2.getLeft(),viewParent2.getRight()-view.getX());
//You can set your dp margin from dimension resources (Suggested)
//float fab_margin= getResources().getDimension(R.dimen.fab_margin);
float fab_margin=15;
//check if is nearest Y o X
if(borderX>borderY) {
if(view.getY()>viewParent2.getHeight()/2) { //view near Bottom
finalY = viewParent2.getBottom() - view.getHeight();
finalY = Math.min(viewParent2.getHeight() - view.getHeight(), finalY) - fab_margin; // Don't allow the FAB past the bottom of the parent
}
else { //view vicina a Top
finalY = viewParent2.getTop();
finalY = Math.max(0, finalY) + fab_margin; // Don't allow the FAB past the top of the parent
}
//check if X it's over fab_margin
finalX=oldX;
if(view.getX()+viewParent2.getLeft()<fab_margin)
finalX=viewParent2.getLeft()+fab_margin;
if(viewParent2.getRight()-view.getX()-view.getWidth()<fab_margin)
finalX=viewParent2.getRight()- view.getWidth()-fab_margin;
}
else { //view near Right
if(view.getX()>viewParent2.getWidth()/2) {
finalX = viewParent2.getRight() - view.getWidth();
finalX = Math.max(0, finalX) - fab_margin; // Don't allow the FAB past the left hand side of the parent
}
else { //view near Left
finalX = viewParent2.getLeft();
finalX = Math.min(viewParent2.getWidth() - view.getWidth(), finalX) + fab_margin; // Don't allow the FAB past the right hand side of the parent
}
//check if Y it's over fab_margin
finalY=oldY;
if(view.getY()+viewParent2.getTop()<fab_margin)
finalY=viewParent2.getTop()+fab_margin;
if(viewParent2.getBottom()-view.getY()-view.getHeight()<fab_margin)
finalY=viewParent2.getBottom()-view.getHeight()-fab_margin;
}
view.animate()
.x(finalX)
.y(finalY)
.setDuration(400)
.start();
Log.d("MovableFAB", "ACTION_UP");
return false;
// A drag consumed
default:
return super.onTouchEvent(motionEvent);
}
}
public CoordinatorLayout.LayoutParams getCoordinatorLayout() {
return coordinatorLayout;
}
public void setCoordinatorLayout(CoordinatorLayout.LayoutParams coordinatorLayout) {
this.coordinatorLayout = coordinatorLayout;
}
}