Java 需要帮助理解加速计代码吗

Java 需要帮助理解加速计代码吗,java,android,Java,Android,我已经阅读了很多代码,但我不明白如何使用加速计传感器移动图像,我知道如何注册它,但我不明白如何实际使图像或形状绘制移动与加速计轴同步,我正在使用android java来实现这一点。请有人帮帮我,因为我真的很挣扎。谢谢你的时间和帮助 下面是注册侦听器的代码(我知道你说过你已经这么做了,但这不会有什么坏处): 您将需要在类声明下方有几个字段: private SesnsorManager sensorManager; private float acceleration; private floa

我已经阅读了很多代码,但我不明白如何使用加速计传感器移动图像,我知道如何注册它,但我不明白如何实际使图像或形状绘制移动与加速计轴同步,我正在使用android java来实现这一点。请有人帮帮我,因为我真的很挣扎。谢谢你的时间和帮助

下面是注册侦听器的代码(我知道你说过你已经这么做了,但这不会有什么坏处):

您将需要在类声明下方有几个字段:

private SesnsorManager sensorManager;
private float acceleration;
private float currentAcceleration;
private float lastAcceleration;
private static final int ACCELERATION_THRESHOLD = 15000;
下面是事件处理程序,它非常接近您需要的帮助:

    private SensorEventListener sensorEventListener = new SensorEventListener() {
    public void onSesnsorChanged(SensorEvent event) {

    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];

lastAcceleration = currentAcceleration; //save previous accel value

currentAcceleration = x*x + y*y + z*z;

acceleration = currentAcceleration * (currentAcceleration - lastAcceleration); // calc the change in acceleration

//if the accel is above a certain threshold:
if (acceleration > ACCELERATION_THRESHOLD) {
//MAKE YOUR CODE HERE THAT RESPONDS TO ACCELERATION EVENTS
//Note, your accel threshold should be determined by trial and error on a number of devices
}
}

public void onAccuracyChanged(Sensor sensor, int accuracy {}

};

下面是注册侦听器的代码(我知道你说过你已经这么做了,但这不会有什么坏处):

您将需要在类声明下方有几个字段:

private SesnsorManager sensorManager;
private float acceleration;
private float currentAcceleration;
private float lastAcceleration;
private static final int ACCELERATION_THRESHOLD = 15000;
下面是事件处理程序,它非常接近您需要的帮助:

    private SensorEventListener sensorEventListener = new SensorEventListener() {
    public void onSesnsorChanged(SensorEvent event) {

    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];

lastAcceleration = currentAcceleration; //save previous accel value

currentAcceleration = x*x + y*y + z*z;

acceleration = currentAcceleration * (currentAcceleration - lastAcceleration); // calc the change in acceleration

//if the accel is above a certain threshold:
if (acceleration > ACCELERATION_THRESHOLD) {
//MAKE YOUR CODE HERE THAT RESPONDS TO ACCELERATION EVENTS
//Note, your accel threshold should be determined by trial and error on a number of devices
}
}

public void onAccuracyChanged(Sensor sensor, int accuracy {}

};

此外,我将尝试解决您的一些动画需求,尽管我在这方面有很多不足之处。我想你需要做的是让你的图像随着加速计检测到的移动而移动。图像必须通过动画移动,而不是直接通过加速计移动。所以说“斑点”是你的形象,好吗?(下面的代码既添加了一个点,又设置了它的动画(虽然没有直接连接到加速计,但我希望这会有所帮助):

就在这里,我想你可以开始做一些加速计事件

正如你在我上面的其他回复中看到的

if (acceleration > ACCELERATION_THRESHOLD) {
spot.animate().x(x2).y(y2).setDuration(animationTime);

animationTime将以毫秒为单位,您认为是合适的,并且不要忘记导入必要的软件包。

此外,我将尝试满足您的一些动画需求,尽管我在这方面有很多缺点。我想您需要做的是在加速计检测到mov时使图像移动es.图像必须通过动画移动,而不是直接通过加速计移动。所以说“点”是你的图像,好吗?(下面的代码既添加了点,又设置了它的动画(没有直接连接到加速计,但我希望这会有所帮助):

就在这里,我想你可以开始做一些加速计事件

正如你在我上面的其他回复中看到的

if (acceleration > ACCELERATION_THRESHOLD) {
spot.animate().x(x2).y(y2).setDuration(animationTime);

动画时间将以毫秒为单位,您认为合适,不要忘记导入必要的软件包。

谢谢您的回复我正在查看并理解您的代码,如果有帮助,我将标记您的答案谢谢您的回复我正在查看并理解如果有帮助的话,我会记下你的答案