加速度传感器
手机的加速度传感器工作时是通过 x、y、z 三个轴的偏移来计算的
在代码基本的 API 主要集中在 Accelerometer 类型中
主要是使用该类型的对象捕获 ReadingChanged 事件监视加速度值变化的
X、Y、Z 加速度传感值
<StackPanel>
<TextBox x:Name="txtX" Header="X:"/>
<TextBox x:Name="txtY" Header="Y:"/>
<TextBox x:Name="txtZ" Header="Z:"/>
</StackPanel>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 先拿到传感器对象
Accelerometer a = Accelerometer.GetDefault();
if (a == null)
{
// 代表没有加速计传感器对象
System.Diagnostics.Debug.WriteLine("没有加速计传感器");
return;
}
a.ReadingChanged += a_ReadingChanged;
a.ReportInterval = a.MinimumReportInterval * ;
} async void a_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
{
System.Diagnostics.Debug.WriteLine(args + "改变了。。。");
// 拿到变化值
await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
txtX.Text = args.Reading.AccelerationX.ToString();
txtY.Text = args.Reading.AccelerationY.ToString();
txtZ.Text = args.Reading.AccelerationZ.ToString();
});
}
平衡检测
<Grid> <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">
<Path.RenderTransform>
<CompositeTransform x:Name="rotate" Rotation="0"/>
</Path.RenderTransform>
</Path> </Grid>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
// 先拿到传感器对象
Accelerometer a = Accelerometer.GetDefault();
if (a == null)
{
// 代表没有加速计传感器对象
System.Diagnostics.Debug.WriteLine("没有加速计传感器");
return;
}
a.ReadingChanged += a_ReadingChanged;
a.ReportInterval = a.MinimumReportInterval * ;
} async void a_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
{
System.Diagnostics.Debug.WriteLine(args + "改变了。。。");
// 拿到变化值
await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
rotate.Rotation = args.Reading.AccelerationY * ;
});
}