Xamarin.Androidãæ¹ä½ã®åå¾ï¼ï¼ç£æ°ã»ã³ãµã¼/å é度ã»ã³ãµã¼ï¼
1 æ¹ä½ã®åå¾æ¹æ³
Androidã§æ¹ä½ãåå¾ããæ¹æ³ã¯ã次ã®ï¼ã¤ãããã¾ãã
(1) TYPE_ORIENTATION(傾きセンサー)による方法
(2) TYPE_MAGNETIC_FIELD(ç£æ°ã»ã³ãµã¼)ã¨TYPE_ACCELEROMETER(å é度ã»ã³ãµã¼)ã«ããæ¹æ³
ä»åã¯ã(2)ã®ç£æ°ã»ã³ãµã¼ã¨å é度ã»ã³ãµã¼ã使ç¨ãã¦ã¿ã¾ãã
ãªããAPI Level8 ã§ãæ¹ä½è§ãåã³ãå¾ãããæ±ããå ´åã¯ãTYPE_ORIENTATIONã¯éæ¨å¥¨ã«ãªã£ã¦ããããã§ãã
2 ç£æ°ã»ã³ãµã¼(TYPE_MAGNETIC_FIELD)åã³å é度ã»ã³ãµã¼(TYPE_ACCELEROMETER)
ã»ã³ãµã¼èªä½ã®ä½¿ç¨æ¹æ³ã¯ã傾きセンサーã®æã¨åãã§ããããæ¹ä½è§ãã¨ãå¾ããã¯ãSensorManagerã®ã¡ã½ããã§è¨ç®ãã¦ãã¾ããhttp://developer.android.com/reference/android/hardware/SensorManager.html
// å é度ã»ã³ãµã¼ã¨ç£æ°ã»ã³ãµã¼ã®å¤ããå転è¡åãæ±ãã public static bool GetRotationMatrix(float[] R, float[] I, float[] gravity, float[] geomagnetic) // 端æ«ã®ç»é¢è¨å®ã«åãããå¤æè¡åãæ±ãã(以ä¸ã¯, 縦表示ã§ç»é¢ãä¸ã«ããå ´å) public static bool RemapCoordinateSystem(float[] inR, Android.Hardware.Axis X, Android.Hardware.Axis Y, float[] outR) // æ¹ä½è§åã³å¾ããæ±ãã public static float[] GetOrientation(float[] R, float[] values)
ãµã³ãã«ã¯ãå¾ãã»ã³ãµã¼ã®å¤ãåããã¦è¡¨ç¤ºãã¦éããæ¯ã¹ã¦è¦ã¾ããããã»ã¨ãã©åãã§ããã
â»å転è§ã®ç¬¦å·ãé転ãã¦ããã®ã«æ³¨æãå¿
è¦ã§ãã
[Activity(Label = "AndroidApplication1", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity, ISensorEventListener{ private SensorManager _senseManager; private TextView _textView; protected override void OnCreate(Bundle bundle){ base.OnCreate(bundle); SetContentView(Resource.Layout.Main); _senseManager = (SensorManager) GetSystemService(SensorService); _textView = FindViewById<TextView>(Resource.Id.textView); } protected override void OnResume(){ base.OnResume(); //ãªã¹ãã¼ç»é² _senseManager.RegisterListener(this, _senseManager.GetDefaultSensor(SensorType.MagneticField),SensorDelay.Ui); _senseManager.RegisterListener(this, _senseManager.GetDefaultSensor(SensorType.Accelerometer),SensorDelay.Ui); _senseManager.RegisterListener(this, _senseManager.GetDefaultSensor(SensorType.Orientation), SensorDelay.Ui); } protected override void OnPause(){ base.OnPause(); //ãªã¹ãã¼è§£é¤ _senseManager.UnregisterListener(this); } //ISensorEventListenerã§å¿ é public void OnAccuracyChanged(Sensor sensor, SensorStatus accuracy){ } float [] _aVal; float [] _mVal; float[] _oVal; public void OnSensorChanged(SensorEvent e) { //ã»ã³ãµã¼å¤ã®åå¾ switch (e.Sensor.Type){ case SensorType.Accelerometer: //å é度ã»ã³ãµã¼ _aVal = new float[3]; _aVal = e.Values.ToArray(); break; case SensorType.MagneticField: //ç£æ°ã»ã³ãµã¼ _mVal = new float[3]; _mVal = e.Values.ToArray(); break; case SensorType.Orientation: //å¾ãã»ã³ãµã¼ _oVal = new float[3]; _oVal = e.Values.ToArray(); break; } var sb = new System.Text.StringBuilder(); if (_oVal != null) { sb.Append(String.Format("å¾ãã»ã³ãµã¼\n")); sb.Append(String.Format("æ¹ä½è§:{0:F1}\n", _oVal[0])); sb.Append(String.Format("å¾æè§:{0:F1}\n", _oVal[1])); sb.Append(String.Format("å転è§:{0:F1}\n", _oVal[2])); sb.Append(String.Format("\n")); } if (_aVal != null && _mVal != null) { var R1 = new float[16]; var R2= new float[16]; var I = new float[16]; var val = new float[3]; // å é度ã»ã³ãµã¼ã¨ç£æ°ã»ã³ãµã¼ã®å¤ããå転è¡åãæ±ãã SensorManager.GetRotationMatrix(R1, I, _aVal, _mVal); // 端æ«ã®ç»é¢è¨å®ã«åãããå¤æè¡åãæ±ãã(以ä¸ã¯, 縦表示ã§ç»é¢ãä¸ã«ããå ´å) SensorManager.RemapCoordinateSystem(R1, Axis.X, Axis.Y, R2); // æ¹ä½è§åã³å¾ããæ±ãã SensorManager.GetOrientation(R2, val); //ã©ã¸ã¢ã³ãè§åº¦ã«å¤æ for (var i = 0; i < 3; i++) { val[i] = (float)(val[i] * 180 / Math.PI); } sb.Append(String.Format("ç£æ°ã»ã³ãµã¼ + å é度ã»ã³ãµã¼\n")); sb.Append(String.Format("æ¹ä½è§:{0:F1}\n", (val[0] < 0) ? val[0] + 360 : val[0])); sb.Append(String.Format("å¾æè§:{0:F1}\n", val[1])); sb.Append(String.Format("å転è§:{0:F1}\n", val[2])); } //表示 _textView.Text = sb.ToString(); } }