在Android设备中具备了很多新的特性,比如各种感应器,GL图形库支持。在大部分Android设备中都有磁力感应器,相对于重力感应而言它可以感应出方向,今天Android123就以Google的Samples的指南针例子和大家说下OpenGL的指南针的实现。
public class CompassActivity extends Activity implements Renderer, SensorEventListener {
private GLSurfaceView mGLSurfaceView; //GL View
private SensorManager mSensorManager;
private float[] mGData = new float[3];
private float[] mMData = new float[3];
private float[] mR = new float[16];
private float[] mI = new float[16];
private FloatBuffer mVertexBuffer;
private FloatBuffer mColorBuffer;
private ByteBuffer mIndexBuffer;
private float[] mOrientation = new float[3];
private int mCount;
public CompassActivity() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mGLSurfaceView = new GLSurfaceView(this);
mGLSurfaceView.setRenderer(this);
setContentView(mGLSurfaceView);
}
@Override
protected void onResume() { //Activity切换到前端时触发
super.onResume();
mGLSurfaceView.onResume();
Sensor gsensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //加速感应器
Sensor msensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); //磁力感应器
mSensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onPause() { //Activity切换到后台时触发
super.onPause();
mGLSurfaceView.onPause();
mSensorManager.unregisterListener(this); //主要是为了省电
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -2);
gl.glMultMatrixf(mR, 0);
gl.glVertexPointer(3, GL_FLOAT, 0, mVertexBuffer);
gl.glColorPointer(4, GL_FLOAT, 0, mColorBuffer);
gl.glDrawElements(GL_LINES, 6, GL_UNSIGNED_BYTE, mIndexBuffer);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glClearColor(1,1,1,1);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
float vertices[] = {
0,0,0,
1,0,0,
0,1,0,
0,0,1
};
float colors[] = {
0,0,0,0,
1,0,0,1,
0,1,0,1,
0,0,1,1
};
byte indices[] = { 0, 1, 0, 2, 0, 3 };
ByteBuffer vbb;
vbb = ByteBuffer.allocateDirect(vertices.length*4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
vbb = ByteBuffer.allocateDirect(colors.length*4);
vbb.order(ByteOrder.nativeOrder());
mColorBuffer = vbb.asFloatBuffer();
mColorBuffer.put(colors);
mColorBuffer.position(0);
mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
mIndexBuffer.put(indices);
mIndexBuffer.position(0);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) { //实现SensorEventListener 接口
int type = event.sensor.getType();
float[] data;
if (type == Sensor.TYPE_ACCELEROMETER) {
data = mGData;
} else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
data = mMData;
} else {
return;
}
for (int i=0 ; i<3 ; i++)
data[i] = event.values[i];
SensorManager.getRotationMatrix(mR, mI, mGData, mMData);
SensorManager.getOrientation(mR, mOrientation);
float incl = SensorManager.getInclination(mI);
if (mCount++ > 50) {
final float rad2deg = (float)(180.0f/Math.PI);
mCount = 0;
Log.d("Compass", "yaw: " + (int)(mOrientation[0]*rad2deg) +
" pitch: " + (int)(mOrientation[1]*rad2deg) +
" roll: " + (int)(mOrientation[2]*rad2deg) +
" incl: " + (int)(incl*rad2deg)
);
}
}
}
RSS