【发布时间】:2020-12-08 09:30:33
【问题描述】:
我想测试我的自定义组件 UI 渲染性能。我使用以下测试用例来检查渲染性能。
private long getLayoutTime(int layoutRes) {
final Context targetContext = getInstrumentation().getTargetContext();
final LayoutInflater layoutInflater = LayoutInflater.from(targetContext);
final long startTime = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
final View view = layoutInflater.inflate(layoutRes, null);
view.setLayoutParams(new ViewGroup.LayoutParams(0, 0));
view.measure(View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
final int measuredHeight = view.getMeasuredHeight();
final int measuredWidth = view.getMeasuredWidth();
view.layout(0, 0, measuredWidth, measuredHeight);
}
return System.currentTimeMillis() - startTime;
}
使用此代码,我可以测试布局渲染时间。所以我改变了布局设计以获得更好的性能。现在我正在创建一个具有多个布局和组件(如图像、文本视图等)的自定义视图类。我将在运行时附加该类,组件将根据服务器响应在运行时创建。我不会在 XML 中附加这个自定义组件。现在我想测试这个自定义视图的渲染性能。请向我推荐任何工具或任何方法来计算自定义视图的 UI 渲染时间。
【问题讨论】:
标签: android automated-tests android-view android-custom-view