Skip to content

Commit b3a87a0

Browse files
Beppi MenozziBeppi Menozzi
authored andcommitted
Swipe
1 parent 6c3bfab commit b3a87a0

File tree

6 files changed

+110
-11
lines changed

6 files changed

+110
-11
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Can act with three independent states, or with two states like a standard checkb
1212
* Can become a 2.5-state toggle: on/off and an unselectable mid button
1313
* Can be enabled / disabled
1414
* Can be programmatically controlled
15+
* Works both with clicks and with swipes
1516

1617
<img src="images/tstb.gif"><br>
1718

@@ -89,6 +90,25 @@ To have a two-states toggle button, with an undefined starting value:
8990
Browse the full example here:
9091
<a href="https://github.com/BeppiMenozzi/TriStateToggleButton/tree/master/tristatetogglebutton_sample/src/main">Example</a>
9192

93+
### Attributes description
94+
List of attributes with description:
95+
<tr><td>tbBorderWidth</td><td>Width of the border of the widget</td></tr>
96+
<tr><td>tdOffBorderColor</td><td>Color of the width that appears with the button in state off. Used also for animations.</td></tr>
97+
<tr><td>tbOffColor</td><td>Color of the background of the toggle when off</td></tr>
98+
<tr><td>tbMidColor</td><td>Color of the background of the toggle with in mid position</td></tr>
99+
<tr><td>tbOnColor</td><td>Color of the background of the toggle when on</td></tr>
100+
<tr><td>tbSpotColor</td><td>Color of the handle of the toggle</td></tr>
101+
<tr><td>tbAnimate</td><td>True for animation</td></tr>
102+
<tr><td>tbDefaultStatus</td><td>Starting value for the toggle</td></tr>
103+
<tr><td>tbIsMidSelectable</td><td>If false, the toggle becomes a standard two-states toggle, but can still assume the mid value if forced programmatically or set as default</td></tr>
104+
<tr><td>tbSwipeSensitivityPixels</td><td>Number of pixels a swipe must travel to fire a toggle event. Default is 200. If set to zero, swipes are disabled</td></tr>
105+
106+
107+
### new in 1.1.0
108+
* Added swipe gesture management together with normal click
109+
* Gradle update
110+
* More documentation
111+
92112
### New in 1.0.5
93113
* Fixed: added super in setEnabled()
94114

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.2.2'
8+
classpath 'com.android.tools.build:gradle:2.2.3'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

