ReactNative动画效果分析(仅从Android端源代码进行分析)

}).start()

}

}

通过查看AnimatedImplementation.js文件可以查看到动画源码,篇幅有限仅展示核心代码

所有Animated的js源码都在AnimatedImplementation.js中,本文RN版本为0.57.8.

首先看下动画的入口函数:Animated.timing

该函数定义在181行,不同RN版本对应的行数不一致,请以自己的版本为准。

const timing = function(

value: AnimatedValue | AnimatedValueXY,

config: TimingAnimationConfig,

): CompositeAnimation {

const start = function(

animatedValue: AnimatedValue | AnimatedValueXY,

configuration: TimingAnimationConfig,

callback?: ?EndCallback,

): void {

callback = _combineCallbacks(callback, configuration);

const singleValue: any = animatedValue;

const singleConfig: any = configuration;

singleValue.stopTracking();

if (configuration.toValue instanceof AnimatedNode) {

singleValue.track(

new AnimatedTracking(

singleValue,

configuration.toValue,

TimingAnimation,

singleConfig,

callback,

),

);

} else {

singleValue.animate(new TimingAnimation(singleConfig), callback);

}

};

这里的singleValue是传入的第一个参数this.state.bounceValue,为AnimatedValue类型。

这里的singleConfig是传入的第二个参数。

首先分析下第一个参数的AnimatedValue这个类型的animate方法:

animate(animation: Animation, callback: ?EndCallback): void {

let handle = null;

if (animation.__isInteraction) {

handle = InteractionManager.createInteractionHandle();

}

const previousAnimation = this._animation;

this._animation && this._animation.stop();

this._animation = animation;

animation.start(

this._value,

value => {

// Natively driven animations will never call into that callback, therefore we can always

// pass flush = true to allow the updated value to propagate to native with setNativeProps

this._updateValue(value, true /* flush */);

},

result => {

this._animation = null;

if (handle !== null) {

InteractionManager.clearInteractionHandle(handle);

}

callback && callback(result);

},

previousAnimation,

this,

);

}

该方法内部主要是为了调用传入参数animation的start方法,这个参数就是上面传过来的new TimingAnimation(singleConfig)对象。TimingAnimation的start方法如下:

start(

fromValue: number,

onUpdate: (value: number) => void,

onEnd: ?EndCallback,

previousAnimation: ?Animation,

animatedValue: AnimatedValue,

): void {

this.__active = true;

this._fromValue = fromValue;

this._onUpdate = onUpdate;

this.__onEnd = onEnd;

const start = () => {

// Animations that sometimes have 0 duration and sometimes do not

// still need to use the native driver when duration is 0 so as to

// not cause intermixed JS and native animations.

if (this._duration === 0 && !this._useNativeDriver) {

this._onUpdate(this._toValue);

this.__debouncedOnEnd({finished: true});

} else {

this._startTime = Date.now();

if (this._useNativeDriver) {

this.__startNativeAnimation(animatedValue);

} else {

this._animationFrame = requestAnimationFrame(

this.onUpdate.bind(this),

);

}

}

};

if (this._delay) {

this._timeout = setTimeout(start, this._delay);

} else {

start();

}

}

这里出现了分支,当注释@1打开时,_useNativeDriver为true,动画的执行方式将交由原生端,不再走下边的js端。

原生端实现:

可以看到js调用了__startNativeAnimation方法,我们看下__startNativeAnimation

__startNativeAnimation(animatedValue: AnimatedValue): void {

animatedValue.__makeNative();

this.__nativeId = NativeAnimatedHelper.generateNewAnimationId();

NativeAnimatedHelper.API.startAnimatingNode(

this.__nativeId,

animatedValue.__getNativeTag(),

this.__getNativeAnimationConfig(),

this.__debouncedOnEnd.bind(this),

);

}

我们着重看下这个方法NativeAnimatedHelper.API.startAnimatingNode:

startAnimatingNode: function(

animationId: ?number,

nodeTag: ?number,

config: Object,

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

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

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

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

最后送福利了,现在关注我并且加入群聊可以获取包含源码解析,自定义View,动画实现,架构分享等。
内容难度适中,篇幅精炼,每天只需花上十几分钟阅读即可。
大家可以跟我一起探讨,欢迎加群探讨,有flutter—底层开发—性能优化—移动架构—资深UI工程师 —NDK相关专业人员和视频教学资料,还有更多面试题等你来拿

点击GitHub领取
录播视频图.png

[外链图片转存中…(img-RXMGV889-1711068766171)]

最后送福利了,现在关注我并且加入群聊可以获取包含源码解析,自定义View,动画实现,架构分享等。
内容难度适中,篇幅精炼,每天只需花上十几分钟阅读即可。
大家可以跟我一起探讨,欢迎加群探讨,有flutter—底层开发—性能优化—移动架构—资深UI工程师 —NDK相关专业人员和视频教学资料,还有更多面试题等你来拿

点击GitHub领取
[外链图片转存中…(img-sOI2QmXH-1711068766171)]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值