Flutter基础之接⼝实现初步学习1、 Flutter main ⼊⼝
Flutter 项⽬的⼊⼝是 main 函数,项⽬运⾏的开始
Flutter 在 android 项⽬⾥会把 Flutter 的 main ⼊⼝编译成 MainActivity
⽽这个 MainActivity 则继承⾃ FlutterActivity
⽰例:
class MainActivity(): FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
}
}
Flutter 就是在 android 中开始的,Flutter 编译的 android 项⽬只有这么⼀个 Activity 的。
2.、FlutterActivity
FlutterActivity ,是声明了 Provider, PluginRegistry, ViewFactory 3个接⼝,然后实现
1. Provider | ViewFactory - 是提供 view 视图的
2. PluginRegistry 着⼀连串⽅法可以看到是代理了 Activity N多⽣命周期函数
3. ⽰例:
/// activity实现了三个接⼝
public class FlutterActivity extends Activity implements Provider, PluginRegistry, ViewFactory {
/// 视图提供类,⼀个⽅法getFlutterView
public interface Provider {
FlutterView getFlutterView();
}
/// 插件注册
public interface PluginRegistry {
/// 注册⼀个插件
PluginRegistry.Registrar registrarFor(String var1);
/
// 是否有这个插件
boolean hasPlugin(String var1);
/// 插件发布值
/// 插件发布值
<T> T valuePublishedByPlugin(String var1);
/// 下⾯开始为插件⽣命周期回调
public interface PluginRegistrantCallback {
void registerWith(PluginRegistry var1);
}
/// 显然是视图销毁的回调
public interface ViewDestroyListener {
boolean onViewDestroy(FlutterNativeView var1);
}
不知道是什么,先留着看
public interface UserLeaveHintListener {
void onUserLeaveHint();
}
activity⽣命周期回调onNewIntent
public interface NewIntentListener {
boolean onNewIntent(Intent var1);
}
activity⽣命周期回调onActivityResult
public interface ActivityResultListener {
boolean onActivityResult(int var1, int var2, Intent var3);
}
activity⽣命周期回调之onRequestPermissionsResult
public interface RequestPermissionsResultListener {
boolean onRequestPermissionsResult(int var1, String[ var2, int[ var3);
}
/// 插件的注册者,相当于插件的宿主。
public interface Registrar {
Activity activity();
Context context();
Context activeContext();
/// 这个BinaryMessager不知道做什么的
BinaryMessenger messenger();
/// TextureRegistry暂不清楚
TextureRegistry textures();
/// 获取视图
FlutterView view();
/// 寻资源
String lookupKeyForAsset(String var1);
/// 寻资源
String lookupKeyForAsset(String var1, String var2);
/
// 发布值,与上⾯的valuePublishedByPlugin对应
PluginRegistry.Registrar publish(Object var1);
/// 增加⽣命周期回调
PluginRegistry.Registrar addRequestPermissionsResultListener(PluginRegistry.RequestPermissionsResultListener var1);
/// 增加⽣命周期回调
PluginRegistry.Registrar addActivityResultListener(PluginRegistry.ActivityResultListener var1);
PluginRegistry.Registrar addNewIntentListener(PluginRegistry.NewIntentListener var1);
/// 增加⽣命周期回调
PluginRegistry.Registrar addUserLeaveHintListener(PluginRegistry.UserLeaveHintListener var1);
/// 增加⽣命周期回调
PluginRegistry.Registrar addViewDestroyListener(PluginRegistry.ViewDestroyListener var1);
}
}
}
/// 视图⼯⼚
public interface ViewFactory {
/// 创建flutterView
FlutterView createFlutterView(Context var1);
创建nativeview
FlutterNativeView createFlutterNativeView();
/// 暂不清楚
boolean retainFlutterNativeView();
}
private final FlutterActivityEvents eventDelegate = delegate;
private final FlutterView.Provider viewProvider = delegate;
private final PluginRegistry pluginRegistry = delegate;
@Override
protected void onCreate(Bundle savedInstanceState) {
}
3、 FlutterActivityDelegate
FlutterActivity 把包括 onCreate 在内的所有⽣命周期函数都交给 eventDelegate 这个代理了, eventDelegate 的类型是FlutterActivityEvents ,那么重点是我们看看 FlutterActivityEvents 的 onCreate
flutter sdkpublic final class FlutterActivityDelegate implements FlutterActivityEvents, FlutterView.Provider, PluginRegistry {
........
@Override
public void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = Window();
window.addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(0x40000000);
}
String[ args = Intent());
flutterView = ateFlutterView(activity);
if (flutterView == null) {
FlutterNativeView nativeView = ateFlutterNativeView();
flutterView = new FlutterView(activity, null, nativeView);
flutterView.setLayoutParams(matchParent);
activity.setContentView(flutterView);
launchView = createLaunchView();
if (launchView != null) {
addLaunchView();
}
}
if (Intent())) {
return;
}
String appBundlePath = FlutterMain.ApplicationContext());
if (appBundlePath != null) {
runBundle(appBundlePath);
}
}
}
其中核⼼就是创建出 FlutterView 这个 view 对象,然后把 FlutterView setContentView 给 activity,这样
就完成了从 Flitter 到android 平台的显⽰,MainActivity 这个AC 可是只有 FlutterView 这⼀个 view ,Flutter 所有的 UI 组件都是由 FlutterView 使⽤canvas 直接绘制的。Flutter 就是这样和 android 联系的,脱离了 android 传统的 xml 布局⽅式,节省了 xml dom 解析反射⽣成viewTree 的操作。
4. FlutterView
FlutterView 可以解答我们⾮常核⼼问题,
public class FlutterView extends SurfaceView implements BinaryMessenger, TextureRegistry {
public FlutterView(Context context, AttributeSet attrs, FlutterNativeView nativeView) {
super(context, attrs);
Activity activity = (Activity) getContext();
if (nativeView == null) {
mNativeView = new ApplicationContext());
} else {
mNativeView = nativeView;
}
mSurfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
assertAttached();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
assertAttached();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
assertAttached();
}
};
}
FlutterView ⼜是继承⾃ SurfaceView ,就是说 UI 绘制不是由 android UI 线程完成,⽽是由 Flutter ⾃⼰完成。代码中 FlutterView 的绘制实际交给了 FlutterJNI() - FlutterJNI完成,
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论