尝试拍摄前景截图时输出是意料之外的

时间:2017-02-18 10:34:28

标签: java android image android-bitmap

所以我的问题是这段代码的输出是这个(方形的)而不是我期望的。任何想法为什么会这样?

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

enter image description here

代码:

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;
}

图像:

enter image description here

1 个答案:

答案 0 :(得分:0)

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

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