そのため、Android Compass のこのバグの修正に 2 日間取り組んできました。基本的に、 Xamarin.Android を使用して Qibla Compass を構築しようとしています。最初のステップは、北を指すコンパスを作成することです。
コンパスが適切な North を表示するように、このチュートリアルの Java コードを翻訳しました 。
I am simply rotating my Relative Layout which contains my Compass Image whenever Sensor reading Changes.
直感でコードを微調整してみましたが、この単純な Compass は 1 つの場所に留まるのが好きではありません。コンパス画像の北を磁北に合わせようとしています。
しかし、私のコンパスは決して正しい北を示さず、北は変化し続けます。
デバイス Compass が実際に機能することを確認するために、Playstore からアプリをテストしたところ、非常にスムーズに動作しました。
マイ アクティビティ :
public class QiblaDirectionActivity : Activity, ISensorEventListener
//all the right inherits done
私のデザインコードスニペット:
<RelativeLayout
android:layout_width="wrap_content"
android:id="@+id/qiblaRL"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/qiblaCompass"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:layout_centerInParent="true"
android:src="@drawable/compass" />
<ImageView
android:id="@+id/qiblaArrow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:layout_centerInParent="true"
android:src="@drawable/needle" />
</RelativeLayout>
私のバックエンド:
float currentCompassDegree = 0f;
public void OnSensorChanged(SensorEvent e)
{
//I have checked the accuracy its high
RotateAnimation ra = new RotateAnimation(currentCompassDegree,-e.Values[0], Dimension.RelativeToSelf, 0.5f, Dimension.RelativeToSelf, 0.5f);
ra.Duration = 120;
ra.FillAfter = true;
qiblaRL.StartAnimation(ra);
currentCompassDegree = -e.Values[0];
}
ここでリスナーを登録します:
protected override void OnResume()
{
base.OnResume();
if (_sensorManager != null)
{
var mF = _sensorManager.GetDefaultSensor(SensorType.MagneticField);
_sensorManager.RegisterListener(this, mF, SensorDelay.Ui);
qiblaAdvice.Text= "(Align Device Properly)";
}
else
{
qiblaAdvice.Text = "(Device doesn't support Compass)";
}
}
そして、私はここでそれを登録解除します:
protected override void OnPause()
{
base.OnPause();
if (_sensorManager != null)
{
_sensorManager.UnregisterListener(this);
}
}