Android 如何捕获当前屏幕上的所有内容,包括对话框

Android 如何捕获当前屏幕上的所有内容,包括对话框,android,screenshot,Android,Screenshot,我可能看过每一篇关于在Android上以编程方式捕获屏幕(截图、屏幕转储)的文章,它们通常都会得到相同的答案 它的问题在于它捕获了您指定的视图,但没有捕获任何可能位于“根视图”顶部的对话框。这是我使用的代码,无法捕获任何位于“顶部”的对话框: 问题是:如何捕获整个屏幕,包括顶部的对话框?我只对捕获我正在编写的应用程序感兴趣,而不是主屏幕或类似的东西,只是根视图顶部的任何东西 我确实读过一些关于rooting的文章,但我真的希望完全截取应用程序Im编写的屏幕转储是不可能的。这是在打开的Dialog

我可能看过每一篇关于在Android上以编程方式捕获屏幕(截图、屏幕转储)的文章,它们通常都会得到相同的答案

它的问题在于它捕获了您指定的视图,但没有捕获任何可能位于“根视图”顶部的对话框。这是我使用的代码,无法捕获任何位于“顶部”的对话框:

问题是:如何捕获整个屏幕,包括顶部的对话框?我只对捕获我正在编写的应用程序感兴趣,而不是主屏幕或类似的东西,只是根视图顶部的任何东西


我确实读过一些关于rooting的文章,但我真的希望完全截取应用程序Im编写的屏幕转储是不可能的。

这是在打开的DialogFragment中工作的

View v1 = ((ViewGroup) (((MyActivity)getActivity()).findViewById(android.R.id.content)));
v1.setDrawingCacheEnabled(true);
Bitmap bitmapParent = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

// dialogView is the inflated view of the DialogFragment
dialogView.setDrawingCacheEnabled(true);
Bitmap bitmapDialog = Bitmap.createBitmap(dialogView.getDrawingCache());
dialogView.setDrawingCacheEnabled(false);

Canvas canvas = new Canvas(bitmapParent);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bitmapDialog, 0, 0, paint);   

// Activity and dialog captured!!
bitmapParent.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(directory, name)));

您可能需要将所有视图根绘制到位图。试用此库:它可以将日志捕获到您的屏幕截图中。

使用此库。。。。这对我很有用。


“问题是:如何捕获整个屏幕,包括顶部的对话框?”--好的,您不能,除非通过根方法。如果对话框是您的,您应该也可以使用getDrawingCache()方法来捕获它们,那么您只需将dialog.jpg覆盖在activity.jpg.Thx上即可。。。所以,我必须“向后”迭代,从根视图到子视图,然后将每个视图覆盖到根视图图像上?Sized,你能让它工作吗?Ted,你试过让根视图的所有子视图,然后将所有位图合并到画布上吗?请将链接的主要内容添加到问题主体。
View v1 = ((ViewGroup) (((MyActivity)getActivity()).findViewById(android.R.id.content)));
v1.setDrawingCacheEnabled(true);
Bitmap bitmapParent = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

// dialogView is the inflated view of the DialogFragment
dialogView.setDrawingCacheEnabled(true);
Bitmap bitmapDialog = Bitmap.createBitmap(dialogView.getDrawingCache());
dialogView.setDrawingCacheEnabled(false);

Canvas canvas = new Canvas(bitmapParent);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bitmapDialog, 0, 0, paint);   

// Activity and dialog captured!!
bitmapParent.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(directory, name)));
  // Saving screenshot to file
            Falcon.takeScreenshot(this, imageFile);
            // Take bitmap and do whatever you want
            Bitmap bitmap = Falcon.takeScreenshotBitmap(this);