关于TimeInterpolator的冷门用法

关键词:插值器、TimeInterpolator、帧率、动画

1、前情提要

在一款屏保应用的开发中,需要防止屏幕因长时间显示同一颜色而老化,需要让TextView和静态的图片适当移动、放大,但是不能让用户察觉UI的元素在动,需要控制在30min完成一轮循环。

2、动画实现

这里简单创建一个实现30min无限左右平移100px的动画:

java 复制代码
private final static int DURATION_30_MIN_MILL = 30 * 60 * 1000;
private void startAnimator() {
    ObjectAnimator animator = ObjectAnimator.ofFloat(findViewById(R.id.hello), "translationX", 100);
    animator.setInterpolator(new LinearInterpolator());
    animator.setDuration(DURATION_30_MIN_MILL);
    animator.setRepeatCount(ObjectAnimator.INFINITE);
    animator.setRepeatMode(ObjectAnimator.REVERSE);
    animator.start();
}

3、LinearInterpolator问题

如果这样实现,看起来View几乎没有动,但是应用还是以满帧率去做动画的,CPU占用十分惊人:

前三个关联进程占用加起来跑了满1个多CPU核心

4、TimeInterpolator使用

java 复制代码
private final static int FRAME_COUNT = 360; // -> 360frame / 3600s = 0.1fps
private final static int DURATION_30_MIN_MILL = 30 * 60 * 1000;
private final TimeInterpolator mTimeInterpolator = input -> (int) (input * FRAME_COUNT) / (FRAME_COUNT * 1.0f);

private void startAnimator() {
    ObjectAnimator animator = ObjectAnimator.ofFloat(findViewById(R.id.hello), "translationX", 100);
    animator.setInterpolator(mTimeInterpolator);
    animator.setDuration(DURATION_30_MIN_MILL);
    animator.setRepeatCount(ObjectAnimator.INFINITE);
    animator.setRepeatMode(ObjectAnimator.REVERSE);
    animator.start();
}

使用TimeInterpolator后,可以计算出动画帧率只有0.1fps,CPU使用率也降低到个位数:

总结

水文一篇,该使用场景比较少,如果有长时间移动和帧速率控制的需求,使用TimeInterpolator可能对你有所帮助。

相关推荐
GEEKVIP2 小时前
手机使用技巧:8 个 Android 锁屏移除工具 [解锁 Android]
android·macos·ios·智能手机·电脑·手机·iphone
model20054 小时前
android + tflite 分类APP开发-2
android·分类·tflite
彭于晏6894 小时前
Android广播
android·java·开发语言
与衫5 小时前
掌握嵌套子查询:复杂 SQL 中 * 列的准确表列关系
android·javascript·sql
500了11 小时前
Kotlin基本知识
android·开发语言·kotlin
人工智能的苟富贵12 小时前
Android Debug Bridge(ADB)完全指南
android·adb
小雨cc5566ru17 小时前
uniapp+Android面向网络学习的时间管理工具软件 微信小程序
android·微信小程序·uni-app
bianshaopeng18 小时前
android 原生加载pdf
android·pdf
hhzz18 小时前
Linux Shell编程快速入门以及案例(Linux一键批量启动、停止、重启Jar包Shell脚本)
android·linux·jar
火红的小辣椒19 小时前
XSS基础
android·web安全