Created
January 19, 2016 02:05
-
-
Save kuanyingchou/03e1636a745f47402868 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ken; | |
import rx.*; | |
import java.util.concurrent.TimeUnit; | |
enum MotionEvent { Down, Move, Up }; | |
class Main { | |
public static void test1() { | |
Observable.just("Hello") | |
.map(s -> s+"!!!") | |
.subscribe(s -> System.out.println(s)); | |
} | |
public static void test2() { | |
final float speed = 1.0f; | |
Observable.from(new MotionEvent[] { | |
MotionEvent.Down, | |
MotionEvent.Move, | |
MotionEvent.Move, | |
MotionEvent.Move, | |
MotionEvent.Up | |
} | |
).zipWith( | |
Observable.interval(2, TimeUnit.SECONDS), | |
(x, y) -> x | |
) | |
.map(e -> { | |
if(e == MotionEvent.Move) { | |
return 2; //TODO | |
} else { | |
return 0; | |
} | |
} | |
) | |
.switchMap( | |
distance -> { | |
if(distance == 0) { | |
return Observable.empty(); | |
} else { | |
return Observable | |
.just("move") | |
.zipWith( | |
Observable.interval( | |
(long)(1.0 / distance * speed), TimeUnit.SECONDS), | |
(x, y) -> x | |
); | |
} | |
} | |
) | |
.subscribe(s -> System.out.println(s)); | |
try { | |
Thread.sleep(11000); | |
} catch(InterruptedException e) {}; | |
} | |
public static void main(String[] args) { | |
//test1(); | |
test2(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment