gpt4 book ai didi

Android 加速度计累积

转载 作者:太空狗 更新时间:2023-10-29 13:30:44 26 4
gpt4 key购买 nike

我在运行时连续获取加速度计值(即)X 和 Y 值。我的问题是,当加速度计的值在移动设备中发生变化时,应根据变化累积相应的值。

这是我的代码:

public class MainActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;

private float accumulation_x = 0;
private float accumulation_y = 0;
private float accumulation_z = 0;

private TextView acessTextview, angleTextview;
private float value;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById();

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);

}

public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

public void onSensorChanged(SensorEvent event) {

if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {





int count = 1;
while(count!=0){

float x = (float) 0.9887777;
float y = (float) 0.187359372;
float z = (float) 0.0228636;

float X_axis = (float) (x + (0.02724095));
float Y_axis = (float) (y + (-0.027792556));
float Z_axis = (float) (z - (0.105689));

accumulation_x = accumulation_x + X_axis;
accumulation_y = accumulation_y + Y_axis;
accumulation_z = accumulation_z + Z_axis;

value = (y / z);
float angle = (float) Math.toDegrees(Math.atan(value));
angleTextview.setText("Angle:" + angle);

acessTextview.setText("accumulation_x :" + X_axis + "\n"
+ "accumulation_y :" + Y_axis + "\n"
+ "accumulation_z :" + Z_axis);

count++;

}

}

}

private void findViewById() {
// TODO Auto-generated method stub
acessTextview = (TextView) findViewById(R.id.accessTextview);
angleTextview = (TextView) findViewById(R.id.angleTextview);
}

}

最佳答案

您的代码是正确的,只需要稍作改动,我已经用一个名为 refreshValues() 的额外方法更新了您的代码。此方法会将 X、Y 的最新值设置到 TextView 中。此方法将从 onSensorChanged() 方法调用。

public class MainActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;

private float accumulation_x = 0;
private float accumulation_y = 0;
private float accumulation_z = 0;

private TextView acessTextview, angleTextview;
private float value;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById();

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);

}

public void onAccuracyChanged(Sensor sensor, int accuracy) { }

public void onSensorChanged(SensorEvent event) {

if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

int count = 1;
while(count!=0){

float x = (float) 0.9887777;
float y = (float) 0.187359372;
float z = (float) 0.0228636;

float X_axis = (float) (x + (0.02724095));
float Y_axis = (float) (y + (-0.027792556));
float Z_axis = (float) (z - (0.105689));

accumulation_x = accumulation_x + X_axis;
accumulation_y = accumulation_y + Y_axis;
accumulation_z = accumulation_z + Z_axis;

value = (y / z);
float angle = (float) Math.toDegrees(Math.atan(value));
angleTextview.setText("Angle:" + angle);

acessTextview.setText("accumulation_x :" + X_axis + "\n"
+ "accumulation_y :" + Y_axis + "\n"
+ "accumulation_z :" + Z_axis);

count++;

}

}

refreshValues ( accumulation_x,accumulation_y );
}

private void findViewById() {
// TODO Auto-generated method stub
acessTextview = (TextView) findViewById(R.id.accessTextview);
angleTextview = (TextView) findViewById(R.id.angleTextview);
}

private void refreshValues ( float x, float y )
{
acessTextview.setText ( String.valueOf(x) );
angleTextview.setText( ( String.valueOf(y)));
}

}

关于Android 加速度计累积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15803812/

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