Skip to content

Instantly share code, notes, and snippets.

@jsaund
Created February 22, 2017 09:11
Show Gist options
  • Select an option

  • Save jsaund/93fe45fdcc8f2dde8ba01ad17dc3e47b to your computer and use it in GitHub Desktop.

Select an option

Save jsaund/93fe45fdcc8f2dde8ba01ad17dc3e47b to your computer and use it in GitHub Desktop.
public void touchEventHandler(@NotNull View view) {
final ConnectedObservable<MotionEvent> motionEventObservable = RxView.touches(view).publish();
// Capture down events
final Observable<MotionEvent> downEventsObservable = motionEventObservable
.filter(event -> event.getAction() == MotionEvent.ACTION_DOWN);
// Capture up events
final Observable<MotionEvent> upEventsObservable = motionEventObservable
.filter(event -> event.getAction() == MotionEvent.ACTION_UP);
// Show a red circle at the position where the down event ocurred
subscriptions.add(downEventsObservable.subscribe(event ->
view.showCircle(event.getX(), event.getY(), Color.RED)));
// Show a blue circle at the position where the up event ocurred
subscriptions.add(upEventsObservable.subscribe(event ->
view.showCircle(event.getX(), event.getY(), Color.BLUE)));
// Connect the source observable to begin emitting events
subscriptions.add(motionEventObservable.connect());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment