【问题标题】:Android using RotateAnimation pointing towards a specific locationAndroid 使用 RotateAnimation 指向特定位置
【发布时间】:2017-01-11 20:11:57
【问题描述】:

我正在尝试创建一个指向特定坐标的箭头,例如指南针。 我正在使用手机中的Sensor.TYPE_ACCELEROMETERSensor.TYPE_MAGNETIC_FIELD 来计算方位角(我参考了这个问题Using orientation sensor to point towards a specific location):

       //calculate RotationMatrix
       if (gravity != null && geomag != null) {

           boolean success = SensorManager.getRotationMatrix(inR, I,
                                gravity, geomag);
              if (success) {
                SensorManager.getOrientation(inR, orientVals);
                azimuth = Math.toDegrees(orientVals[0]);
                pitch = Math.toDegrees(orientVals[1]);
                roll = Math.toDegrees(orientVals[2]);
              }
       }

        azimuth += geomagneticField.getDeclination();
        //azimuth = Math.round(sensorEvent.values[0]);
        float bearing = lastLocation.bearingTo(targetLocation);
        float angle = (float) azimuth + bearing;

然后我使用 RotatateAnimation 来创建箭头本身的旋转:

//the +90 below is because the ImageView arrow is pointing towards left by default 
RotateAnimation animation = new RotateAnimation(
                -(angle+90),
                -(angle+90),
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f);

但是,我测试了四个不同的 targetLocations,每个 targetLocations 分别在东南西北方向,箭头仅正确指向东西位置。对于南北方向的位置,箭头会旋转 180 度,这意味着它将完全指向相反的方向。如果我在旋转中添加 180 度,箭头将正确指向北方或南方的位置,但东方和西方的位置将是错误的。 这真的很令人沮丧,这对我来说完全没有意义。

如果有人能在这里帮助我,我将非常感激!提前致谢!

【问题讨论】:

    标签: android rotation location orientation compass-geolocation


    【解决方案1】:

    我做了一些进一步的测试,发现方位角和方位角的范围都是 (0...180,-180...0) 顺时针从 0 开始,而 RotateAnimation 从 0 开始...360 顺时针。因此,如果方位角和方位角小于 0,我只是在进行计算之前通过添加 360 来转换方位角和方位角。

    【讨论】:

      猜你喜欢
      • 2011-07-25
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多