【问题标题】:how to move ellipse using an Accelerometer in xamarin如何在 xamarin 中使用加速度计移动椭圆
【发布时间】:2021-05-18 03:04:32
【问题描述】:

我想在手机上使用加速度计移动一个非常基本的椭圆或图像

下面我已经设置了 xamarin Essentials Acceleromter,但椭圆没有移动并且 Z 坐标丢失。有谁知道我做错了什么?

查看

<StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
    <Ellipse x:Name="ballEllipse" Fill="Green" WidthRequest="50" HeightRequest="50"/>
</StackLayout>

查看后端

public partial class AccelerometerPage2 : ContentPage
{
    public AccelerometerPage2()
    {
        InitializeComponent();

        if (Accelerometer.IsMonitoring)
            return;

        // Register and Staart Accelermeter
        Accelerometer.ReadingChanged += Accelerometer_ReadingChanged;
        Accelerometer.Start(SensorSpeed.UI);
    }

    void Accelerometer_ReadingChanged(object sender, AccelerometerChangedEventArgs e)
    {
        await ballEllipse.TranslateTo(e.Reading.Acceleration.X, 
            e.Reading.Acceleration.Y, 1000);
    }
}

【问题讨论】:

    标签: c# android xamarin xamarin.forms xamarin.android


    【解决方案1】:

    其实应该可以的,因为Acceleration.X,Acceleration.Y的值太小了,Ellipse看起来不像在移动,你可以尝试扩大值看看它在移动。另外,这里没有捕获 Z 坐标,所以可以尝试打印它的值。

    async void Accelerometer_ReadingChanged(object sender, AccelerometerChangedEventArgs e)
        {
            var data = e.Reading;
            Console.WriteLine($"Reading: X: {data.Acceleration.X}, Y: {data.Acceleration.Y}, Z: {data.Acceleration.Z}");
            await ballEllipse.TranslateTo(e.Reading.Acceleration.X*100,
                e.Reading.Acceleration.Y*100, 1000);
        }
    

    【讨论】:

    • 哇,太酷了!你的权利它正在工作,但 x 和 y 的值太小(在 -1 到 1 之间)......所以通过 100 来计时会产生很大的不同。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多