Tim*_*Sim 5 performance android
在我的Android Vitas中,我有30%的用户体验缓慢的渲染。我的应用程序的UI非常复杂,因此我做了一个非常基础的项目来尝试解决此问题。但是事实证明,即使使用最简单的布局,它也相当慢。
布局是Android Studio提供作为模板的居中文本:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="slowrenderingtest.pichaipls.com.slowrenderingtest.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timerView"
android:text="00:00"
android:textSize="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
使用布局的活动每秒更改一次文本(因为这是一个计时器):
Timer updateTicks = new Timer();
updateTicks.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Random r = new Random();
timerView.setText(String.format("%02d", r.nextInt(60))+":"+
String.format("%02d", r.nextInt(60)));
}
});
}
}, 100, 1000);
Run Code Online (Sandbox Code Playgroud)
当我打开GPU配置文件时,这已经接近16ms的极限,以便在一些较慢的设备(显然是30%的用户)上绘制每一帧。只有一个TextView。但是这里有个要点:当我第一次运行该活动时,渲染时间很短,但是几秒钟后它们就会急剧增加。如果我不断触摸屏幕(屏幕上没有控件),则渲染时间仍然很短。我猜这是由于CPU / GPU处于低功耗状态(因此渲染需要更长的时间)。
我的问题是Android要点。我一直看到有关渲染时间过慢的警告(我认为遇到缓慢渲染的会话中超过5%的任何内容都会收到警告),但我不知道如何加快渲染速度。我担心这可能会影响我的应用排名,但即使使用这个非常简单的示例,仍然有太多Android用户的设备运行缓慢。
对此可以采取任何措施吗?
小智 4
我们遇到了同样的问题,经过一番挖掘,我怀疑这是由于 CPU 空闲所致?
我们的情况类似。我们有一个 Runnable,在其中我们使用 Handler 每秒(仅)更新 TextView 的文本。TextView 驻留在 RecyclerView 内的一个项目上。有趣的是,当我们滚动页面,或者有一些动画正在进行时,或者与应用程序交互时(基本上是除了滴答声之外的其他任何事情),我们不会达到 16 毫秒的标记。但让设备空闲并发出声音,每个帧需要 30-50 毫秒。
对于类似的情况,我们尝试了SO 中提到的不同解决方案。就像将视图的宽度从wrap_content更改为match_parent一样。没有帮助解决这个问题。我们尝试将 1000ms 间隔更改为 500、200、100、50、20、16、10,仅在使用 16/10ms 时看到更好的结果(CPU 一直在工作?)。
找到这篇关于 CPU 限制的文章。那么当 CPU 空闲时,可能需要更多时间来绘制帧?
如果您在 Android Studio 中查看 CPU 监视器,您会发现只要没有发生任何事情,CPU 就会变得空闲。(更高的是当有交互并且使用 Nexus 5 时)。

但看看 GPU 监视器,它看起来与屏幕上的配置文件 GPU 渲染条完全不同,实际上没有帧超过 16 毫秒标记!不确定哪个 GPU 监控是正确的。(大黄色表示有互动)。
