Google Androidで傾きセンサーの値を表示する
靴を修理してもらった帰りに寄ったベローチェでひとりもくもく会。
座標系がわからない、単語もわからない。RollとPitchは見たことはあるけど、Azimuthってなんやねん。読み方は「アジムス」か「アジマス」かわからないけど、Googleの検索結果を見るに、「アジマス」っぽい。
そんなこんなで、この前購入した本とネットを参考にして、傾きと加速度センサーの値を表示するコードを書いた。
デバイスを傾けて絵を動かしたい場合は、Azimuthは必要なのか?長い道のりになりそう・・・
package com.example.android.axissensor; import java.util.List; import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.widget.TextView; public class AxisSensor extends Activity implements SensorEventListener { private Sensor orientationSensor; private Sensor accelerometerSensor; private float[] orientationValues = new float[3]; private float[] accelerometerValues = new float[3]; private TextView orientationText; private TextView accelerometerText; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE); List<Sensor> sensorList; sensorList = sm.getSensorList(Sensor.TYPE_ORIENTATION); orientationSensor = sensorList.get(0); sensorList = sm.getSensorList(Sensor.TYPE_ACCELEROMETER); accelerometerSensor = sensorList.get(0); sm.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL); sm.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL); orientationText = (TextView)findViewById(R.id.sensor_orientation_text); accelerometerText = (TextView)findViewById(R.id.sensor_accelerometer_text); } public void onSensorChanged(SensorEvent event) { if (event.sensor == orientationSensor) { orientationValues = event.values; orientationText.setText( "SENSOR_ORIENTATION:\n"+ " Azimuth:"+(int)orientationValues[0]+"\n"+ " Pitch :"+(int)orientationValues[1]+"\n"+ " Roll :"+(int)orientationValues[2]+"\n"); orientationText.invalidate(); } else if (event.sensor == accelerometerSensor) { accelerometerValues = event.values; accelerometerText.setText( "SENSOR_ACCELEROMETER:\n"+ " X:"+(int)accelerometerValues[0]+"\n"+ " Y:"+(int)accelerometerValues[1]+"\n"+ " Z:"+(int)accelerometerValues[2]+"\n"); accelerometerText.invalidate(); } } public void onAccuracyChanged(Sensor sensor, int accuracy) { } }