11
11
import android .graphics .RectF ;
12
12
import android .util .AttributeSet ;
13
13
import android .util .TypedValue ;
14
+ import android .view .MotionEvent ;
14
15
import android .view .View ;
15
16
16
17
import com .facebook .rebound .SimpleSpringListener ;
19
20
import com .facebook .rebound .SpringSystem ;
20
21
import com .facebook .rebound .SpringUtil ;
21
22
22
- import static it .beppi .tristatetogglebutton_library .TriStateToggleButton .ToggleStatus .off ;
23
23
import static it .beppi .tristatetogglebutton_library .TriStateToggleButton .ToggleStatus .mid ;
24
+ import static it .beppi .tristatetogglebutton_library .TriStateToggleButton .ToggleStatus .off ;
24
25
import static it .beppi .tristatetogglebutton_library .TriStateToggleButton .ToggleStatus .on ;
25
26
26
27
@@ -120,6 +121,10 @@ public static ToggleStatus intToToggleStatus(int toggleIntValue) {
120
121
// Beppi: added midSelectable
121
122
private boolean midSelectable = true ;
122
123
124
+ // Beppi: swipe management
125
+ private int swipeSensitivityPixels = 200 ;
126
+ private int swipeX = 0 ;
127
+
123
128
/** 是否默认处于打开状态*/ // Whether it is on by default
124
129
// Beppi: changed the type of isDefaultOn from boolean to ToggleStatus
125
130
// Beppi: refactored the variable name from isDefaultOn to defaultStatus
@@ -129,6 +134,8 @@ public static ToggleStatus intToToggleStatus(int toggleIntValue) {
129
134
private boolean enabled = true ;
130
135
private int disabledColor = Color .parseColor ("#bdbdbd" ); // grey 400
131
136
137
+ private boolean swipeing = false ;
138
+
132
139
private OnToggleChanged listener ;
133
140
134
141
private TriStateToggleButton (Context context ) {
@@ -162,13 +169,48 @@ public void setup(AttributeSet attrs) {
162
169
springSystem = SpringSystem .create ();
163
170
spring = springSystem .createSpring ();
164
171
spring .setSpringConfig (SpringConfig .fromOrigamiTensionAndFriction (50 , 7 ));
165
-
172
+
173
+ /* with onTouch this has become useless
166
174
this.setOnClickListener(new OnClickListener() {
167
175
@Override
168
176
public void onClick(View arg0) {
169
177
toggle(defaultAnimate);
170
178
}
171
179
});
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
+ });
172
214
173
215
TypedArray typedArray = getContext ().obtainStyledAttributes (attrs , R .styleable .TriStateToggleButton );
174
216
offBorderColor = typedArray .getColor (R .styleable .TriStateToggleButton_tbOffBorderColor , offBorderColor );
@@ -187,6 +229,10 @@ public void onClick(View arg0) {
187
229
// Beppi: added enabled
188
230
enabled = typedArray .getBoolean (R .styleable .TriStateToggleButton_enabled , enabled );
189
231
typedArray .recycle ();
232
+
233
+ // Beppi: swipe
234
+ swipeSensitivityPixels = typedArray .getInt (R .styleable .TriStateToggleButton_tbSwipeSensitivityPixels , swipeSensitivityPixels );
235
+ // 0 == off
190
236
191
237
borderColor = offBorderColor ;
192
238
@@ -224,7 +270,7 @@ public void toggle(boolean animate) {
224
270
listener .onToggle (toggleStatus , toggleStatusToBoolean (toggleStatus ), toggleStatusToInt (toggleStatus ));
225
271
}
226
272
}
227
-
273
+
228
274
public void toggleOn () {
229
275
setToggleOn ();
230
276
if (listener != null ){
@@ -316,6 +362,35 @@ public void setToggleStatus(int toggleIntValue, boolean animate) {
316
362
setToggleStatus (intToToggleStatus (toggleIntValue ), animate );
317
363
}
318
364
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
+
319
394
// Beppi: rewritten takeEffect() method to manage 3 states
320
395
/*
321
396
private void takeEffect(boolean animate) {
@@ -371,8 +446,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
371
446
372
447
373
448
@ 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 ) {
376
450
super .onLayout (changed , left , top , right , bottom );
377
451
378
452
final int width = getWidth ();
@@ -414,7 +488,7 @@ public void draw(Canvas canvas) {
414
488
if (offLineWidth > 0 ){
415
489
final float cy = offLineWidth * 0.5f ;
416
490
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 );
418
492
canvas .drawRoundRect (rect , cy , cy , paint );
419
493
}
420
494
@@ -478,7 +552,11 @@ private void calculateEffect(final double value) {
478
552
} else
479
553
if (previousToggleStatus == on && toggleStatus == off ) {
480
554
toColor = offBorderColor ; fromColor = onColor ;
481
- } else {
555
+ } else
556
+ if (previousToggleStatus == on && toggleStatus == mid ) {
557
+ toColor = midColor ; fromColor = onColor ;
558
+ } else
559
+ {
482
560
toColor = offBorderColor ; fromColor = onColor ;
483
561
}
484
562
0 commit comments