android – 用对话框截图

前端之家收集整理的这篇文章主要介绍了android – 用对话框截图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要以程序方式截取活动视图.
我已经找到很多答案怎么做,
但是没有回答如何用打开的对话框来做到这一点

解决方法

好的,这个有点棘手.据我所知,没有直接的方法去做.这是有用的东西.我试图拍摄单独的照片并分层.
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();
}

我只是试过了,它的工作.希望这可以帮助.让我知道如果不行.

原文链接:https://www.f2er.com/android/310814.html

猜你在找的Android相关文章