【问题标题】:How to make azimuth and degree less shaky in Android如何在 Android 中减少方位角和度数的抖动
【发布时间】:2015-01-25 06:09:32
【问题描述】:

首先我使用TYPE_ORIENTATION,但它已被贬值。因此,我将代码更改为更新的版本,并按照this post 中的建议使用了TYPE_ACCELEROMETERTYPE_MAGNETIC_FIELD

// Initialize android device sensor capabilities
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

然后在 onResume() 中,我使用了这个:

// For the system's orientation sensor registered listeners
mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);

以下是我使用的:

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // not in use
}

float[] mGravity;
float[] mGeomagnetic;

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
    mGravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
    mGeomagnetic = event.values;
    if (mGravity != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];
        boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
        if (success) {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            float azimuthInRadians = orientation[0];
            float azimuthInDegrees = (float) Math.toDegrees(azimuthInRadians);
            if (azimuthInDegrees < 0.0f) {
                azimuthInDegrees += 360f;
            }
            float degree = Math.round(azimuthInDegrees);
            directionHeading.setText("Heading: " + degree + " degrees");

            // Create rotation animation
            RotateAnimation ra = new RotateAnimation(currentDegree, -degree,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);

            // How long the animation will take place
            ra.setDuration(210);

            // Set the animation after the end of the reservation status
            ra.setFillAfter(true);

            // Start the animation
            image.startAnimation(ra);
            currentDegree = -degree;
        }
    }
}

旧方法相当精确,一点也不晃动。但是现在,以不折旧的方式,它摇晃得很厉害。为什么会这样?如何解决抖动问题?

【问题讨论】:

  • @HoanNguyen 等待回复,我回家后试试看。在您的答案下方的 cmets 中,有几件事,首先,有人说:“现在唯一的问题是快速移动它会跳很多”,并且您说您使用“正常”而其他人正在使用“UI”。我将它用于指南针,我希望它尽可能稳定。我也不希望它在快速移动时跳得很重。那么我应该使用什么,正常的,UI,如果快速移动它会跳转吗?
  • @HoanNguyen 我试过了,但 mFacing 是 NaN。这是为什么呢?
  • mFacing 是后置摄像头的方向,所以如果设备平放则没有摄像头方向,因此为 NaN
  • 我的手机是360的,还是不行,但是我已经找到了解决问题的办法。我必须对其应用低通滤波器。

标签: android accelerometer azimuth


【解决方案1】:

解决方案是应用 (LPF) 低通滤波器。它现在很顺利并且工作完美。 我在这里找到了解决方案:Low Pass Filter

【讨论】:

    猜你喜欢
    • 2011-12-29
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 2016-06-16
    • 2013-10-03
    • 1970-01-01
    相关资源
    最近更新 更多