23

多窗口文档

多窗口模式下禁用的功能

当设备处于多窗口模式时,某些功能会被禁用或忽略,因为它们对于可能与其他活动或应用程序共享设备屏幕的活动没有意义。这些功能包括:

  • 某些系统 UI 自定义选项被禁用;例如,如果应用程序未在全屏模式下运行,它们将无法隐藏状态栏。
  • 系统会忽略对 android:screenOrientation 属性的更改。

我知道对于大多数应用程序来说,区分纵向和横向模式是没有意义的,但是我正在开发包含相机视图的 SDK,用户可以进行他们希望的任何活动 - 包括支持多窗口模式的活动。问题是相机视图包含显示相机预览的 SurfaceView/TextureView,并且为了在所有活动方向上正确显示预览,需要有关正确活动方向的知识,以便可以正确旋转相机预览。

问题是我的代码通过检查当前配置方向(纵向或横向)和当前屏幕旋转来计算正确的活动方向。问题是在多窗口模式下,当前配置方向并不能反映真实的活动方向。这会导致相机预览旋转 90 度,因为 Android 报告的配置与方向不同。

我目前的解决方法是检查请求的活动方向并将其用作基础,但这有两个问题:

  1. 请求的活动方向不必反映实际的活动方向(即请求可能仍未得到满足)
  2. 请求的活动方向可以是“后面”、“传感器”、“用户”等,它不会透露有关当前活动方向的任何信息。
  3. 根据文档,在多窗口模式下实际上会忽略屏幕方向,因此 1. 和 2. 不起作用

即使在多窗口配置中,是否有任何方法可以稳健地计算正确的活动方向?

这是我目前使用的代码(请参阅有问题的部分的注释):

protected int calculateHostScreenOrientation() {
    int hostScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    int rotation = getDisplayOrientation(wm);

    boolean activityInPortrait;
    if ( !isInMultiWindowMode() ) {
        activityInPortrait = (mConfigurationOrientation == Configuration.ORIENTATION_PORTRAIT);
    } else {
        // in multi-window mode configuration orientation can be landscape even if activity is actually in portrait and vice versa
        // Try determining from requested orientation (not entirely correct, because the requested orientation does not have to
        // be the same as actual orientation (when they differ, this means that OS will soon rotate activity into requested orientation)
        // Also not correct because, according to https://developer.android.com/guide/topics/ui/multi-window.html#running this orientation
        // is actually ignored.
        int requestedOrientation = getHostActivity().getRequestedOrientation();
        if ( requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
                requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT ||
                requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT ||
                requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT ) {
            activityInPortrait = true;
        } else if ( requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
                requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE ||
                requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE ||
                requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE ) {
            activityInPortrait = false;
        } else {
            // what to do when requested orientation is 'behind', 'sensor', 'user', etc. ?!?
            activityInPortrait = true; // just guess
        }
    }

    if ( activityInPortrait ) {
        Log.d(this, "Activity is in portrait");
        if (rotation == Surface.ROTATION_0) {
            Log.d(this, "Screen orientation is 0");
            hostScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        } else if (rotation == Surface.ROTATION_180) {
            Log.d(this, "Screen orientation is 180");
            hostScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        } else if (rotation == Surface.ROTATION_270) {
            Log.d(this, "Screen orientation is 270");
            // natural display rotation is landscape (tablet)
            hostScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        } else {
            Log.d(this, "Screen orientation is 90");
            // natural display rotation is landscape (tablet)
            hostScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        }
    } else {
        Log.d(this, "Activity is in landscape");
        if (rotation == Surface.ROTATION_90) {
            Log.d(this, "Screen orientation is 90");
            hostScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        } else if (rotation == Surface.ROTATION_270) {
            Log.d(this, "Screen orientation is 270");
            hostScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
        } else if (rotation == Surface.ROTATION_0) {
            Log.d(this, "Screen orientation is 0");
            // natural display rotation is landscape (tablet)
            hostScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        } else {
            Log.d(this, "Screen orientation is 180");
            // natural display rotation is landscape (tablet)
            hostScreenOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
        }
    }
    return hostScreenOrientation;
}

