FlatViewManager 在反应原生 android 中的目的是什么? [英] What is the purpose of FlatViewManager in react native android?

查看:37
本文介绍了FlatViewManager 在反应原生 android 中的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,FlatViewManager 会将它的所有子视图和它本身作为一个名为 FlatViewGroup 的视图进行平面化.但后来我发现 RCTTextManager 返回 RCTText 作为其阴影节点,而 RCTText 不是虚拟节点.那么 RCTTextManager 会返回 FlatViewGroup 作为它的 viewInstance 吗?FlatViewGroup 中会有视图层次结构吗?我不确定 flat 的含义是什么.

As far as my understanding, FlatViewManager would flat all its children and itself as a single view called FlatViewGroup. But then I found RCTTextManager return RCTText as its shadow nodes and RCTText is not a virtual node. So RCTTextManager will return FlatViewGroup as its viewInstance? And will there be view hierarchy in FlatViewGroup? I am not sure what the meaning of flat is.

推荐答案

你的理解是正确的,但是你缺少了一些东西.

Your understanding is correct, but there is something you are missing.

FlatViewGroup 本身不会做或画任何东西.你可以注意到每个 View 实际上都是一个 FlatViewGroup(文本、图像、视图——一切都映射到一个 FlatViewGroup).

FlatViewGroup doesn't do or draw anything by itself. You can notice that every View is actually a FlatViewGroup (Text, Image, View - everything maps to a FlatViewGroup).

但是通用的 FlatViewGroup 如何绘制图像、文本、边框等?事实是,它不知道这些.

But how can a generic FlatViewGroup draw images, text, borders and more? Thing is, it doesn't know about any of that.

它的工作方式是,视图、图像和文本在后台线程中准备内容,对其进行优化并尽可能少地使用资源(Android 视图).在许多情况下,它可以将数百个元素放入单个 FlatViewGroup.

The way it works, View, Image and Text prepare content in background thread, optimize it and try to use as little resources (Android Views) as possible. In many cases, it can fit hundreds of elements into a single FlatViewGroup.

但在某些情况下,查看次数越多越好.例如,如果有一个元素发生了很多变化,最好将该元素移出到它自己的 View 中,这样当该元素发生变化时,我们只能重新绘制该元素.

But in some cases, more Views is better. For example, it there is an element that you mutate a lot, it is best if that element is moved out into its own View so that when that element changes we can only redraw that element only.

每个 FlatShadowNode 中都有一个标志位,调用 mMountsToView 来控制我们是否可以将元素展平为父元素.

There is flag in every FlatShadowNode calles mMountsToView that controls whether we can flatten the element into a parent.

那么当 FlatShadowNode 确实将 mmMountsToView 标志设置为 true 时,我们应该使用哪个 View?好吧,这就是 ViewManager.createViewInstance() 发挥作用的地方.

So when a FlatShadowNode does have mMountsToView flag set to true, which View should we use? Well, this is where the ViewManager.createViewInstance() kicks in.

现在我可以回答你的问题了.是的,RCTViewManager 会为 RCTText 返回 FlatViewGroup,但不会为 每个 RCTText 返回 FlatViewGroup.大多数文本会将 mMountsToView 设置为 false 并因此扁平化为父级.

Now I can answer your question. Yes, RCTViewManager will return FlatViewGroup for an RCTText, but not for every RCTText. Most texts will have mMountsToView set to false and thus get flattened into a parent.

让您感到困惑的是虚拟节点与安装到视图.

The thing where you got confused is the virtual node vs mounts to a View.

虚拟节点是一个Flexbox的东西,意思是节点不是独立的,不能被测量.这有一个副作用,它总是与父非虚拟节点合并.这与 Flat 实现无关.

Virtual node is a Flexbox thing, it means that the node is not an independent one, and cannot be measured. This has a side effect that it is always merged with a parent non-virtual node. This is regardless of Flat implementation.

Flat UI Manager 将节点扁平化提升到一个全新的水平.它添加了这个 mMountsToView 标志,用于控制节点是否将进一步展平.如果节点发生大量变异并导致其兄弟节点无故重绘,则将其设置为 true.

Flat UI Manager takes node flattening to a whole new level. It adds this mMountsToView flag that controls if the node will be flatten further. Set it to true if the node mutates a lot and causes its siblings to redraw for no reason.

为了帮助您多了解一点,假设您要优化 RCTText.您注意到它有时会挂载到 FlatViewGroup,但由于它不能包含任何子项,因此它不必是 ViewGroup.您可以添加一个 FlatView(或者甚至更专业的东西,如 FlatTextView),将 FlatViewGroup 的内容复制到其中,删除所有子管理逻辑,修改 RCTTextManager 以返回您的视图,它会起作用.如果你重新运行你的 React,你会注意到一些文本现在正在使用你的新视图,但只有那些将 mMountsToView 标志设置为 true 的文本(你可以通过例如将文本不透明度设置为 0.99 来触发它).其余部分将被展平到可能仍在使用 FlatViewGroup 的父级中.

To help you understand a little bit more, imagine that you want to optimize RCTText. You noticed that it sometimes mounts to a FlatViewGroup, but since it cannot contain any children, it doesn't have to be a ViewGroup. You could add a FlatView (or even something more specialized, like FlatTextView), copy the contents of FlatViewGroup into it, remove all children management logic, modify RCTTextManager to return your View and it will work. If you rerun your React, you will notice that some texts are now using your new Views, but only those that have mMountsToView flag set to true (you can trigger it by e.g. setting text opacity to 0.99). The rest will be flattened into parent which is likely still using a FlatViewGroup.

希望有所帮助.

这篇关于FlatViewManager 在反应原生 android 中的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