gpt4 book ai didi

android - 是否可以在 Activity 环境之外使用加速度计?

转载 作者:行者123 更新时间:2023-11-29 21:43:42 24 4
gpt4 key购买 nike

嗨,我是新来的(我只编程了几个月,所以我的把握不稳),正在尝试创建一个游戏,让玩家的头像随着加速度计移动,他们必须躲避屏幕上的东西(从上到下的角度)。我已经创建了一个 gameScreen 类来绘制头像、背景和敌人,我正在尝试为加速度计创建一个类来告诉 gameScreen 将头像移动到哪里。目前我的类包含用于扩展 AndroidGame 框架的加速度计的代码,但由于它不是 Activity 并且没有 onCreate,我不确定如何设置加速度计。有没有办法做到这一点,还是我只是在做这一切都错了?

这是我目前所拥有的(显然是不完整的)。

import edu.austincc.testlearnand.SplashLoadingScreen;
import edu.austincc.framework.Screen;
import edu.austincc.testlearnand.Assets;
import edu.austincc.framework.implementation.AndroidGame;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class BuffaloStampede extends AndroidGame implements SensorEventListener {
TextView textView;
SensorManager manager;
double[] gravity = new double[3];
public double[] linear_acceleration = new double[3];

boolean firstTimeCreate = true;

@Override
public Screen getInitScreen() {

if (firstTimeCreate) {
Assets.loadBS(this);
firstTimeCreate = false;
}

return new SplashLoadingScreen(this);

}

@Override
public void onBackPressed() {
getCurrentScreen().backButton();
}


@Override
public void onResume() {
super.onResume();

Assets.theme.play();

}

@Override
public void onPause() {
super.onPause();
Assets.theme.pause();
manager.unregisterListener(this);

}







public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

textView = new TextView(this);

manager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0) {
textView.setText("No accelerometer installed");
} else {
Sensor accelerometer = manager.getSensorList(
Sensor.TYPE_ACCELEROMETER).get(0);
if (!manager.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_NORMAL)) {
textView.setText("Couldn't register accelerometer listener");
}
}
setContentView(textView);
}

@Override
public void onSensorChanged(SensorEvent event) {
// alpha is calculated as t / (t + dT)
// with t, the low-pass filter's time-constant
// and dT, the event delivery rate

final double alpha = 0.8;

gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

linear_acceleration[0] = event.values[0] - gravity[0];
linear_acceleration[1] = event.values[1] - gravity[1];
linear_acceleration[2] = event.values[2] - gravity[2];

}

public double[] getGravity() {
return gravity;
}

public double[] getLinear_acceleration() {
return linear_acceleration;
}

public void setGravity(double[] gravity) {
this.gravity = gravity;
}

public void setLinear_acceleration(double[] linear_acceleration) {
this.linear_acceleration = linear_acceleration;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// nothing to do here
}
}

最佳答案

在您的加速度计类中创建一个带有 Context 参数的构造函数,并使用此参数实例化一个 SensorManager。

public MyAccelerometerClass implements SensorEventListener
{
private SensorManager sensorManager;

public MyAccelerometerClass(Context context)
{
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
}

// codes
}

关于android - 是否可以在 Activity 环境之外使用加速度计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16429821/

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