2017-02-18 41 views
-1

所以我的问题是这个代码的输出是(这个广场之一),而不是我所期望的。任何想法为什么发生这种情况输出意想不到,而试图采取前景 - 屏幕截图

我将进一步解释这一点,输出基本上是一个像图像的方形,但不是真正的方形,但它包含所有的视图,但它与我们所期望的不同,是压缩的,全屏尺寸的图像与所有视图适当的位置:

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

+0

我不能添加新的链接(我需要10和声誉)。我只是绝望的管理员。链接:https://www.pixeldroidof.net/qcode.txt –

+0

http://stackoverflow.com/questions/2661536/how-to-programmatically-take-a-screenshot-in-android/5651242#5651242请检查此两个链接 http://stackoverflow.com/questions/32624133/how-to-take-a-screen-shot-on-a-button-click-can-anyone-provide-a-android-code/32624305#32624305 –

+0

如果你需要没有标题栏的屏幕截图,请查看http://stackoverflow.com/a/30212385/7320259 –

回答

0

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

上也是如此高度 https://i.stack.imgur.com/mFuNG.png