加速度传感器
手机的加速度传感器工作时是通过 x、y、z 三个轴的偏移来计算的
在代码基本的 API 主要集中在 Accelerometer 类型中
主要是使用该类型的对象捕获 ReadingChanged 事件监视加速度值变化的
X、Y、Z 加速度传感值
1 <StackPanel> 2 <TextBox x:Name="txtX" Header="X:"/> 3 <TextBox x:Name="txtY" Header="Y:"/> 4 <TextBox x:Name="txtZ" Header="Z:"/> 5 </StackPanel>
1 protected override void OnNavigatedTo(NavigationEventArgs e) 2 { 3 // 先拿到传感器对象 4 Accelerometer a = Accelerometer.GetDefault(); 5 if (a == null) 6 { 7 // 代表没有加速计传感器对象 8 System.Diagnostics.Debug.WriteLine("没有加速计传感器"); 9 return; 10 } 11 a.ReadingChanged += a_ReadingChanged; 12 a.ReportInterval = a.MinimumReportInterval * 5; 13 } 14 15 async void a_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args) 16 { 17 System.Diagnostics.Debug.WriteLine(args + "改变了。。。"); 18 // 拿到变化值 19 await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => 20 { 21 txtX.Text = args.Reading.AccelerationX.ToString(); 22 txtY.Text = args.Reading.AccelerationY.ToString(); 23 txtZ.Text = args.Reading.AccelerationZ.ToString(); 24 }); 25 }
平衡检测
1 <Grid> 2 3 <Path Data="M180.348,341.493 L144.279,392.488 L162.935,391.244 L161.692,630.05 L200.249,630.05 L196.517,392.488 L216.418,393.731 z" Fill="#FF6AEA00" HorizontalAlignment="Center" Height="289.557" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" VerticalAlignment="Bottom" Width="73.139" RenderTransformOrigin="0.5,1"> 4 <Path.RenderTransform> 5 <CompositeTransform x:Name="rotate" Rotation="0"/> 6 </Path.RenderTransform> 7 </Path> 8 9 </Grid>
1 protected override void OnNavigatedTo(NavigationEventArgs e) 2 { 3 DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape; 4 // 先拿到传感器对象 5 Accelerometer a = Accelerometer.GetDefault(); 6 if (a == null) 7 { 8 // 代表没有加速计传感器对象 9 System.Diagnostics.Debug.WriteLine("没有加速计传感器"); 10 return; 11 } 12 a.ReadingChanged += a_ReadingChanged; 13 a.ReportInterval = a.MinimumReportInterval * 10; 14 } 15 16 async void a_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args) 17 { 18 System.Diagnostics.Debug.WriteLine(args + "改变了。。。"); 19 // 拿到变化值 20 await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => 21 { 22 rotate.Rotation = args.Reading.AccelerationY * 90; 23 }); 24 }
时间: 2024-10-11 04:50:58