好的,这个有点棘手.据我所知,没有直接的方法可以做到这一点.这是有效的.我试图拍摄单个照片并将它们分层.
Dialog dialog = // Dialog that is showing
View mainView = getSupportActivity().getWindow().getDecorView();
mainView = getSupportActivity().getWindow().getDecorView()
.findViewById(android.R.id.content);
mainView.setDrawingCacheEnabled(true);
// This is the bitmap for the main activity
Bitmap bitmap = mainView.getDrawingCache();
View dialogView = dialog.getView();
int location[] = new int[2];
mainView.getLocationOnScreen(location);
int location2[] = new int[2];
dialogView.getLocationOnScreen(location2);
dialogView.setDrawingCacheEnabled(true);
// This is the bitmap for the dialog view
Bitmap bitmap2 = dialogView.getDrawingCache();
Canvas canvas = new Canvas(bitmap);
// Need to draw the dialogView into the right position
canvas.drawBitmap(bitmap2, location2[0] - location[0], location2[1] - location[1],
new Paint());
String filename = // filename to save
File myPath = new File(filename);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
Trace.d("Twitter", "The file path is " + myPath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我刚尝试过这个并且有效.希望这可以帮助.如果它不起作用,请告诉我.
连接设备/模拟器后,使用 DDMS 的“设备”窗口中的“屏幕捕获”按钮。这允许您截取屏幕上任何内容的屏幕截图,包括打开对话框时的屏幕截图。

编辑:当我回答这个问题时,不清楚是否需要以编程方式完成,后来进行了编辑以澄清。