采取截图与对话截图

2023-09-06 02:27:04 作者:美腿欧巴

我需要截图编程活动视图。 我已经找到了很多的答案如何做到这一点的, 但没有回答如何与打开的对话​​框中做到这一点

I need to take screenshot programmatically of activity view. I have found a lot of answers how to do it, but there is not answer how to do it with opened dialog

推荐答案

确定这个人是有点棘手。没有直接的办法做到这一点,据我所知。下面是一些作品。我试图以个人的拍摄和层他们。

Ok this one is a little tricky. There is no direct way to do it as far as I know. Here is something that works. I tried to take individual shots and layer them.

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

我只是想这和它的工作。 希望这可以帮助。让我知道,如果它不能正常工作。

I just tried this and it worked. Hope this helps. Let me know if it doesn't work.