android截取屏幕外页面的截图 [英] android taking screenshot of offscreen page

查看:37
本文介绍了android截取屏幕外页面的截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个安卓应用程序.我有一个活动,比如说 A,它用视图填充整个屏幕.在 A 中单击按钮,我想启动另一个活动,比如说 B,它也有一些视图和控件.我希望活动 B 在屏幕外,并希望从 A 截取 B 的屏幕截图.是否可以?

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.

  1. 用 startActivityForResult() 代替开始活动 BstartActivity().

  1. 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() 中,获取 mainLayoutB、使用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();
    }
    

    这里我取位图并将其保存为文件并返回文件名作为结果代码.我相信有更好的发送方法位图到父活动而不是另存为文件并发送文件名.但无论如何我有一个要求来保存位图某个时候.所以对我来说,这是更简单的方法.设置结果后完成活动.

    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.

    现在在活动 A 的 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);
        } 
      }
    }
    

  • 这篇关于android截取屏幕外页面的截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