/// <summary> /// 加速度センサーの値を取得 /// </summary> private void StartAccelerometerCapture() { motion.Start(MotionSensorType.Accelerometer, MotionSensorDelay.Default); if (motion.IsActive(MotionSensorType.Accelerometer)) { motion.SensorValueChanged += (object s, SensorValueChangedEventArgs e) => { if (!capturingAccelerometer) { return; } if (!e.SensorType.Equals(MotionSensorType.Accelerometer)) { return; } var value = (MotionVector)e.Value; Debug.WriteLine($"accelerometer => X:{value.X} Y:{value.Y} Z:{value.Z}"); // Formに描画する Device.BeginInvokeOnMainThread(() => { AccelerometerValue.Text = $"X:{value.X} Y:{value.Y} Z:{value.Z}"; }); }; } }
private void StartClicked(object sender, EventArgs e) { gyroData = new ObservableCollection <double>(); motion.Start(MotionSensorType.Gyroscope, MotionSensorDelay.Default); if (motion.IsActive(MotionSensorType.Gyroscope)) { motion.SensorValueChanged += (object s, SensorValueChangedEventArgs v) => { // 乱暴ですが、Gyroscopeの各絶対値が4を超えた回数が4回以上(2回シェイク)でシェイクと判定しています。 gyroData.Add(((MotionVector)v.Value).X); gyroData.Add(((MotionVector)v.Value).Y); gyroData.Add(((MotionVector)v.Value).Z); var shake = gyroData.Where(gyroData => Math.Abs(gyroData) > 4).Count(); if (shake > 3) { motion.Stop(MotionSensorType.Gyroscope); Device.BeginInvokeOnMainThread(async() => { await DisplayAlert("Gyroscope", "Shaked!", "OK"); }); } Device.BeginInvokeOnMainThread(() => { xLabel.Text = ((MotionVector)v.Value).X.ToString("0.0000"); yLabel.Text = ((MotionVector)v.Value).Y.ToString("0.0000"); zLabel.Text = ((MotionVector)v.Value).Z.ToString("0.0000"); }); }; } }
public MainPageCS() { xLabel = new Label { Text = "x = " }; yLabel = new Label { Text = "y = " }; zLabel = new Label { Text = "z = " }; startButton = new Button { Text = "Start" }; startButton.Clicked += (_, __) => { motion.Start(MotionSensorType.Accelerometer, MotionSensorDelay.Default); if (motion.IsActive(MotionSensorType.Accelerometer)) { motion.SensorValueChanged += (object sender, SensorValueChangedEventArgs e) => { System.Diagnostics.Debug.WriteLine("X:{0}, Y:{1}, Z:{2}", ((MotionVector)e.Value).X, ((MotionVector)e.Value).Y, ((MotionVector)e.Value).Z); Device.BeginInvokeOnMainThread(() => { xLabel.Text = ((MotionVector)e.Value).X.ToString("0.0000"); yLabel.Text = ((MotionVector)e.Value).Y.ToString("0.0000"); zLabel.Text = ((MotionVector)e.Value).Z.ToString("0.0000"); }); }; } }; stopButton = new Button { Text = "Stop" }; stopButton.Clicked += (_, __) => { motion.Stop(MotionSensorType.Accelerometer); }; Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 0); Content = new StackLayout { Children = { xLabel, yLabel, zLabel, startButton, stopButton } }; }
public MainPageCS() { angleLabel = new Label { Text = "Angle", FontSize = 40, HorizontalTextAlignment = TextAlignment.Center, }; startButton = new Button { Text = "Start" }; startButton.Clicked += (sender, e) => { motion.Start(MotionSensorType.Compass, MotionSensorDelay.Default); if (motion.IsActive(MotionSensorType.Compass)) { motion.SensorValueChanged += (object s, SensorValueChangedEventArgs a) => { Device.BeginInvokeOnMainThread(() => { angleLabel.Text = string.Format($"{a.Value.Value:N0}"); }); }; } }; stopButton = new Button { Text = "Stop" }; stopButton.Clicked += (sender, e) => { motion.Stop(MotionSensorType.Compass); }; Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 0); Content = new StackLayout { Children = { new Frame { Padding = 30, Content = angleLabel, }, startButton, stopButton } }; }
private void StartClicked(object sender, EventArgs e) { motion.Start(MotionSensorType.Compass, MotionSensorDelay.Default); if (motion.IsActive(MotionSensorType.Compass)) { motion.SensorValueChanged += (object s, SensorValueChangedEventArgs a) => { Device.BeginInvokeOnMainThread(() => { angleLabel.Text = string.Format($"{a.Value.Value:N0}°"); }); }; } }
public MainPageXaml() { InitializeComponent(); SizeChanged += MainPageXaml_SizeChanged; //Viewのサイズが決まった時のイベント // Motion処理 motion.Start(MotionSensorType.Accelerometer, MotionSensorDelay.Ui); if (motion.IsActive(MotionSensorType.Accelerometer)) { // 現在の位置を取得 nowX = SensorText.X; nowY = SensorText.Y; motion.SensorValueChanged += (object sender, SensorValueChangedEventArgs e) => { Device.BeginInvokeOnMainThread(() => { SensorText.Text = string.Format("X: {0:N4}\nY: {1:N4}", ((MotionVector)e.Value).X, ((MotionVector)e.Value).Y); }); if (Device.OS == TargetPlatform.Android) { moveX = ((MotionVector)e.Value).X * -10; moveY = ((MotionVector)e.Value).Y * 10; } else { moveX = ((MotionVector)e.Value).X * 10; moveY = ((MotionVector)e.Value).Y * -10; } //Debug.WriteLine($"layoutW: {layoutW}, layoutH: {layoutH}"); //Debug.WriteLine($"nowX: {nowX}, nowY: {nowY}"); //Debug.WriteLine($"moveX: {moveX}, moveY: {moveY}"); Device.BeginInvokeOnMainThread(() => { if (nowX + moveX > ((layoutW - textW) / 2) * -1 && nowX + moveX < (layoutW - textW) / 2 && nowY + moveY > ((layoutH - textH) / 2) * -1 && nowY + moveY < (layoutH - textH) / 2) { SensorText.TranslateTo(nowX + moveX, nowY + moveY); nowX = nowX + moveX; nowY = nowY + moveY; } }); }; } }
private void StartClicked(object sender, EventArgs e) { motion.Start(MotionSensorType.Magnetometer, MotionSensorDelay.Default); if (motion.IsActive(MotionSensorType.Magnetometer)) { motion.SensorValueChanged += (object s, SensorValueChangedEventArgs a) => { Device.BeginInvokeOnMainThread(() => { xLabel.Text = ((MotionVector)a.Value).X.ToString("0.0000"); yLabel.Text = ((MotionVector)a.Value).Y.ToString("0.0000"); zLabel.Text = ((MotionVector)a.Value).Z.ToString("0.0000"); }); }; } }
private void StartClicked(object sender, EventArgs e) { motion.Start(MotionSensorType.Accelerometer, MotionSensorDelay.Default); if (motion.IsActive(MotionSensorType.Accelerometer)) { motion.SensorValueChanged += (object s, SensorValueChangedEventArgs a) => { System.Diagnostics.Debug.WriteLine("X:{0}, Y:{1}, Z:{2}", ((MotionVector)a.Value).X, ((MotionVector)a.Value).Y, ((MotionVector)a.Value).Z); Device.BeginInvokeOnMainThread(() => { xLabel.Text = ((MotionVector)a.Value).X.ToString("0.0000"); yLabel.Text = ((MotionVector)a.Value).Y.ToString("0.0000"); zLabel.Text = ((MotionVector)a.Value).Z.ToString("0.0000"); }); }; } }
public MainPageCS() { abs = new AbsoluteLayout { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, }; SensorText = new Label { HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Center, BackgroundColor = Color.Silver, FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), Text = "test" }; abs.Children.Add(SensorText); Content = abs; SizeChanged += MainPageCS_SizeChanged; // Motion処理 motion.Start(MotionSensorType.Accelerometer, MotionSensorDelay.Ui); if (motion.IsActive(MotionSensorType.Accelerometer)) { // 現在の位置を取得 nowX = SensorText.X; nowY = SensorText.Y; motion.SensorValueChanged += (object sender, SensorValueChangedEventArgs e) => { Device.BeginInvokeOnMainThread(() => { SensorText.Text = string.Format("X: {0:N4}\nY: {1:N4}", ((MotionVector)e.Value).X, ((MotionVector)e.Value).Y); }); if (Device.OS == TargetPlatform.Android) { moveX = ((MotionVector)e.Value).X * -10; moveY = ((MotionVector)e.Value).Y * 10; } else { moveX = ((MotionVector)e.Value).X * 10; moveY = ((MotionVector)e.Value).Y * -10; } //Debug.WriteLine($"layoutW: {layoutW}, layoutH: {layoutH}"); //Debug.WriteLine($"nowX: {nowX}, nowY: {nowY}"); //Debug.WriteLine($"moveX: {moveX}, moveY: {moveY}"); Device.BeginInvokeOnMainThread(() => { if (nowX + moveX > ((layoutW - textW) / 2) * -1 && nowX + moveX < (layoutW - textW) / 2) { SensorText.TranslationX = nowX + moveX; nowX = nowX + moveX; } if (nowY + moveY > ((layoutH - textH) / 2) * -1 && nowY + moveY < (layoutH - textH) / 2) { SensorText.TranslationY = nowY + moveY; nowY = nowY + moveY; } }); }; } }
private void RegisterAccelerometer(IDeviceMotion deviceMotion) { deviceMotion.Start(MotionSensorType.Accelerometer, MotionSensorDelay.Ui); deviceMotion.SensorValueChanged += DeviceMotionOnSensorValueChanged; }
public MainPageCS() { xLabel = new Label { Text = "x = " }; yLabel = new Label { Text = "y = " }; zLabel = new Label { Text = "z = " }; startButton = new Button { Text = "Start" }; startButton.Clicked += (_, __) => { gyroData = new ObservableCollection <double>(); motion.Start(MotionSensorType.Gyroscope, MotionSensorDelay.Default); if (motion.IsActive(MotionSensorType.Gyroscope)) { motion.SensorValueChanged += (object sender, SensorValueChangedEventArgs e) => { // 乱暴ですが、Gyroscopeの各絶対値が4を超えた回数が4回以上(2回シェイク)でシェイクと判定しています。 gyroData.Add(((MotionVector)e.Value).X); gyroData.Add(((MotionVector)e.Value).Y); gyroData.Add(((MotionVector)e.Value).Z); var shake = gyroData.Where(gyroData => Math.Abs(gyroData) > 4).Count(); if (shake > 3) { motion.Stop(MotionSensorType.Gyroscope); Device.BeginInvokeOnMainThread(async() => { await DisplayAlert("Gyroscope", "Shaked!", "OK"); }); } Device.BeginInvokeOnMainThread(() => { xLabel.Text = ((MotionVector)e.Value).X.ToString("0.0000"); yLabel.Text = ((MotionVector)e.Value).Y.ToString("0.0000"); zLabel.Text = ((MotionVector)e.Value).Z.ToString("0.0000"); }); }; } }; stopButton = new Button { Text = "Stop" }; stopButton.Clicked += (_, __) => { motion.Stop(MotionSensorType.Gyroscope); }; Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 0); Content = new StackLayout { Children = { xLabel, yLabel, zLabel, startButton, stopButton, } }; }