BiuTextView替代TextView

Android 3XCN.com站长2023年3月23日 am9:14发布6个月前更新
0
导航号,我的单页导航
目录

前言

Android 高版本后,TextView走马灯耗CPU。

使用BiuTextView替代TextView来实现跑马灯

注意,下面测试还要其他应用使用surfaceflinger,所以很高,我们只是对比同一环境下,BiuTextView和TextView跑马灯的情况。

正文

说明:Demo中只放了1个TextView或BiuTextView进行跑马灯

TextView跑马灯

只要当前界面有TextView的走马灯,CPU占用就高起来。

可以通过

//比如过滤:com.biumall.demo
adb shell top | grep demo
  1. grep跟当前电脑环境有关

  2. 这里只看surfaceflinger和走马灯CPU占用情况

命令查看某个进程的CPU占用

  103    surfaceflinger
 37.5    com.biumall.demo+
  104    surfaceflinger
 36.0    com.biumall.demo+
  103    surfaceflinger
 36.6    com.biumall.demo+
  108    surfaceflinger
 33.6    com.biumall.demo+
 90.0    surfaceflinger
 38.0    com.biumall.demo+
  108    surfaceflinger
 37.6    com.biumall.demo+
  107    surfaceflinger
 37.3    com.biumall.demo+
  105    surfaceflinger
 36.6    com.biumall.demo+
  103    surfaceflinger
 38.0    com.biumall.demo+
 73.3    surfaceflinger
 40.3    com.biumall.demo+
 53.0    surfaceflinger
 26.6    com.biumall.demo+
 65.0    surfaceflinger
 39.6    com.biumall.demo+
 72.6    surfaceflinger
 44.6    com.biumall.demo+
 73.0    surfaceflinger
 42.3    com.biumall.demo+
 72.6    surfaceflinger
 42.6    com.biumall.demo+
 80.6    surfaceflinger
 39.6    com.biumall.demo+
 73.3    surfaceflinger
 48.0    com.biumall.demo+
 73.0    surfaceflinger
 44.6    com.biumall.demo+
 78.3    surfaceflinger
 45.6    com.biumall.demo+
 75.0    surfaceflinger
 42.6    com.biumall.demo+
  103    surfaceflinger
 31.6    com.biumall.demo+
 66.0    surfaceflinger
 19.0    com.biumall.demo+
 86.0    surfaceflinger
 39.0    com.biumall.demo+
  101    surfaceflinger
 37.3    com.biumall.demo+
  106    surfaceflinger
 35.0    com.biumall.demo+
  106    surfaceflinger
 33.6    com.biumall.demo+
  106    surfaceflinger
 36.0    com.biumall.demo+
  109    surfaceflinger
 38.0    com.biumall.demo+
  107    surfaceflinger
 39.0    com.biumall.demo+
  106    surfaceflinger
 38.6    com.biumall.demo+
  108    surfaceflinger
 32.0    com.biumall.demo+
  107    surfaceflinger
 28.6    com.biumall.demo+
 66.6    surfaceflinger
 20.6    com.biumall.demo+
  108    surfaceflinger
 34.0    com.biumall.demo+
  107    surfaceflinger
 33.6    com.biumall.demo+
  106    surfaceflinger
 33.3    com.biumall.demo+
  107    surfaceflinger
 33.6    com.biumall.demo+

开启走马灯后,当前com.biumall.demo占用持续在40%左右,而且surfaceflinger也很高。

BiuTextView跑马灯

使用《BiuTextView.aar》中自定义的BiuTextView

 43.3    surfaceflinger
  3.3    com.biumall.demo+
 20.6    surfaceflinger
  3.6    com.biumall.demo+
 37.6    surfaceflinger
  6.3    com.biumall.demo+
 37.0    surfaceflinger
  5.6    com.biumall.demo+
 38.0    surfaceflinger
  6.0    com.biumall.demo+
 38.3    surfaceflinger
  6.3    com.biumall.demo+
 39.3    surfaceflinger
  6.3    com.biumall.demo+
 61.3    surfaceflinger
  6.6    com.biumall.demo+
 61.6    surfaceflinger
  6.6    com.biumall.demo+
 61.6    surfaceflinger
  6.3    com.biumall.demo+
 33.3    surfaceflinger
  3.3    com.biumall.demo+
 62.3    surfaceflinger
  5.6    com.biumall.demo+
 61.3    surfaceflinger
  6.0    com.biumall.demo+
 59.3    surfaceflinger
  5.6    com.biumall.demo+
 58.0    surfaceflinger
  5.6    com.biumall.demo+
 61.3    surfaceflinger
  6.0    com.biumall.demo+
 61.3    surfaceflinger
  6.3    com.biumall.demo+
 61.6    surfaceflinger
  6.0    com.biumall.demo+
 63.6    surfaceflinger
  6.0    com.biumall.demo+
 61.0    surfaceflinger
  6.0    com.biumall.demo+
 33.6    surfaceflinger
  3.6    com.biumall.demo+
 60.6    surfaceflinger
  6.3    com.biumall.demo+
 61.6    surfaceflinger
  6.0    com.biumall.demo+
 61.0    surfaceflinger
  6.0    com.biumall.demo+
 60.3    surfaceflinger
  5.6    com.biumall.demo+
 61.3    surfaceflinger
  6.3    com.biumall.demo+
 62.3    surfaceflinger
  6.6    com.biumall.demo+
 38.6    surfaceflinger
  6.3    com.biumall.demo+
 38.0    surfaceflinger
  6.6    com.biumall.demo+
 25.0    surfaceflinger
  3.3    com.biumall.demo+
 62.0    surfaceflinger
  6.0    com.biumall.demo+
 61.6    surfaceflinger
  7.6    com.biumall.demo+
 62.0    surfaceflinger
  5.6    com.biumall.demo+
 64.6    surfaceflinger
  5.6    com.biumall.demo+

测试中发现,使用SurfaceView自定义走马灯,com.biumall.demo和surfaceflinger占用较低些。

小结

虽然只是单一的一次数据,但从上面大致可以了解到。

  1. TextView跑马灯时com.biumall.demo占CPU比较高

  2. TextView跑马灯时surfaceflinger占CPU相对高

在CPU为稀缺情况下,能少占一点就尽可能少占一点。空闲CPU过少,容易导致系统卡顿。因此,如果需要跑马灯,个人推荐还是使用BiuTextView替换TextView。

复杂情况下没怎么测试,比如ListView中等列表中可能有问题。

参考文章

  1. BiuTextView.aar

版权声明 1、 本站名称: 91易搜
2、 本站网址: https://www.91es.com/
3、 本站文章: 部分来源于网络,仅供站长学习和参考,若侵权请留言
导航号,我的单页导航

暂无评论

评论审核已启用。您的评论可能需要一段时间后才能被显示。

暂无评论...