写一个小例子,比如在flutter的界面中显示android原生的TextView组件,
效果图:
步骤如下:
1、通过Android studio打开flutter工程,选中工程名,单击鼠标右键,"Flutter"->"Open Android module in Android Studio",打开Android模块。
2、创建一个Class实现PlatformView接口,比如新建CustomView类,实现PlatformView接口,在CustomView的构造方法中实例化TextView,构造方法也可以接收外部传来的参数,如TextView要显示的文本text。在getView()方法中返回TextView实例.
package com.example.eiss_app.plugin;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import io.flutter.plugin.platform.PlatformView;
public class CustomView implements PlatformView {
private TextView textView;
public CustomView(Context context,String text) {
textView = new TextView(context);
textView.setText(text);
}
@Override
public View getView() {
return textView;
}
@Override
public void dispose() {
}
}
3、创建一个Class继承自PlatformViewFactory,重写create方法,在create方法中实例化刚刚的View类CustomView,然后返回。create方法的第三个参数表示flutter传递给android的参数。
package com.example.eiss_app.plugin;
import android.content.Context;
import java.util.Map;
import io.flutter.plugin.common.MessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;
public class CustomViewFactory extends PlatformViewFactory {
public CustomViewFactory(MessageCodec
4、创建一个class用来注册view,这个class提供一个方法,用来注册,方法名随便写,需要一个参数PluginRegistry,具体如下:
注意 registrar.platformViewRegistry().registerViewFactory("widget.name", new CustomViewFactory(new StandardMessageCodec()))中的widget.name,这个会在flutter中用到。
package com.example.eiss_app.plugin;
import android.util.Log;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.StandardMessageCodec;
public class CustomViewRegistrant {
private static final String TAG = CustomViewRegistrant.class.getName();
public static void registerWith(PluginRegistry registry) {
final String key = TextureWidgetRegistrant.class.getCanonicalName();
Log.i(TAG,"registerKey="+key);
if (registry.hasPlugin(key)) {
return;
}
PluginRegistry.Registrar registrar = registry.registrarFor(key);
registrar.platformViewRegistry().registerViewFactory("widget.name", new CustomViewFactory(new StandardMessageCodec()));
}
}
5、在MainActivity中调用注册类。
注意:不用手动修改GeneratedPluginRegistrant类,自己写的插件不要放到GeneratedPluginRegistrant中注册,而是应该自己写注册类,然后在MainActivity中调用注册类
package com.example.eiss_app;
import android.os.Bundle;
import android.util.Log;
import com.example.eiss_app.plugin.hkvideo.CustomViewRegistrant;
import com.example.eiss_app.plugin.hkvideo.HKPlayer;
import com.example.eiss_app.plugin.hkvideo.HKVideoPlayerFlutterPlugin;
import com.example.eiss_app.plugin.hkvideo.TextureWidgetRegistrant;
import com.hikvision.open.hikvideoplayer.HikVideoPlayerFactory;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
private static final String TAG = MainActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
CustomViewRegistrant.registerWith(this);
}
}
6、在flutter中使用,使用AndroidView,viewType为刚刚定义的"widget.name".
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class My extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: AndroidView(
viewType: 'widget.name',
creationParams: {'text': 'Flutter传给Android的参数'},
creationParamsCodec: StandardMessageCodec(),
),
);
}
}