21

Android 中的“后台无效动画“行为分析 · Android Performance

 4 years ago
source link: https://www.androidperformance.com/2019/10/24/Android-Background-Animation/?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Android 中的“后台无效动画“行为分析



字数统计: 1.3k阅读时长: 4 min
 2019/10/24  954  Share

当一个 Android App 退到后台之后,只要他没有被杀死,那么他做什么事情大家都不要奇怪,因为这就是 Android。但是当用户知道一个你一个 App 退到后台之后还在持续做无效的动画,而这个动画完全是无意义的,而且用户还不知道他在做动画,消耗用户那可怜的电量的时候,轻则被多任务杀掉,禁止后台运行,重则直接卸载。

一般的开发者很难发现这个问题,但是如果你经常使用 Systrace ,多开几十个应用然后退回到桌面,左右滑动抓取 Systrace ,就可以很容易发现,总有那么几个后台的应用,还在频繁地做无效的动画。

这里说的后台做动画,指的是由于某种原因,应用在退到后台之后,用户看不到任何这个 App 界面的时候,他仍然在后台不断地更新,耗费 CPU。引起这个问题的原因可能有好多个,毕竟 往 Choreographer 扔 CALLBACK_ANIMATION 的地方太多了,而且每个应用可能都不一样,但最终还是需要各个应用去做修复

下面我们就以两个实例,从技术的角度来看一下事件发生时候的情况和原因,希望看到这篇文章的开发者,检查一下自己的应用是否有这个问题,有则改之,无则恭喜

实例 - 网易新闻

我们在使用网易新闻后,将网易新闻退到后台,然后左右滑动桌面,抓 Systrace 来看:

网易新闻到后台之后还在持续做 Animation 的回调(红框内),每一帧都还是在 doFrame 操作

15718769486548.jpg

放大每一个 doFrame 来看,Choreographer 中的 input 和 traversal 都没有触发,只有 animation 的回调一直在执行

15718769576130.jpg

我们把这份 Trace 上的 cpu 部分全选,然后下面按照 Wall Duration 排序,可以发现网易新闻后台动画执行时间最长。应用已经在后台且不可见的时候,还在这么频繁地工作,占用 CPU 资源,消耗电量,实在是不应该

15718769674934.jpg

抓对应的 MethodTrace 来看,就是在做动画,没有进行关闭 ,动画依旧在每一帧进行 onAnimationUpdate 的回调 ,可以看到这里是因为使用了 Airbnb 的 Lottie 库导致的,动画没有关闭,所以还是一直在做触发

15718769752471.jpg

实例 - QQ音乐

启动 QQ 音乐,然后回到桌面, 左右滑动桌面并抓取 Systrace 和 MethodTrace ,可以看到跟上面的网易新闻的表现一致

15718769826711.jpg

抓取了 QQ 音乐的后台动画时候的 MethodTrace 发现,也是由于退到后台之后,没有暂停动画导致的,也是 Airbnb 的 Lottie 的锅, 而且 QQ 音乐有三个动画没有停止,比网易新闻还要严重一些

15718769926973.jpg

放大后可以看到

15718770037520.jpg

当然也不是每一个都是 Airbnb 的 Lottie 动画库引起的,比如下面这个,就是普通的动画没有结束

15718770123948.jpg

根本原因是应用在不可见之后,没有将动画暂停,导致应用切换到后台之后,依然在刷新动画的回调,但此时由于是不可见的,不会触发 Input Callback 和 draw Callback ,所以也不会有任何的绘制操作,也就是说这个 Animation 的刷新完全是没有意义的(当然也有可能是业务需求?)

上面两个例子里面,网易新闻和 QQ 音乐都是因为使用了 Lottie 来实现动画,但是没有正确的关闭导致的。

Lottie 库的 issue 列表里面有人提到了这个情况:

提出问题:

I recently did some benchmarking on an app which uses lottie to do some animations (autoplay and looping). I noticed that there is quite some CPU usage when the app is in the background and tried to investigate.

It seems to me looping animations do not pause/stop when the containing LottieAnimationView is off screen, and/or the Activity is paused.

I believe this is due to the cleanup code being only in onDetachedFromWindow() which is not necessarily being called once the Activity goes into a paused state and most definitely not, when the view is simply not visible (GONE, INVISIBLE ) anymore.

解决方法:

Overriding LottieAnimationView and doing the following solves the visibility issue for me and Lottie is paused when not visible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility == VISIBLE && wasAnimatingWhenVisibilityChanged) {
resumeAnimation();
} else {
if (isAnimating()) {
wasAnimatingWhenVisibilityChanged = true;
pauseAnimation();
} else {
wasAnimatingWhenVisibilityChanged = false;
}
}
}

总之就是 : 当 App 不可见的时候,停止所有的动画:pauseAnimation!!!

本文其他地址

由于博客留言交流不方便,点赞或者交流,可以移步本文的知乎或者掘金页面
知乎 - Android 中的“后台无效动画“行为分析
掘金 - Android 中的“后台无效动画“行为分析

关于我 && 博客

一个人可以走的更快 , 一群人可以走的更远

微信扫一扫

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK