標籤:
加速度感應器
手機的加速度感應器工作時是通過 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 }
Windows Phone 十八、加速計