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

标签 android flutter android-layout kotlin flutter-layout

我有一个双摄像头 Activity 作为 Android Kotlin 项目的实现。我想在 flutter 中使用相同的双摄像头,因为 flutter 没有任何用于双摄像头功能的库。
我曾尝试使用平台 channel 来做这件事,但事实证明平台 channel 只是用于 flutter 和原生平台之间的消息传递。我不只是想传递消息,我希望将一个 android 项目的 fragment/Activity 包含为一个 flutter 小部件,我可以在任何我想要的地方使用它。
基本上,有一个 Android Activity ,其中有一些与之关联的 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>

最佳答案

您可以尝试使用 PlatformView在 flutter 中。
您可以将您的 android/ios 功能转换为各自独立的 View / Controller ,并使用 AndroidView 将 View 添加到 Flutter 小部件树中。 (Android) 或 UiKitView (iOS)。

Platform views allow to embed native views in a Flutter app, so you can apply transforms, clips, and opacity to the native view from Dart. This allows you, for example, to use the native Google Maps from the Android and iOS SDKs directly inside your Flutter app, by using Platform Views.

PlatformView 可以引用官方文档here
以下是一个简单的 android View 示例:
flutter :
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(),
  );
}
安卓:
在android上创建原生View类并实现PlatformView来自安卓 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() {}
}
通过实现 PlatformViewFactory 为上述原生 View 类创建工厂类
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);
  }
}
最后只需注册 PlatformView在您的 FlutterActivity类(class):
public class MainActivity extends FlutterActivity {
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        flutterEngine
            .getPlatformViewsController()
            .getRegistry()
            .registerViewFactory("NativeView", new NativeViewFactory());
    }
}

关于android - 将 Android 实现转移到 Flutter 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64204898/

相关文章:

c# - Xamarin Forms - 将光标更改为等待光标

flutter - 如何检查用户是否已使用 Google Sign in 和 Firebase Auth 登录以检索他们的数据?

flutter - Flutter 是否会在 iOS 中自动显示 Cupertino UI,在 Android 中使用单一代码库显示 Material?

android - 如何在android中设置这个tableLayout?

android - 对话框 fragment 中的自动完成位置

android - 找不到动态功能中的 Activity (ClassNotFound)

android - 导入 Paypal 库

android - 如何在 android 中以 html 格式添加新行?

Android 相机视频 Intent 返回空 URI

android - Android Studio类 'FlutterLocationService'不是抽象的,没有实现抽象成员public abstract fun onRequestPermissionsResult