Skip to content

Commit ddd7685

Browse files
committed
Swipe distance is now configurable from java, not xml.
Known issue: arrow animates in background while the button is loading and morphing
1 parent 98af014 commit ddd7685

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

app/src/main/java/in/shadowfax/proswipebutton_app/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ protected void onCreate(Bundle savedInstanceState) {
1515

1616
final ProSwipeButton proSwipeBtn = findViewById(R.id.proswipebutton_main);
1717
final ProSwipeButton proSwipeBtnError = findViewById(R.id.proswipebutton_main_error);
18+
proSwipeBtn.setSwipeDistance(0.5f);
1819

1920
proSwipeBtn.setOnSwipeListener(new ProSwipeButton.OnSwipeListener() {
2021
@Override
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package in.shadowfax.proswipebutton;
2+
3+
import static in.shadowfax.proswipebutton.UiUtils.dpToPx;
4+
5+
/**
6+
* Created by shadow-admin on 22/2/18.
7+
*/
8+
9+
public class Constants {
10+
11+
public static final float DEFAULT_TEXT_SIZE = dpToPx(14);
12+
public static final float DEFAULT_SWIPE_DISTANCE = 0.85f;
13+
public static final int BTN_INIT_RADIUS = dpToPx(2);
14+
public static final int BTN_MORPHED_RADIUS = dpToPx(100);
15+
public static final int MORPH_ANIM_DURATION = 500;
16+
17+
}

proswipebutton/src/main/java/in/shadowfax/proswipebutton/ProSwipeButton.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
import android.widget.RelativeLayout;
3030
import android.widget.TextView;
3131

32+
import static in.shadowfax.proswipebutton.Constants.BTN_INIT_RADIUS;
33+
import static in.shadowfax.proswipebutton.Constants.BTN_MORPHED_RADIUS;
34+
import static in.shadowfax.proswipebutton.Constants.DEFAULT_SWIPE_DISTANCE;
35+
import static in.shadowfax.proswipebutton.Constants.DEFAULT_TEXT_SIZE;
36+
import static in.shadowfax.proswipebutton.Constants.MORPH_ANIM_DURATION;
3237
import static in.shadowfax.proswipebutton.UiUtils.animateFadeHide;
3338
import static in.shadowfax.proswipebutton.UiUtils.animateFadeShow;
3439
import static in.shadowfax.proswipebutton.UiUtils.dpToPx;
@@ -48,10 +53,6 @@ public class ProSwipeButton extends RelativeLayout {
4853
private ImageView arrow2;
4954
private LinearLayout arrowHintContainer;
5055
private ProgressBar progressBar;
51-
private static final float DEFAULT_TEXT_SIZE = dpToPx(14);
52-
private static final int BTN_INIT_RADIUS = dpToPx(2);
53-
private static final int BTN_MORPHED_RADIUS = dpToPx(100);
54-
private static final int MORPH_ANIM_DURATION = 500;
5556

5657
//// TODO: 26/10/17 Add touch blocking
5758

@@ -70,6 +71,7 @@ public class ProSwipeButton extends RelativeLayout {
7071
private float textSize = DEFAULT_TEXT_SIZE;
7172
@Nullable
7273
private OnSwipeListener swipeListener = null;
74+
private float swipeDistance = DEFAULT_SWIPE_DISTANCE;
7375

7476
public ProSwipeButton(Context context) {
7577
super(context);
@@ -168,7 +170,7 @@ public boolean onTouch(View v, MotionEvent event) {
168170
return true;
169171
case MotionEvent.ACTION_UP:
170172
//Release logic here
171-
if (arrowHintContainer.getX() + arrowHintContainer.getWidth() > getWidth() * 0.85) {
173+
if (arrowHintContainer.getX() + arrowHintContainer.getWidth() > getWidth() * swipeDistance) {
172174
// swipe completed, fly the hint away!
173175
animateFadeHide(context, arrowHintContainer);
174176
if (swipeListener != null)
@@ -429,6 +431,26 @@ public float getTextSize() {
429431
return this.textSize;
430432
}
431433

434+
/**
435+
* How much of the button must the user swipe to trigger the OnSwipeListener successfully
436+
*
437+
* @param swipeDistance float from 0.0 to 1.0 where 1.0 means user must swipe the button fully from end to end. Default is 0.85.
438+
*/
439+
public void setSwipeDistance(@Dimension float swipeDistance) {
440+
if (swipeDistance > 1.0f) {
441+
swipeDistance = 1.0f;
442+
}
443+
if (swipeDistance < 0.0f) {
444+
swipeDistance = 0.0f;
445+
}
446+
this.swipeDistance = swipeDistance;
447+
}
448+
449+
@Dimension
450+
public float getSwipeDistance() {
451+
return this.swipeDistance;
452+
}
453+
432454
public void setOnSwipeListener(@Nullable OnSwipeListener customSwipeListener) {
433455
this.swipeListener = customSwipeListener;
434456
}

0 commit comments

Comments
 (0)