如何设置带有缺口显示的Media Recorder的尺寸

时间:2019-07-05 03:57:53

标签: android mediarecorder

我正在使用MediaRecorder记录表面,所以我在准备

之前将视频大小定义为MediaRecorder
Size size = getWindowManager().getDefaultDisplay().getSize(size);
... preparing the media recorder
mediaRecorder.setVideoSize(size.x, size.y);

因此,如果没有缺口,则可以正常工作,当出现缺口的设备时,视频将记录为黑色。

请帮助我

预先感谢

2 个答案:

答案 0 :(得分:2)

检测并减去缺口高度

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;

    Log.d("height",""+height);

    //Detect notch
    int statusBarHeight = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        statusBarHeight = getResources().getDimensionPixelSize(resourceId);
    }

    if (statusBarHeight > Math.round( 24 * (displayMetrics.densityDpi / 160f))) {
        height -= statusBarHeight;
    }

    Log.d("height",""+height);

答案 1 :(得分:0)

我将答案分为两部分:

  1. 在Android 9(API级别28)中使用API​​。

    /**
     * Returns true if this WindowInsets has any nonzero insets.
     *
     * @return true if any inset values are nonzero
     */
    public boolean hasInsets() {
        return hasSystemWindowInsets() || hasWindowDecorInsets() || hasStableInsets()
                || mDisplayCutout != null;
    }
    
    /**
     * Returns the display cutout if there is one.
     *
     * @return the display cutout or null if there is none
     * @see DisplayCutout
     */
    @Nullable
    public DisplayCutout getDisplayCutout() {
        return mDisplayCutout;
    } 
    

我们使用API​​并通过应用setOnApplyWindowInsetsListener获取displayCutout 在我们的根视图上并保存大小。您还可以添加所需的逻辑以在需要时进行其他调整。 一旦有了插图,我们就可以检查是否有切口,并可以根据需要计算尺寸。 另外,您可以将活动设置为呈现,就像没有缺口/切口一样 更多信息可以阅读here

  1. 使用一些OEM提供的API(在本例中,我向华为展示,我认为还有更多OEM拥有自己的API,但我不确定) 下面的代码是Kotlin中的2个Context扩展,使我们可以检查是否存在缺口,并获取其大小以用于进一步的工作。

    fun Context.hasNotchInScreenHuawei(): Boolean {
            var ret = false
            try {
                val cl = this.classLoader
                val hwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil")
                val get = hwNotchSizeUtil.getMethod("hasNotchInScreen")
                ret = get.invoke(hwNotchSizeUtil) as Boolean
            } catch (ignored: Exception) {
            } finally {
                return ret
            }
        }
    
    fun Context.getNotchSizeHuawei(): IntArray {
        var ret = intArrayOf(0, 0)
        try {
            val cl = this.classLoader
            val hwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil")
            val get = hwNotchSizeUtil.getMethod("getNotchSize")
            ret = get.invoke(hwNotchSizeUtil) as IntArray
        } catch (ignored: Exception) {
        } finally {
            return ret
        }
    }
    

您还需要在应用程序下向Manifest添加简短的元数据。

    <meta-data android:name="android.notch_support" android:value="true" />

这将使华为手机了解您的应用程序使用了由华为添加的API