用对话框截取屏幕截图

byl*_*nka 11 android

我需要以编程方式截取活动视图的截图.我已经找到了很多答案如何做到这一点,但没有回答如何使用打开的对话框来做到这一点

Asw*_*ran 7

好的,这个有点棘手.据我所知,没有直接的方法可以做到这一点.这是有效的.我试图拍摄单个照片并将它们分层.

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)

我刚尝试过这个并且有效.希望这可以帮助.如果它不起作用,请告诉我.


小智 5

试试这个库:https://github.com/jraska/Falcon.它包含所有活动窗口的截图,包括对话框,可以找出问题所在.


Ken*_*olf 0

连接设备/模拟器后,使用 DDMS 的“设备”窗口中的“屏幕捕获”按钮。这允许您截取屏幕上任何内容的屏幕截图,包括打开对话框时的屏幕截图。

在此输入图像描述

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