截图分两种:

1.原生能力截图

1、原生能力截图分为Android和iOS,
Android不能直接截取包含flutter和原生界面的截图,只能单独截取flutter或者原生界面

//android截Native界面核心代码
Window window = this.activity.getWindow();
View view = this.activity.getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(
    view.getWidth(),
    view.getHeight(),
    Bitmap.Config.ARGB_8888
); // Bitmap()
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
//Android截flutter界面核心代码
View view = this.activity.getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);

Bitmap bitmap = null;
if (this.renderer.getClass() == FlutterView.class) {
    bitmap = ((FlutterView) this.renderer).getBitmap();
} else if(this.renderer.getClass() == FlutterRenderer.class ) {
    bitmap = ( (FlutterRenderer) this.renderer ).getBitmap();
}

if(bitmap == null) {
    Log.println(Log.INFO, TAG, "The bitmap cannot be created :(");
    return ;
}
view.setDrawingCacheEnabled(false);

iOS可以直接截取包含flutter和原生界面的截图。

// iOS截图核心代码
  func takeScreenshot(view: UIView, toImageGallery :Bool = true) {
        let scale :CGFloat = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, scale)
        view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
        let optionalImage :UIImage? = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
}

特殊情况:
Android需要截取含有flutter与Native的界面,可以分别单独截取flutter界面和Native界面,获取到两个bitmap,然后做一个bitmap合并,最终得到flutter与native界面的截图

2.flutter能力截图

2、flutter能力截图可以使用screenshot库来截取。


zorro
52 声望2 粉丝