如何以编程方式截取整个活动页面的屏幕截图?[已关闭]

2022-09-04 22:23:13

如何截取 Android 上整个活动的屏幕截图,即使内容不可见?例如,截取完整聊天的屏幕截图,然后生成图像文件?

enter image description here

我也想要截图不可见的区域。

谢谢


答案 1

一种方法是提取当前屏幕的位图并保存它。试试这个:

public void takeScreenshot(){
    Bitmap bitmap = getScreenBitmap(); // Get the bitmap
    saveTheBitmap(bitmap);               // Save it to the external storage device.
}

public Bitmap getScreenBitmap() {
   View v= findViewById(android.R.id.content).getRootView();
   v.setDrawingCacheEnabled(true); 
   v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
   v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 

   v.buildDrawingCache(true);
   Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
   v.setDrawingCacheEnabled(false); // clear drawing cache
   return b;
}

安卓以编程方式拍摄屏幕截图


答案 2

这是拍摄视图“屏幕截图”的一种方法。我还没有测试过它,所以告诉它是否有效

//View v is the root view/parent of your layout, for example LinearLayout or RelativeLayout (layout where you have placed content of which you want to take screenshot)
private Bitmap getBitmapFromView(View v) {
    Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas (bitmap);
    v.draw(canvas);
    // Returns screenshot
    return bitmap;
}