Skip to content

Instantly share code, notes, and snippets.

@kuanyingchou
Created January 19, 2016 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuanyingchou/03e1636a745f47402868 to your computer and use it in GitHub Desktop.
Save kuanyingchou/03e1636a745f47402868 to your computer and use it in GitHub Desktop.
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