C#开发Android移动应用系列之调用手机陀螺仪信息
By
jerrxjr1220
at 2023-08-18 • 0人收藏 • 781人看过
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Essentials; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace XamarinApp01 { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class Page1 : ContentPage { Label theLabel; public Page1() { InitializeComponent(); theLabel = new Label() { HorizontalOptions=LayoutOptions.Center }; // 检查设备是否支持陀螺仪 if (Gyroscope.IsMonitoring) { theLabel.Text = "陀螺仪不可用"; } else { Gyroscope.ReadingChanged += Gyroscope_ReadingChanged; Gyroscope.Start(SensorSpeed.UI); } Content = new StackLayout { Children = { new Label() { Text = "陀螺仪信息:", FontSize = 24 }, theLabel } }; } private void Gyroscope_ReadingChanged(object sender, GyroscopeChangedEventArgs e) { var data = e.Reading; // 获取陀螺仪的X、Y、Z轴的角速度 var x = data.AngularVelocity.X; var y = data.AngularVelocity.Y; var z = data.AngularVelocity.Z; // 更新UI Device.BeginInvokeOnMainThread(() => { theLabel.Text = $"X: {x}\nY: {y}\nZ: {z}"; }); } } }
3 个回复 | 最后更新于 2023-08-18
登录后方可回帖
获取手机定位信息(经纬度),具体地址需要结合地图Api接口进行解析