我编写了一个简单的代码,它获取加速度计和方向计并根据值显示一些图形:
package pl.aadamczyk.enginesdriver;
import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
public class EnginesDriverActivity extends Activity implements SensorEventListener
{
private SensorManager sensorManager;
private Sensor accelerometer;
private Sensor orientation;
private Panel panel;
public EnginesDriverActivity()
{
sensorManager = null;
accelerometer = null;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
orientation = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
panel = new Panel(this.getApplicationContext(), null);
setContentView(panel);
}
protected void onResume()
{
super.onResume();
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, orientation, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause()
{
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent se)
{
Sensor source = se.sensor;
if (source == accelerometer)
{
System.out.println("Updating orientation: " + se.values[1]);
panel.setOrientation(se.values);
}
else if (source == orientation)
{
System.out.println("Updating TYPE_ACCELEROMETER: " + se.values[1]);
panel.setAccelerometer(se.values);
}
panel.postInvalidate();
}
@Override
public void onAccuracyChanged(Sensor sensor, int arg1)
{
}
}
package pl.aadamczyk.enginesdriver;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class Panel extends View
{
private Paint paint;
private float[] orientation;
private float[] accelerometer;
public Panel(Context context, AttributeSet attrs)
{
super(context, attrs);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
orientation = new float[]{0, 0, 0};
accelerometer = new float[]{0, 0, 0};
}
public void setOrientation(float[] _orientation)
{
orientation = _orientation;
}
public void setAccelerometer(float[] _accelerometer)
{
accelerometer = _accelerometer;
}
@Override
protected void onDraw(Canvas canvas)
{
// Some calculations here
float y = orientation[1];
float z = orientation[2];
System.out.println(y);
// Some graphics here
}
}
问题是它将方向的值与加速度计的值混合在一起,显示出奇怪的东西。代码中有两个 System.out.println() - 第一个在 onSensorChanged 中显示良好的值,但第二个在 onDraw 显示方向或加速度计值。
这怎么可能?
感谢您的建议,并对我的英语感到抱歉。