尝试拍摄前景屏幕截图时出现意外输出

Output is unexpected while trying to take a foreground-screenshot

所以我的问题是这段代码的输出是这个(正方形)而不是我应该期望的。为什么会发生这种情况的任何想法?

我会进一步解释,输出基本上是一个正方形的图像,但不是真正的正方形,但它包含所有视图,但它被压缩,不像我们应该期望的那样,一个全屏大小的图像,包含所有视图合适的地方:

代码:

private Bitmap getScreenshot(int id) {
    Log.d(TAG, "Creating screenshot");
    RelativeLayout RL= (RelativeLayout)findViewById(id);
    View v1 = getWindow().getDecorView().getRootView(); //or RL.getRootView();

    v1.setDrawingCacheEnabled(true);

    v1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                  MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    v1.layout(0, 0, v1.getMeasuredWidth(), v1.getMeasuredHeight()); 

    v1.buildDrawingCache(true);
    Bitmap bm = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);
    return bm;
}

图片:

这个问题的答案是: 只需强制视图的宽度和高度。 如果像 match_parent 这样自动缩放,屏幕截图将不会像输出的那样看到它。 代码: view.getLayoutParams().width = 宽度;

身高也一样 https://i.stack.imgur.com/mFuNG.png