Skip to content

Instantly share code, notes, and snippets.

@amay077
Created December 10, 2014 05:15
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 amay077/1d22ba8ffa8ad95e9393 to your computer and use it in GitHub Desktop.
Save amay077/1d22ba8ffa8ad95e9393 to your computer and use it in GitHub Desktop.
simply_shape_moving_by_xamarin_with_rx
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Reactive.Linq;
using NDebug = System.Diagnostics.Debug;
using Android.Graphics;
namespace DrawingTest
{
[Activity(Label = "DrawingTest", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
ActionBar.Hide();
var rectangle = FindViewById<View>(Resource.Id.view1);
rectangle.DownEvent()
.SelectMany(rectangle.MoveEvent())
.Subscribe(e =>
{
rectangle.SetX(e.RawX);
rectangle.SetY(e.RawY - 50f);
});
}
}
}
using System;
using Android.Views;
using System.Reactive.Linq;
namespace DrawingTest
{
public static class ViewExtensions
{
public static IObservable<MotionEvent> DownEvent(this View v)
{
return Observable.FromEventPattern<View.TouchEventArgs>(h => v.Touch += h, h => v.Touch -= h)
.Where(e => e.EventArgs.Event.Action == MotionEventActions.Down)
.Select(e => e.EventArgs.Event);
}
public static IObservable<MotionEvent> MoveEvent(this View v)
{
return Observable.FromEventPattern<View.TouchEventArgs>(h => v.Touch += h, h => v.Touch -= h)
.Where(e => e.EventArgs.Event.Action == MotionEventActions.Move)
.Select(e => e.EventArgs.Event);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment