gpt4 book ai didi

java - 需要帮助来理解加速度计代码

转载 作者:行者123 更新时间:2023-11-30 04:17:08 26 4
gpt4 key购买 nike

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

最佳答案

因此,这是注册监听器的代码(我知道你说过你已经这样做了,但它永远不会伤害):

private void enableAccelerometerListening() {
sensorManager = (SensorManager) getSystemService(COntext.SENSOR_SERVICE);
sensorManager.registerListener(sensorEventListener), sensorManager.getDefaultSensor(
Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}

private void disableAccelerometerListening() {
if (sensorManager != null) {
sesnsorManager.unregisterListener(sensorEVentListener, sensorManager.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER));
sensorManager = null;
}}

您需要在类声明下方添加几个字段:

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 {}

};

关于java - 需要帮助来理解加速度计代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9810649/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com