tristatetogglebutton_library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
defaultConfig {
88
minSdkVersion 9
99
targetSdkVersion 25
10-
versionCode 4
11-
versionName "1.0.5"
10+
versionCode 5
11+
versionName "1.1.0"
1212

1313
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1414

tristatetogglebutton_library/src/main/java/it/beppi/tristatetogglebutton_library/TriStateToggleButton.java

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.graphics.RectF;
1212
import android.util.AttributeSet;
1313
import android.util.TypedValue;
14+
import android.view.MotionEvent;
1415
import android.view.View;
1516

1617
import com.facebook.rebound.SimpleSpringListener;
@@ -19,8 +20,8 @@
1920
import com.facebook.rebound.SpringSystem;
2021
import com.facebook.rebound.SpringUtil;
2122

22-
import static it.beppi.tristatetogglebutton_library.TriStateToggleButton.ToggleStatus.off;
2323
import static it.beppi.tristatetogglebutton_library.TriStateToggleButton.ToggleStatus.mid;
24+
import static it.beppi.tristatetogglebutton_library.TriStateToggleButton.ToggleStatus.off;
2425
import static it.beppi.tristatetogglebutton_library.TriStateToggleButton.ToggleStatus.on;
2526

2627

@@ -120,6 +121,10 @@ public static ToggleStatus intToToggleStatus(int toggleIntValue) {
120121
// Beppi: added midSelectable
121122
private boolean midSelectable = true;
122123

124+
// Beppi: swipe management
125+
private int swipeSensitivityPixels = 200;
126+
private int swipeX = 0;
127+
123128
/** 是否默认处于打开状态*/ // Whether it is on by default
124129
// Beppi: changed the type of isDefaultOn from boolean to ToggleStatus
125130
// Beppi: refactored the variable name from isDefaultOn to defaultStatus
@@ -129,6 +134,8 @@ public static ToggleStatus intToToggleStatus(int toggleIntValue) {
129134
private boolean enabled = true;
130135
private int disabledColor = Color.parseColor("#bdbdbd"); // grey 400
131136

137+
private boolean swipeing = false;
138+
132139
private OnToggleChanged listener;
133140

134141
private TriStateToggleButton(Context context) {
@@ -162,13 +169,48 @@ public void setup(AttributeSet attrs) {
162169
springSystem = SpringSystem.create();
163170
spring = springSystem.createSpring();
164171
spring.setSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(50, 7));
165-
172+
173+
/* with onTouch this has become useless
166174
this.setOnClickListener(new OnClickListener() {
167175
@Override
168176
public void onClick(View arg0) {
169177
toggle(defaultAnimate);
170178
}
171179
});
180+
*/
181+
182+
// Beppi: swipe management
183+
this.setOnTouchListener(new OnTouchListener() {
184+
@Override
185+
public boolean onTouch(View view, MotionEvent motionEvent) {
186+
int x = (int) motionEvent.getX();
187+
int action = motionEvent.getAction();
188+
if (action == MotionEvent.ACTION_DOWN) {
189+
swipeX = x;
190+
swipeing = false;
191+
}
192+
else if (action == MotionEvent.ACTION_MOVE) {
193+
if (swipeSensitivityPixels == 0) return false;
194+
else if (x - swipeX > swipeSensitivityPixels) {
195+
swipeX = x;
196+
swipeing = true;
197+
increaseValue();
198+
return true;
199+
}
200+
else if (swipeX - x > swipeSensitivityPixels) {
201+
swipeX = x;
202+
swipeing = true;
203+
decreaseValue();
204+
return true;
205+
}
206+
}
207+
else if (action == MotionEvent.ACTION_UP) {
208+
if (!swipeing) toggle(defaultAnimate); // here simple clicks are managed.
209+
return true;
210+
}
211+
return false;
212+
}
213+
});
172214

173215
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TriStateToggleButton);
174216
offBorderColor = typedArray.getColor(R.styleable.TriStateToggleButton_tbOffBorderColor, offBorderColor);
@@ -187,6 +229,10 @@ public void onClick(View arg0) {
187229
// Beppi: added enabled
188230
enabled = typedArray.getBoolean(R.styleable.TriStateToggleButton_enabled, enabled);
189231
typedArray.recycle();
232+
233+
// Beppi: swipe
234+
swipeSensitivityPixels = typedArray.getInt(R.styleable.TriStateToggleButton_tbSwipeSensitivityPixels, swipeSensitivityPixels);
235+
// 0 == off
190236

191237
borderColor = offBorderColor;
192238

@@ -224,7 +270,7 @@ public void toggle(boolean animate) {
224270
listener.onToggle(toggleStatus, toggleStatusToBoolean(toggleStatus), toggleStatusToInt(toggleStatus));
225271
}
226272
}
227-
273+
228274
public void toggleOn() {
229275
setToggleOn();
230276
if(listener != null){
@@ -316,6 +362,35 @@ public void setToggleStatus(int toggleIntValue, boolean animate) {
316362
setToggleStatus(intToToggleStatus(toggleIntValue), animate);
317363
}
318364

365+
public void increaseValue(boolean animate) { // same as toggle, but after on does not rewind to off
366+
switch (toggleStatus) {
367+
case off: if (midSelectable) putValueInToggleStatus(mid); else putValueInToggleStatus(on); break;
368+
case mid: putValueInToggleStatus(on); break;
369+
case on: break;
370+
}
371+
takeEffect(animate);
372+
if(listener != null){
373+
listener.onToggle(toggleStatus, toggleStatusToBoolean(toggleStatus), toggleStatusToInt(toggleStatus));
374+
}
375+
}
376+
public void increaseValue() {
377+
increaseValue(true);
378+
}
379+
public void decreaseValue(boolean animate) {
380+
switch (toggleStatus) {
381+
case on: if (midSelectable) putValueInToggleStatus(mid); else putValueInToggleStatus(off); break;
382+
case mid: putValueInToggleStatus(off); break;
383+
case off: break;
384+
}
385+
takeEffect(animate);
386+
if(listener != null){
387+
listener.onToggle(toggleStatus, toggleStatusToBoolean(toggleStatus), toggleStatusToInt(toggleStatus));
388+
}
389+
}
390+
public void decreaseValue() {
391+
decreaseValue(true);
392+
}
393+
319394
// Beppi: rewritten takeEffect() method to manage 3 states
320395
/*
321396
private void takeEffect(boolean animate) {
@@ -371,8 +446,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
371446

372447

373448
@Override
374-
protected void onLayout(boolean changed, int left, int top, int right,
375-
int bottom) {
449+
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
376450
super.onLayout(changed, left, top, right, bottom);
377451

378452
final int width = getWidth();
@@ -414,7 +488,7 @@ public void draw(Canvas canvas) {
414488
if(offLineWidth > 0){
415489
final float cy = offLineWidth * 0.5f;
416490
rect.set(spotX - cy, centerY - cy, endX + cy, centerY + cy);
417-
paint.setColor(enabled ? offColor : disabledColor);
491+
paint.setColor(enabled ? (toggleStatus == mid ? midColor : offColor) : disabledColor);
418492
canvas.drawRoundRect(rect, cy, cy, paint);
419493
}
420494

@@ -478,7 +552,11 @@ private void calculateEffect(final double value) {
478552
} else
479553
if (previousToggleStatus == on && toggleStatus == off) {
480554
toColor = offBorderColor; fromColor = onColor;
481-
} else {
555+
} else
556+
if (previousToggleStatus == on && toggleStatus == mid) {
557+
toColor = midColor; fromColor = onColor;
558+
} else
559+
{
482560
toColor = offBorderColor; fromColor = onColor;
483561
}
484562

tristatetogglebutton_library/src/main/res/values/toggle_button_attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
</attr>
1616
<attr name="tbIsMidSelectable" format="reference|boolean"/>
1717
<attr name="enabled" format="reference|boolean"/>
18+
<attr name="tbSwipeSensitivityPixels" format="integer" />
1819
</declare-styleable>
1920
</resources>
2021

0 commit comments

Comments
 (0)