【发布时间】:2016-03-17 10:14:45
【问题描述】:
我实际上是在 Android 上编写一个应用程序,我需要指出一个特定的方向,解释,我有一个固定的位置和我的智能手机的位置(两个在经度纬度),我想指向一个箭头往两人之间的方向。所以我像地狱一样搜索,我有一个完美工作的指南针,我试图通过改变北方向来改变指向方向 getRotationMatrix 东西但是在失去我的大脑尝试之后我不知道该怎么做xD所以是的我需要一些想法和提示。
这是我的代码以防万一(我刚刚通过 onSensorChanged 函数,其余的只是基本初始化)
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor == mAccelerometer) {
System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
mLastAccelerometerSet = true;
} else if (event.sensor == mMagnetometer) {
System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
mLastMagnetometerSet = true;
}
if (mLastAccelerometerSet && mLastMagnetometerSet) {
SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
SensorManager.getOrientation(mR, mOrientation);
float azimuthInRadians = mOrientation[0];
float azimuthInDegress = (float)(Math.toDegrees(azimuthInRadians)+360)%360;
RotateAnimation ra = new RotateAnimation(
mCurrentDegree,
-azimuthInDegress,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
ra.setDuration(250);
ra.setFillAfter(true);
mPointer.startAnimation(ra);
mCurrentDegree = -azimuthInDegress;
}
}
【问题讨论】:
标签: android android-studio android-sensors compass magnetometer