【问题标题】:Force compass north to portrait in samsung tablets在三星平板电脑中强制指南针北向肖像
【发布时间】:2013-01-08 10:01:32
【问题描述】:

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

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

【问题讨论】:

    标签: compass-geolocation


    【解决方案1】:

    我在 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");
    
            }
        }
    

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

    【讨论】:

      猜你喜欢
      • 2015-02-02
      • 1970-01-01
      • 2014-06-30
      • 1970-01-01
      • 2011-07-30
      • 2014-02-25
      • 2014-10-27
      • 2014-12-13
      • 2013-12-05
      相关资源
      最近更新 更多