private int getDisplayOrientation(WindowManager wm) {
    if (DeviceManager.getSdkVersion() < 8) {
        return wm.getDefaultDisplay().getOrientation();
    }

    return wm.getDefaultDisplay().getRotation();
}

private boolean isInMultiWindowMode() {
    return Build.VERSION.SDK_INT >= 24 && getHostActivity().isInMultiWindowMode();
}

protected Activity getHostActivity() {
    Context context = getContext();
    while (context instanceof ContextWrapper) {
        if (context instanceof Activity) {
            return (Activity) context;
        }
        context = ((ContextWrapper) context).getBaseContext();
    }
    return null;
}

编辑:我也将此报告给Android 问题跟踪器

4

2 回答 2

6

我不知道这是否应该被视为一种解决方案或只是一种解决方法。

正如您所说,您的问题来自 Android N 及其多窗口模式。当应用程序处于多窗口中时,您Activity不会绑定到完整的显示尺寸。这重新定义了Activity方向的概念。引用伊恩湖

事实证明:“纵向”实际上仅表示高度大于宽度,“横向”表示宽度大于高度。因此,考虑到这个定义,您的应用程序可以在调整大小的同时从一个过渡到另一个,这当然是有道理的。

Activity因此,方向改变和设备物理旋转之间不再存在联系。(我认为现在唯一合理使用 Activity 方向更改是更新您的资源。

由于您对设备尺寸感兴趣,因此只需获取它的DisplayMetrics. 引用文档,

如果从非活动上下文指标请求,将根据当前旋转和减去系统装饰区域报告整个显示的大小。

所以解决方案是:

final Context app = context.getApplicationContext();
WindowManager manager = (WindowManager) app.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
boolean portrait = height >= width;

当设备倾斜时,宽度和高度值将交换(或多或少)。

如果这行得通,我每次都会亲自运行它,删除isInMultiWindowMode()分支,因为

  • 不贵
  • 我们的假设也适用于非多窗口模式
  • 它可能适用于任何其他未来类型的模式
  • 您避免了isInMultiWindowMode() CommonsWare 描述的竞争条件
于 2016-12-30T19:43:09.270 回答
1

我认为您可以利用加速度计来检测“向下”的位置 - 从而检测手机的方向。工程师盖伊解释说,电话本身就是这样做的。

我在 SO 上搜索了一种方法,并找到了这个答案。基本上,您需要检查 3 个加速度计中的哪一个检测到引力的最重要分量,您知道在地球地面附近大约为 9.8m/s²。这是其中的代码片段:

private boolean isLandscape;

mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener,     mSensorManager.getDefaultSensor(
                                      Sensor.TYPE_ACCELEROMETER),1000000);
private final SensorEventListener mSensorListener = new SensorEventListener() { 
    @Override 
    public void onSensorChanged(SensorEvent mSensorEvent) {   
        float X_Axis = mSensorEvent.values[0]; 
        float Y_Axis = mSensorEvent.values[1]; 

        if((X_Axis <= 6 && X_Axis >= -6) && Y_Axis > 5){
        isLandscape = false; 
        }
        else if(X_Axis >= 6 || X_Axis <= -6){
        isLandscape = true;
        }

    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
};  

请小心,因为此代码可能不适用于所有情况,因为您需要考虑诸如在停止/加速的火车上或在游戏中快速移动手机等场景 - 这是wiki 上的数量级页面以帮助您入门。看起来你对 Khalil 在他的代码中输入的值(在他的回答中)是安全的,但我会格外小心并研究在不同场景中可能会生成哪些值。

这不是一个完美的想法,但我认为只要 API 以它的方式构建 - 不让您获得它们的计算方向 - 我认为这是一个有益的解决方法。

于 2016-12-29T21:39:10.673 回答