乐趣区

关于flutter:flutter截图方案

截图分两种:

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 库来截取。

退出移动版