Flutter&Android 启动页(闪屏页)的加载流程和优化方案(2)

lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);

delegate = new FlutterActivityAndFragmentDelegate(this);

///创建绑定引擎等

delegate.onAttach(this);

///用于插件、框架恢复状态

delegate.onActivityCreated(savedInstanceState);

///设置窗口背景透明,隐藏 status bar

configureWindowForTransparency();

///这里是咱们的入口

setContentView(createFlutterView());

configureStatusBarForFullscreenFlutterExperience();

}

setContentView大家很熟悉,我们直接看createFlutterView() 这个方法:

@NonNull

private View createFlutterView() {

return delegate.onCreateView(

null /* inflater /, null / container /, null / savedInstanceState */);

}

FlutterActivityAndFragmentDelegate


flutter的初始化、启动等操作都是委托给它的。

我们继续看onCreateView,我将说明以注释的形式写在代码里

@NonNull

View onCreateView(

LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

Log.v(TAG, “Creating FlutterView.”);

ensureAlive();

if (host.getRenderMode() == RenderMode.surface) {

/// flutter 应用在surface上显示,所以会进入到这里

FlutterSurfaceView flutterSurfaceView =

new FlutterSurfaceView(

host.getActivity(), host.getTransparencyMode() == TransparencyMode.transparent);

// Allow our host to customize FlutterSurfaceView, if desired.

host.onFlutterSurfaceViewCreated(flutterSurfaceView);

// Create the FlutterView that owns the FlutterSurfaceView.

///用我们的flutterSurfaceView 初始化了一个 FlutterView

flutterView = new FlutterView(host.getActivity(), flutterSurfaceView);

} else {

FlutterTextureView flutterTextureView = new FlutterTextureView(host.getActivity());

// Allow our host to customize FlutterSurfaceView, if desired.

host.onFlutterTextureViewCreated(flutterTextureView);

// Create the FlutterView that owns the FlutterTextureView.

flutterView = new FlutterView(host.getActivity(), flutterTextureView);

}

// Add listener to be notified when Flutter renders its first frame.

flutterView.addOnFirstFrameRenderedListener(flutterUiDisplayListener);

/// 创建一个闪屏view - FlutterSplashView

flutterSplashView = new FlutterSplashView(host.getContext());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {

flutterSplashView.setId(View.generateViewId());

} else {

// TODO(mattcarroll): Find a better solution to this ID. This is a random, static ID.

// It might conflict with other Views, and it means that only a single FlutterSplashView

// can exist in a View hierarchy at one time.

flutterSplashView.setId(486947586);

}

/// 显示闪屏页

flutterSplashView.displayFlutterViewWithSplash(flutterView, host.provideSplashScreen());

Log.v(TAG, “Attaching FlutterEngine to FlutterView.”);

///所创建surface 绑定到engine上

flutterView.attachToFlutterEngine(flutterEngine);

return flutterSplashView;

}

这里我们可以大致了解到,创建了一个FlutterSurfaceView 它继承自surfaceView(我们的flutter页面也是渲染在这个surface上的)。之后我们用它初始化一个FlutterView,

FlutterView继承自 FrameLayout

随后我们再创建一个FlutterSplashView (继承FrameLayout)并调用displayFlutterViewWithSplash()方法。

public void displayFlutterViewWithSplash(

@NonNull FlutterView flutterView, @Nullable SplashScreen splashScreen) {

// If we were displaying a previous FlutterView, remove it.

if (this.flutterView != null) {

this.flutterView.removeOnFirstFrameRenderedListener(flutterUiDisplayListener);

removeView(this.flutterView);

}

// If we were displaying a previous splash screen View, remove it.

if (splashScreenView != null) {

removeView(splashScreenView);

}

// Display the new FlutterView.

this.flutterView = flutterView;

///添加flutterView

addView(flutterView);

this.splashScreen = splashScreen;

// Display the new splash screen, if needed.

if (splashScreen != null) {

if (isSplashScreenNeededNow()) {

Log.v(TAG, “Showing splash screen UI.”);

// This is the typical case. A FlutterEngine is attached to the FlutterView and we’re

// waiting for the first frame to render. Show a splash UI until that happens.

splashScreenView = splashScreen.createSplashView(getContext(), splashScreenState);

///添加 splashScreenView

addView(this.splashScreenView);

flutterView.addOnFirstFrameRenderedListener(flutterUiDisplayListener);

} else if (isSplashScreenTransitionNeededNow()) {

Log.v(

TAG,

“Showing an immediate splash transition to Flutter due to previously interrupted transition.”);

splashScreenView = splashScreen.createSplashView(getContext(), splashScreenState);

addView(splashScreenView);

transitionToFlutter();

} else if (!flutterView.isAttachedToFlutterEngine()) {

Log.v(

TAG,

“FlutterView is not yet attached to a FlutterEngine. Showing nothing until a FlutterEngine is attached.”);

flutterView.addFlutterEngineAttachmentListener(flutterEngineAttachmentListener);

}

}

}

这个方法对flutterView进行了保存(不用管这个),随后我们保存了一个 接口的实现类——splashScreen,这个实现类则是由FlutterActivity实现的(MainActivity)来提供的:

///host 是个接口,由FlutterActivity实现

回顾上面:flutterSplashView.displayFlutterViewWithSplash(flutterView, host.provideSplashScreen());

public SplashScreen provideSplashScreen() {

Drawable manifestSplashDrawable = getSplashScreenFromManifest();

if (manifestSplashDrawable != null) {

///DrawableSplashScreen 实现了 splashScreen的接口

return new DrawableSplashScreen(manifestSplashDrawable);

} else {

return null;

}

}

通过getSplashScreenFromManifest 初始化了一个drawable,我们看一下它内部:

@Nullable

@SuppressWarnings(“deprecation”)

private Drawable getSplashScreenFromManifest() {

try {

ActivityInfo activityInfo =

getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);

Bundle metadata = activityInfo.metaData;

///这里就是我们在 AndroidManifest.xml中设置启动页了

///SPLASH_SCREEN_META_DATA_KEY 的值 见下方

int splashScreenId = metadata != null ? metadata.getInt(SPLASH_SCREEN_META_DATA_KEY) : 0;

return splashScreenId != 0

? Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP

? getResources().getDrawable(splashScreenId, getTheme())

getResources().getDrawable(splashScreenId)

null;

} catch (PackageManager.NameNotFoundException e) {

// This is never expected to happen.

return null;

}

}

static final String SPLASH_SCREEN_META_DATA_KEY =

“io.flutter.embedding.android.SplashScreenDrawable”;

可以看到,通过上面的方法,我们以在AndroidManifest.xml设置的启动资源 初始化了一个drawable,进而初始化了DrawableSplashScreen 并返回。

我们回到displayFlutterViewWithSplash 继续向下看,

///通过我们之前的drawable 生成一个 view

splashScreenView = splashScreen.createSplashView(getContext(), splashScreenState);

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

学习福利

【Android 详细知识点思维脑图(技能树)】

其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。

这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。

详细整理在GitHub可以见;

Android架构视频+BAT面试专题PDF+学习笔记

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

ndroid 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。

这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。

[外链图片转存中…(img-7Q3YuwHk-1711393870926)]

详细整理在GitHub可以见;

Android架构视频+BAT面试专题PDF+学习笔记

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

  • 27
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值