在以前的一篇文章中,介紹了在原生項目中引入Flutter。php
在這個基礎上,記錄一下在Flutter中引入原生View。(建議先看看上面的文章)java
最終的結果就是,在原生項目中,以一個View的方式引入Flutter,再在這個Flutter的View中使用一個原生的View。android
效果圖以下:web
整個界面分紅了兩部分,上面是Flutter的View,裏面有個原生的ImageView。下面是原生的WebView。bash
首先是MainActivity的佈局文件,上面一個FrameLayout
用於承載Flutter。app
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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" android:background="#000000" tools:context=".MainActivity">
<FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintHeight_percent="0.5" app:layout_constraintTop_toTopOf="parent"></FrameLayout>
<WebView android:id="@+id/web" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/content"/>
</android.support.constraint.ConstraintLayout>
複製代碼
Flutter以一個View的方式被裝載。ide
class MainActivity : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableSlowWholeDocumentDraw()
setContentView(R.layout.activity_main)
val flutterView = Flutter.createView(this@MainActivity,lifecycle,"route1")
ViewRegistrant().registerWith(flutterView.pluginRegistry)
val layout = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
content.addView(flutterView, layout)
initWebView()
}
@SuppressLint("SetJavaScriptEnabled")
private fun initWebView() {
var webSettings = web.settings
webSettings.javaScriptEnabled = true
webSettings.setSupportZoom(false)
web.requestFocusFromTouch()
web.isVerticalScrollBarEnabled = false
web.isHorizontalScrollBarEnabled = false
web.loadUrl("https://www.baidu.com")
}
}
複製代碼
使用val flutterView = Flutter.createView(this@MainActivity,lifecycle,"route1")
生成一個FlutterView,而後Add到佈局中。「route1」會被傳入到Flutter中。佈局
繼承PlatformViewFactory
在它的create()
方法中返回一個在Flutter中要用的原生View。post
這裏我返回了一個ImageView
ui
class NativeImageView(createArgsCodec: MessageCodec<Any>) : PlatformViewFactory(createArgsCodec) {
override fun create(context: Context, i: Int, o: Any?): PlatformView {
var imageView = ImageView(context)
imageView.layoutParams = ViewGroup.LayoutParams(100, 100)
imageView.background = context.resources.getDrawable(R.mipmap.ic_launcher)
return object : PlatformView {
override fun getView(): View {
return imageView
}
override fun dispose() {
}
}
}
}
複製代碼
寫一個橋接器,把上面寫好的View傳進去。
class ViewRegistrant : PluginRegistry.PluginRegistrantCallback {
override fun registerWith(registry: PluginRegistry) {
val key = ViewRegistrant::class.java.canonicalName
if (registry.hasPlugin(key)) return
val registrar = registry.registrarFor(key)
registrar.platformViewRegistry().registerViewFactory("imageView", NativeImageView(StandardMessageCodec()))
}
}
複製代碼
這裏的"imageView"
,會在Flutter中用到。
裝載橋接器,把橋接器和咱們已經建立好的FlutterView進行綁定。
ViewRegistrant().registerWith(flutterView.pluginRegistry)
在Flutter中引用便可。
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: Container(
color: Color(0xff0000ff),
child: SizedBox(
width: size,
height: size,
child: AndroidView(
viewType: 'imageView',
),
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _changeSize,
child: new Icon(Icons.add),
),
);
複製代碼
注:此方法須要Android API 20以上,