将 Android 实现转移到 Flutter 应用程序

Ham*_*hid 8 android android-layout kotlin flutter flutter-layout

我有一个作为 Android Kotlin 项目的双摄像头活动的实现。我想在 flutter 中使用相同的双摄像头,因为 flutter 没有任何用于双摄像头功能的库。

我曾尝试使用平台通道来做这件事,但事实证明平台通道仅用于 Flutter 和本机平台之间的消息传递。我不只是想传递消息,我想要一个 android 项目的片段/活动作为颤振小部件包含在内,我可以在任何我想要的地方使用它。

基本上,有一个 Android 活动,它有一些与之相关的 Kotlin 代码。我希望所有这些都在 Flutter 中工作并与之通信,包括 XML 前端和 Kotlin 后端。我在这里附上我的 xml 文件以供参考。代码文件仅填充 xml 组件。


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <SurfaceView
            android:id="@+id/surfaceView2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1.2" />

        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="-65dp"
            android:layout_weight="1" />
    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:maxHeight="30dp"
            android:src="@drawable/ic_inclinedline"/>
    </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Run Code Online (Sandbox Code Playgroud)

小智 5

您可以尝试使用PlatformViewin flutter执行此操作。

您可以将您的 android/ios 功能转换为它们各自独立的视图/控制器,并使用AndroidView(Android) 或UiKitView(iOS)将视图添加到颤动小部件树中。

平台视图允许在 Flutter 应用程序中嵌入原生视图,因此您可以将变换、剪辑和不透明度应用到 Dart 的原生视图。例如,这允许您通过使用平台视图直接在 Flutter 应用程序中使用来自 Android 和 iOS SDK 的原生 Google 地图。

你可以参考官方文档PlatformView here

以下是一个简单的 android 视图示例:

扑:

Widget build(BuildContext context) {
  // This is used in the platform side to register the view.
  final String viewType = "NativeView";
  // Pass parameters to the platform side.
  final Map<String, dynamic> creationParams = <String, dynamic>{};

  return AndroidView(
    viewType: viewType,
    layoutDirection: TextDirection.ltr,
    creationParams: creationParams,
    creationParamsCodec: const StandardMessageCodec(),
  );
}
Run Code Online (Sandbox Code Playgroud)

Android: 在android上创建原生View类,并PlatformView从android flutter插件实现

class NativeView implements PlatformView {
   @NonNull private final TextView textView;

    NativeView(@NonNull Context context, int id, @Nullable Map<String, Object> creationParams) {
        textView = new TextView(context);
        textView.setTextSize(72);
        textView.setBackgroundColor(Color.rgb(255, 255, 255));
        textView.setText("Rendered on a native Android view (id: " + id + ")");
    }

    @NonNull
    @Override
    public View getView() {
        return textView;
    }

    @Override
    public void dispose() {}
}
Run Code Online (Sandbox Code Playgroud)

通过实现上面的原生视图类创建一个工厂类 PlatformViewFactory

class NativeViewFactory extends PlatformViewFactory {
  @NonNull private final BinaryMessenger messenger;
  @NonNull private final View containerView;

  NativeViewFactory(@NonNull BinaryMessenger messenger, @NonNull View containerView) {
    super(StandardMessageCodec.INSTANCE);
    this.messenger = messenger;
    this.containerView = containerView;
  }

  @NonNull
  @Override
  public PlatformView create(@NonNull Context context, int id, @Nullable Object args) {
    final Map<String, Object> creationParams = (Map<String, Object>) args;
    return new NativeView(context, id, creationParams);
  }
}
Run Code Online (Sandbox Code Playgroud)

最后只需PlatformView在您的FlutterActivity班级中注册:

public class MainActivity extends FlutterActivity {
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        flutterEngine
            .getPlatformViewsController()
            .getRegistry()
            .registerViewFactory("NativeView", new NativeViewFactory());
    }
}
Run Code Online (Sandbox Code Playgroud)