1

我已经实现了一个只能在纵向模式下工作的应用程序。该应用程序有一个指南针,我们知道三星平板电脑(例如:samsung Galaxy Tab 2)和移动设备中的指南针工作方式不同,一个是横向的北(0º),另一个是纵向的。

我们需要全部都处于纵向模式,换句话说,我们希望 NORTH (0º) 在 PORTRAIT 中。有人有解决方案吗?

4

1 回答 1

0

我在 Galaxy Note 上遇到了同样的问题,并遵循了有关如何确定平板电脑或手机的建议。这是另一个帖子,但不记得链接了。代码如下所示:

DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
    int screenHeight = metrics.heightPixels;
    double inches = Math.sqrt((metrics.widthPixels * metrics.widthPixels)
            + (metrics.heightPixels * metrics.heightPixels))
            / metrics.densityDpi;
    // tablets typically larger than 6"" diagonally
    // this is attempt to orient both player and magnetic sensors
    if (inches > 6) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    } else
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

完成此操作后,我翻译了 sensorlistener 代码中的方位角设置:

        public void onSensorChanged(SensorEvent event) {
        // Note: Not all sensor events will supply both accelerometer and
        // mag-field
        int s = event.sensor.getType();
        switch (s) {
        case Sensor.TYPE_ACCELEROMETER:
            aValues = lowPass(event.values.clone(), aValues);
            break;
        case Sensor.TYPE_MAGNETIC_FIELD:

            mValues = lowPass(event.values.clone(), mValues);
            break;
        }

        float[] R = new float[16]; // Rotation Matrix result goes here
        float[] I = new float[9]; // Inclination Matrix result goes here
        float[] oValues = new float[3]; // used for new orientation

        boolean success = SensorManager.getRotationMatrix(R, I, aValues,
                mValues);
        // if both aValues and mValues are not null success will be true
        // Also returns the Inclination Matrix

        if (success) {
            // Now get the device's orientation from the Rotation and
            // Inclination Matrices
            SensorManager.getOrientation(R, oValues);

            // Change Radians to Degrees
            oValues[0] = (360 + (float) Math.toDegrees(oValues[0])) % 360;
            oValues[1] = (float) Math.toDegrees(oValues[1]);
            oValues[2] = (float) Math.toDegrees(oValues[2]);
            azimuth = oValues[0];//
            y_az = oValues[1];// orientationValues[1];
            z_az = oValues[2];// =

            // test portrait or landscape (different on Tablets)
            int test = getResources().getConfiguration().orientation;
          // if necessary, azimuth = azimuth+270f  or + 90f %360
            heading.setText("X:" + Math.round(azimuth) + " Y:"
                    + Math.round(y_az) + " Z: " + Math.round(z_az)
                    + " degrees");

        }
    }

我最终让平板电脑保持横向模式,因为大多数用户会以这种方式定位它,而北方位于景观的顶部。我希望我的菜鸟代码(在其他人的大量帮助下)可以帮助你。

于 2013-07-25T21:09:49.667 回答