离屏页面的机器人拍摄截图截图、机器人、页面

2023-09-12 21:56:33 作者:越夜越有神

我工作的Andr​​oid应用程序。我有一个活动,比方说,它充满views..On整个屏幕单击按钮人工智能要开始另一个活动,说B,其中也有一些看法和控制。我想b活动进行屏幕之外,并希望采取的B从A的屏幕截图。可能吗?

I am working on an android application. I have an activity, say A, which fills the entire screen with views..On a button click in A I want to start another activity, say B, which also has some views and controls. I want activity B to be offscreen , and want to take the screenshot of B from A . Is it possible?

注:我成功地通过保存图纸缓存为位图,而是奋斗采取离屏页面的截图以网页A的屏幕截图

Note: I am successful in taking the screenshot of page A by saving the drawing cache in to a bitmap, but struggling to take the offscreen page's screenshot.

推荐答案

嗯,我已经实现了我想要的。这些是我使用的步骤。

Well I have achieved what I want. These are the steps I used.

与startActivityForResult开始活动B(),而不是 startActivity()。

Start activity B with startActivityForResult() instead of startActivity().

Intent bIntent = new Intent(A.this,B.class);
startActivityForResult(bIntent,B_REQUEST_CODE);

在b活动中的onCreate采取的B mainLayout并启用其 绘图缓存

机战页游 冲啊 机器人 海量截图曝光

In onCreate of activity B take mainLayout of B and enable its drawing cache

final LinearLayout layout = (LinearLayout)
                     findViewById(R.id.app_parent_layout);
layout.setDrawingCacheEnabled(true);
layout.setDrawingCacheQuality(LinearLayout.DRAWING_CACHE_QUALITY_HIGH);

在b活动等待,直到它的布局被完全抽出(这是很 重要)。对于活动中的B的onCreate(),获得mainLayout B,使用ViewTreeObserver得到一个电话回来时,它的布局绘制 彻底。

In activity B wait till its layout is drawn completely(This is very important). For that in onCreate() of activity B, get mainLayout of B, use ViewTreeObserver to get a call back when its layout is drawn completely.

ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
 @Override
 public void onGlobalLayout() {
  layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  getDrawingBitmap();
 }
});

现在getDrawingBitmap()被调用时,布局被完全抽出。 还有把你的截图。

Now getDrawingBitmap() gets called when layout is drawn completely. There take your screenshot.

public void getDrawingBitmap(){
    LinearLayout mainLayout = (LinearLayout)
                        findViewById(R.id.app_parent_layout);
    Bitmap b = mainLayout.getDrawingCache();

    File file = saveBitmapAsFile(b);

    Intent resultIntent = new Intent();
    resultIntent.putExtra("FullFileName",file.getAbsolutePath());
    setResult(Activity.RESULT_OK,resultIntent);
    finish();
}

在这里,我以位图,保存为一个文件,并返回 文件名作为结果code。我相信有更好的方法来发送 位图到父活动不是保存为文件,发送 文件名。但我有一个要求,仍要保存位图 一段时间。所以,对我来说这是最简便的方法。设置结果后 完成活动。

Here I am taking bitmap and saving it as a file and returning the filename as a result code. I am sure there are better method to send the bitmap to parent activity than saving as a file and sending filename. But I have a requirement anyway to save bitmap for sometime. So For me this is the easier method. After setting result finish activity.

现在的活动一个onActivityResult()来检索文件名。

Now on Activity A onActivityResult() retrieve the filename.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if(requestCode == B_REQUEST_CODE){
    if (resultCode == Activity.RESULT_OK) { 
      String newText = data.getStringExtra("FullFileName");
     File dir = new File (newText);
    } 
  }
}