42
42
* Created by shadow-admin on 24/10/17.
43
43
*/
44
44
45
- public class ProSwipeButton extends RelativeLayout {
45
+ public class ProSwipeButton extends RelativeLayout implements View . OnTouchListener {
46
46
47
47
private Context context ;
48
48
private View view ;
@@ -115,6 +115,7 @@ private void setAttrs(Context context, AttributeSet attrs) {
115
115
public void init () {
116
116
LayoutInflater inflater = LayoutInflater .from (context );
117
117
view = inflater .inflate (R .layout .view_proswipebtn , this , true );
118
+ setOnTouchListener (this );
118
119
}
119
120
120
121
@ Override
@@ -136,56 +137,51 @@ protected void onFinishInflate() {
136
137
gradientDrawable .setCornerRadius (btnRadius );
137
138
setBackgroundColor (bgColorInt );
138
139
updateBackground ();
139
- setupTouchListener ();
140
140
}
141
141
142
- private void setupTouchListener () {
143
- setOnTouchListener (new OnTouchListener () {
144
- @ Override
145
- public boolean onTouch (View v , MotionEvent event ) {
146
- switch (event .getAction ()) {
147
- case MotionEvent .ACTION_DOWN :
148
- return true ;
149
- case MotionEvent .ACTION_MOVE :
150
- // Movement logic here
151
- if (event .getX () > arrowHintContainer .getWidth () / 2 &&
152
- event .getX () + arrowHintContainer .getWidth () / 2 < getWidth () &&
153
- (event .getX () < arrowHintContainer .getX () + arrowHintContainer .getWidth () || arrowHintContainer .getX () != 0 )) {
154
- // snaps the hint to user touch, only if the touch is within hint width or if it has already been displaced
155
- arrowHintContainer .setX (event .getX () - arrowHintContainer .getWidth () / 2 );
156
- }
157
-
158
- if (arrowHintContainer .getX () + arrowHintContainer .getWidth () > getWidth () &&
159
- arrowHintContainer .getX () + arrowHintContainer .getWidth () / 2 < getWidth ()) {
160
- // allows the hint to go up to a max of btn container width
161
- arrowHintContainer .setX (getWidth () - arrowHintContainer .getWidth ());
162
- }
163
-
164
- if (event .getX () < arrowHintContainer .getWidth () / 2 &&
165
- arrowHintContainer .getX () > 0 ) {
166
- // allows the hint to go up to a min of btn container starting
167
- arrowHintContainer .setX (0 );
168
- }
169
-
170
- return true ;
171
- case MotionEvent .ACTION_UP :
172
- //Release logic here
173
- if (arrowHintContainer .getX () + arrowHintContainer .getWidth () > getWidth () * swipeDistance ) {
174
- // swipe completed, fly the hint away!
175
- performSuccessfulSwipe ();
176
- } else if (arrowHintContainer .getX () <= 0 ) {
177
- // upon click without swipe
178
- startFwdAnim ();
179
- } else {
180
- // swipe not completed, pull back the hint
181
- animateHintBack ();
182
- }
183
- return true ;
142
+ @ Override
143
+ public boolean onTouch (View v , MotionEvent event ) {
144
+ switch (event .getAction ()) {
145
+ case MotionEvent .ACTION_DOWN :
146
+ return true ;
147
+ case MotionEvent .ACTION_MOVE :
148
+ // Movement logic here
149
+ if (event .getX () > arrowHintContainer .getWidth () / 2 &&
150
+ event .getX () + arrowHintContainer .getWidth () / 2 < getWidth () &&
151
+ (event .getX () < arrowHintContainer .getX () + arrowHintContainer .getWidth () || arrowHintContainer .getX () != 0 )) {
152
+ // snaps the hint to user touch, only if the touch is within hint width or if it has already been displaced
153
+ arrowHintContainer .setX (event .getX () - arrowHintContainer .getWidth () / 2 );
184
154
}
185
155
186
- return false ;
187
- }
188
- });
156
+ if (arrowHintContainer .getX () + arrowHintContainer .getWidth () > getWidth () &&
157
+ arrowHintContainer .getX () + arrowHintContainer .getWidth () / 2 < getWidth ()) {
158
+ // allows the hint to go up to a max of btn container width
159
+ arrowHintContainer .setX (getWidth () - arrowHintContainer .getWidth ());
160
+ }
161
+
162
+ if (event .getX () < arrowHintContainer .getWidth () / 2 &&
163
+ arrowHintContainer .getX () > 0 ) {
164
+ // allows the hint to go up to a min of btn container starting
165
+ arrowHintContainer .setX (0 );
166
+ }
167
+
168
+ return true ;
169
+ case MotionEvent .ACTION_UP :
170
+ //Release logic here
171
+ if (arrowHintContainer .getX () + arrowHintContainer .getWidth () > getWidth () * swipeDistance ) {
172
+ // swipe completed, fly the hint away!
173
+ performSuccessfulSwipe ();
174
+ } else if (arrowHintContainer .getX () <= 0 ) {
175
+ // upon click without swipe
176
+ startFwdAnim ();
177
+ } else {
178
+ // swipe not completed, pull back the hint
179
+ animateHintBack ();
180
+ }
181
+ return true ;
182
+ }
183
+
184
+ return false ;
189
185
}
190
186
191
187
private void performSuccessfulSwipe () {
@@ -297,7 +293,7 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) {
297
293
}
298
294
299
295
private void morphToRect () {
300
- setupTouchListener ( );
296
+ setOnTouchListener ( this );
301
297
ObjectAnimator cornerAnimation =
302
298
ObjectAnimator .ofFloat (gradientDrawable , "cornerRadius" , BTN_MORPHED_RADIUS , BTN_INIT_RADIUS );
303
299
0 commit comments