gpt4 book ai didi

android - 使用对话框截取屏幕截图

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:00:21 25 4
gpt4 key购买 nike

我需要以编程方式截取 Activity View 的屏幕截图。我找到了很多答案如何去做,但没有回答如何用打开的对话框做到这一点

最佳答案

好吧,这个有点棘手。据我所知,没有直接的方法可以做到这一点。这是有用的东西。我尝试拍摄单张照片并将它们分层。

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

我刚刚试过了,它成功了。希望这可以帮助。如果它不起作用,请告诉我。

关于android - 使用对话框截取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17393656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com