react-native源码分析系列五 绘制js组件

本文深入探讨ReactNative如何将JS组件转化为原生控件进行绘制。通过UIManagerModule.java的分析,揭示了JS组件更新在Java层以确保一次性处理,避免中间状态显示,并利用CSSNode进行非UI线程的布局计算,最终在主线程批量更新视图。
摘要由CSDN通过智能技术生成

上篇分析了负责分发touch的ReatRootView,这篇文章继续分析view的绘制。

react-native绘制view的思路是将js写的控件映射到native的控件,通过addView之类的函数将js的控件添加到reactRootView.

首先看一个我个人项目中的例子。ui长这样。


用ddms的hierarchy view抓一下看一下。



可以看到有ReactDrawerLayout ReactImageView ReactTextView等,这些都是java的控件。因此我们看到js写的界面

全都被解析成了native的对应控件。具体是怎么实现的呢?

答案在这个类UIManagerModule.java.

/**

 * <p>Native module to allow JS to create and update native Views.</p>

 * <h2>== Transactional Requirement ==</h2>

 * A requirement of this class is to make sure that transactional UI updates occur all at, meaning

 * that no intermediate state is ever rendered to the screen. For example, if a JS application

 * update changes the background of View A to blue and the width of View B to 100, both need to

 * appear at once. Practically, this means that all UI update code related to a single transaction

 * must be executed as a single code block on the UI thread. Executing as multiple code blocks

 * could allow the platform UI system to interrupt and render a partial UI state. 

 *     //这里说 js的一批变化必须在java层要一起处理 不能出现中间态

 * <p>To facilitate this, this module enqueues operations that are then applied to native view

 * hierarchy through { @link NativeViewHierarchyManager} at the end of each transaction.

 * <h2>== CSSNodes ==</h2>

 * In order to allow layout and measurement to occur on a non-UI thread, this module also

 * operates on intermediate CSSNode objects that correspond to a native view. These CSSNode are able

 * to calculate layout according to their styling rules, and then the resulting x/y/width/height of

 * that layout is scheduled as an operation that will be applied to native view hierarchy at the end

 * of current batch.     //js通过操作cssnode来操作view 计算好全部的cssnode后再统一刷新ui(mainthread不能做耗时的工作

 */

public UIManagerModule(ReactApplicationContext reactContext, List<ViewManager> viewManagerList) {
  super(reactContext);
  mViewManagers = new ViewManagerRegistry(viewManagerList);               //各种控件的manager
  mEventDispatcher = new EventDispatcher(reactContext);                       //这个上篇文章见过 dispatch touch等事件的
  mNativeViewHierarchyManager = new NativeViewHierarchyManager(
     mAnimationRegistry, mViewManagers ) ;                                                   //统一管理视图变化
  mOperationsQueue = new UIViewOperationQueue(reactContext,this,mNativeViewHierarchyManager,
     mAnimationRegistry);                                                                             //js发过来的operation统一处理
  mNativeViewHierarchyOptimizer = new NativeViewHierarchyOptimizer(mOperationsQueue,
     mShadowNodeRegistry);
  DisplayMetrics displayMetrics = reactContext.getResources().getDisplayMetrics();
  DisplayMetricsHolder.setDisplayMetrics(displayMetrics);
  mModuleConstants = UIManagerModuleConstantsHelper.createConstants(d
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值