안드로이드 센서값을 읽어서 회전정도를 구하는게 필요해서 방법을 찾았는데 검색해서 나오는 것들 대부분이 구버전이라 4.4에서 쓸려고 하니 depress 뜨길래 요즘꺼(?)에 맞는 방법으로 찾아서 쓴거 정리 해둠
public class Activity_Main extends Activity implements SensorEventListener{ private SensorManager mSensorManager; private Sensor accelerometer; private Sensor magnetometer; private float[] mGravity = null; private float[] mGeomagnetic= null; private float R[] = new float[9]; private float I[] = new float[9]; //----------------------------------------------------------- @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI); mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override 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) { boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic); if (success) { float orientation[] = new float[3]; SensorManager.getOrientation(R, orientation); double degreeY = Math.toDegrees(orientation[2]); } } } }
답글 남기기