TYPE_ORIENTATION을 이용해서 안드로이드 기기의 방향을 쉽게 얻을 수 있습니다.
sm = (SensorManager)this.getSystemService(Context.SENSOR_SERVICE); gyroSensor = sm.getDefaultSensor(Sensor. gyroListener = new SensorEventListener() {
@Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub float[] values = event.values; //float x= values[0]; //float y = values[1]; //float z = values[2]; }
@Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub
} };
|
그런데 Warning을 살펴보시면 아래와 같은 메세지를 확인 할 수 있습니다.
The field Sensor.TYPE_ORIENTATION is deprecated
향후에 언제 지원을 멈출지 모르는 불안한 메세지..!
방향을 얻기위해서는 TYPE_ACCELEROMETER와 TYPE_MAGNETIC_FIELD를 이용해 이벤트를 등록해야합니다.
아래는 샘플코드입니다.
import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Paint.Style; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.view.View;
public class CompassActivity extends Activity implements SensorEventListener {
Float azimut; // View to draw a compass
public class CustomDrawableView extends View { Paint paint = new Paint(); public CustomDrawableView(Context context) { super(context); paint.setColor(0xff00ff00); paint.setStyle(Style.STROKE); paint.setStrokeWidth(2); paint.setAntiAlias(true); };
protected void onDraw(Canvas canvas) { int width = getWidth(); int height = getHeight(); int centerx = width/2; int centery = height/2; canvas.drawLine(centerx, 0, centerx, height, paint); canvas.drawLine(0, centery, width, centery, paint); // Rotate the canvas with the azimut if (azimut != null) canvas.rotate(-azimut*360/(2*3.14159f), centerx, centery); paint.setColor(0xff0000ff); canvas.drawLine(centerx, -1000, centerx, +1000, paint); canvas.drawLine(-1000, centery, 1000, centery, paint); canvas.drawText("N", centerx+5, centery-10, paint); canvas.drawText("S", centerx-10, centery+15, paint); paint.setColor(0xff00ff00); } }
CustomDrawableView mCustomDrawableView; private SensorManager mSensorManager; Sensor accelerometer; Sensor magnetometer;
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mCustomDrawableView = new CustomDrawableView(this); setContentView(mCustomDrawableView); // Register the sensor listeners mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); }
protected void onResume() { super.onResume(); mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI); mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI); }
protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); }
public void onAccuracyChanged(Sensor sensor, int accuracy) { }
float[] mGravity; float[] mGeomagnetic; public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) mGravity = event.values; if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) mGeomagnetic = event.values; if (mGravity != null && mGeomagnetic != null) { float R[] = new float[9]; float I[] = new float[9]; boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic); if (success) { float orientation[] = new float[3]; SensorManager.getOrientation(R, orientation); azimut = orientation[0]; // orientation contains: azimut, pitch and roll } } mCustomDrawableView.invalidate(); } }
|
출처 : http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html
'Android' 카테고리의 다른 글
안드로이드 방향 센서를 이용한 3D 개체 회전 (0) | 2013.09.25 |
---|---|
Welcome to Android Development 창이 나오지 않을 때 (2) | 2013.09.13 |