NativeViewExample,PlatformViewLink,AndroidViewSurface就是我们展示之后层次结构。
下面在详细分析。
=====================================================================================
要分析Hybrid composition不得不先了解一下:FlutterImageView。
FlutterImageView并不是一个真正的ImageView
事实上Hybrid composition上混合原生控件所需的突出合成都是通过FlutterImageView来实现的。FlutterImageView原版就是一个原生的View,我们来看源码:
// FlutterImageView 适用于开发人员需要渲染 Flutter UI,但也需要渲染交互式 io.flutter.plugin.platform.PlatformView 的情况。
public class FlutterImageView extends View implements RenderSurface {
@NonNull private ImageReader imageReader;
@Nullable private Queue imageQueue;
@Nullable private Image currentImage;
@Nullable private Bitmap currentBitmap;
@Nullable private FlutterRenderer flutterRenderer;
//从imageRender读取bitmap绘制到canvas上
@Override
protected void onDraw(Canvas cavas) {
super.onDraw(canvas);
if (!imageQueue.isEmpty()) {
if (currentImage != null) {
currentImage.close();
}
currentImage = imageQueue.poll();
updateCurrentBitmap();
}
if (currentBitmap != null) {
canvas.drawBitmap(currentBitmap, 0, 0, null);
}
}
}
源码基本上没什么可将,我们能看到FlutterImageView本质上是一个普通的原生View
,它实现了RenderSurface
接口,从而实现类似FlutterSurfaceView
的部分能力。
下面来了解一下FlutterImageView中的几个关键类作用:ImageReader
,Image
,Bitmap
-
ImageReader
:ImageReader 类允许应用程序直接访问渲染到 Surface 中的图像数据,图像数据封装在Image对象中,可以同时访问多个这样的对象,最多可达maxImages构造函数参数指定的数量。 通过其 Surface 发送到 ImageReader 的新图像将排队,直到通过 AcquireLatestImage 或 AcquireNextImage 调用访问。 -
Image
:Image就是包含了ByteBuffers的像素数据 -
Bitmap
:Bitmap就是将Image转化为可以绘制的位图
这样我们就能理解FlutterImageView,其实就是通过ImageReader读取Surface的数据,然后通过Bitmap绘制出来。Surface的数据从哪来呢?其实跟前面FlutterSurfaceView类似,都是从FlutterRender而来。
下面再来看Flutter侧,我们从上面Flutter Inspector视图层次入手,先来来分析PlatformViewLink
我们首先来看看PlatformViewLink的一个结构图:
PlatformViewLink源码部分,看构造函数:
class PlatformViewLink extends StatefulWidget {
const PlatformViewLink({
Key? key,
required PlatformViewSurfaceFactory surfaceFactory,
required CreatePlatformViewCallback onCreatePlatformView,
required this.viewType,
}) : assert(surfaceFactory != null),
assert(onCreatePlatformView != null),
assert(viewType != null),
_surfaceFactory = surfaceFactory,
_onCreatePlatformView = onCreatePlatformView,
super(key: key);
}
从代码可以看出,PlatformViewLink核心在于三个必选参数:surfaceFactory,onCreatePlatformView,viewType,其中surfaceFactory
和 onCreatePlatformView
仅在此widget的状态被初始化或 viewType
更改时被调用。
-
PlatformViewSurfaceFactory
: 工厂类,返回一个_surface,实际上也是一个widget,Android平台返回的是:AndroidViewSurface -
CreatePlatformViewCallback
: 返回的其实是通过PlatformViewsService.initSurfaceAndroidView
创建的一个PlatformViewController,通过它可以控制单个平台视图的界面,并与平台视图交互。
下面来逐一分析下AndroidViewSurface
和PlatformViewController
首先来看看PlatformViewController
PlatformViewController是一个抽象类,字面意思就是平台视图的控制器,被PlatformViewSurface引用,用于与平台视图交互,我们来看类源码:
abstract class PlatformViewController {
// 与控制器关联的视图ID,
// viewId 应始终唯一且非负。 并且它不能为空。
// viewId通过全局唯一的管理器PlatformViewsRegistry生成管理
int get viewId;
// 事件分发到平台视图
Future dispatchPointerEvent(PointerEvent event);
// Disposes平台视图
Future dispose();
// 清理平台视图的焦点
Future clearFocus();
}
我们再来看看AndroidViewSurface
,见源码:
class AndroidViewSurface extends PlatformViewSurface {
/// Construct an AndroidPlatformViewSurface
.
const AndroidViewSurface({
Key? key,
required AndroidViewController controller,
required PlatformViewHitTestBehavior hitTestBehavior,
required Set<Factory> gestureRecognizers,
}) : assert(controller != null),
assert(hitTestBehavior != null),
assert(gestureRecognizers != null),
super(
key: key,
controller: controller,
hitTestBehavior: hitTestBehavior,
gestureRecognizers: gestureRecognizers);
@override
RenderObject createRenderObject(BuildContext context) {
final PlatformViewRenderBox renderBox =
super.createRenderObject(context) as PlatformViewRenderBox;
(controller as AndroidViewController).pointTransformer =
(Offset position) => renderBox.globalToLocal(position);
return renderBox;
}
}
AndroidViewSurface
核心代码不多,我们在页面中创建的AndroidSurafeView,其实内部啥都没有。主要逻辑都在PlatformViewSurface
类中,后面单独开文章分析。AndroidViewSurface
的主要作用就是将 Android 视图与 Flutter 的合成器、触摸和语义子系统集成。合成器集成是通过PlatformViewRenderBox
将 PlatformViewLayer
添加到层树来完成的。PlatformViewLayer就是一个平台视图的合成层,是通过PlatformViewRenderBox
在paint时候,添加到PaintingContext
,Flutter中的Layer
后面再花单独文章来分析。
关于Hybrid composition的flutter framework层大致流程到这里介绍完毕了
我们这里再抛出一个疑问,在Hybrid Composition模式下,FlutterImageView是如何工作的呢?也就是说我们在flutterUI中创建了 PlatformViewLink,这时候引擎又是如何知道需要创建哪个PlatformView的呢?他们又是如何进行关联的呢?
代码看起来比较枯燥,这里直接画了个时序图
这样看流程比较清晰,这里大流程分为两步:
- 创建PlatformView
_platformViewLinkState是什么相信学Flutter应该都知道,在它的initState
方法里面间接调用到了SurfaceAndroidViewController
的create方法,在调用到_sendCreateMessage
方法,在_sendCreateMessage
方法内部,发送消息到Flutter Engine层PlatformViewsChannel.java的create方法。Flutter Engine通过dart端口传递过来的参数最终由PlatformViewsController的createAndroidViewForPlatformView
方法创建PlatformView。
- 创建PlatformViewLayer
引擎创建完PlatformView以后,会触发SurfaceAndroidViewController
的PlatformViewCreatedCallback
回调,间接触发了_PlatformViewLinkState
的setState
方法,最后通过AndroidViewSurface.createRenderObject
方法创建并添加PlatformViewLayer到flutter layer tree。
讲到这里我们再稍微来看看PlatformViewLayer是如何工作的
先开看看PlatformViewLayer源码:
class PlatformViewLayer extends Layer {
@override
void addToScene(ui.SceneBuilder builder, [ Offset layerOffset = Offset.zero ]) {
final Rect shiftedRect = layerOffset == Offset.zero ? rect : rect.shift(layerOffset);
builder.addPlatformView(
viewId,
offset: shiftedRect.topLeft,
width: shiftedRect.width,
height: shiftedRect.height,
);
}
}
从源码看到PlatformViewLayer代码很简单,直接调用到了ScenBuilder.addPlatformView
方法,我们继续看addPlatformView:
void addPlatformView(
int viewId, {
Offset offset = Offset.zero,
double width = 0.0,
double height = 0.0,
}) {
_addPlatformView(offset.dx, offset.dy, width, height, viewId);
}
void _addPlatformView(double dx, double dy, double width, double height, int viewId)
native ‘SceneBuilder_addPlatformView’;
addPlatformView见名知意,主要作用就是向scene添加平台视图。看源码addPlatformView
方法直接调用到了SceneBuilder_addPlatformView这个native方法。
这里省略若干的native c++的跟踪…
FlutterJNI.onDisplayPlatformView分析
SceneBuilder_addPlatformView
这个方法最终会调用到FlutterJNI.onDisplayPlatformView,看源码
@UiThread
public void onDisplayPlatformView(
int viewId,
int x,
int y,
int width,
int height,
int viewWidth,
int viewHeight,
FlutterMutatorsStack mutatorsStack) {
ensureRunningOnMainThread();
if (platformViewsController == null) {
throw new RuntimeException(
“platformViewsController must be set before attempting to position a platform view”);
}
platformViewsController.onDisplayPlatformView(
viewId, x, y, width, height, viewWidth, viewHeight, mutatorsStack);
}
可以看到它直接调用到了PlatformViewsController
的onDisplayPlatformView
方法:
/**
- 在当前帧中显示PlatformView的时候调用,每一帧开始都会走这个流程
*/
public void onDisplayPlatformView(
int viewId,
int x,
int y,
int width,
int height,
int viewWidth,
int viewHeight,
FlutterMutatorsStack mutatorsStack) {
//步骤1,创建并添加lutterImageView到FlutterView,FlutterImageView分析可以见上面分析。
initializeRootImageViewIfNeeded();
//步骤2,如果FlutterMutatorView为空的话,创建FlutterMutatorView,并将之前创建好的PlatformView添加到FlutterMutatorView中。再调用FlutterView的addView原生方法将FlutterMutatorView添加到FlutterView中。
//这样原生的PlatformView就显示到了FlutterView
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
总结
算法知识点繁多,企业考察的题目千变万化,面对越来越近的“金九银十”,我给大家准备好了一套比较完善的学习方法,希望能帮助大家在有限的时间里尽可能系统快速的恶补算法,通过高效的学习来提高大家面试中算法模块的通过率。
这一套学习资料既有文字档也有视频,里面不仅仅有关键知识点的整理,还有案例的算法相关部分的讲解,可以帮助大家更好更全面的进行学习,二者搭配起来学习效果会更好。
部分资料展示:
有了这套学习资料,坚持刷题一周,你就会发现自己的算法知识体系有明显的完善,离大厂Offer的距离更加近。
资料获取方法:点赞+关注+转发,然后进入我的【GitHub】,里面有免费获取途径
图片转存中…(img-1gt780wy-1711072486025)]
总结
算法知识点繁多,企业考察的题目千变万化,面对越来越近的“金九银十”,我给大家准备好了一套比较完善的学习方法,希望能帮助大家在有限的时间里尽可能系统快速的恶补算法,通过高效的学习来提高大家面试中算法模块的通过率。
这一套学习资料既有文字档也有视频,里面不仅仅有关键知识点的整理,还有案例的算法相关部分的讲解,可以帮助大家更好更全面的进行学习,二者搭配起来学习效果会更好。
部分资料展示:
[外链图片转存中…(img-Vrr0oLnS-1711072486025)]
[外链图片转存中…(img-5eIgYdeX-1711072486025)]
[外链图片转存中…(img-Oo8ddht1-1711072486026)]
[外链图片转存中…(img-ixaG9ljw-1711072486026)]
有了这套学习资料,坚持刷题一周,你就会发现自己的算法知识体系有明显的完善,离大厂Offer的距离更加近。
资料获取方法:点赞+关注+转发,然后进入我的【GitHub】,里面有免费获取途径