问题描述
我想测试我的自定义组件UI渲染性能.我使用以下测试用例来检查渲染性能.
I want to test my Custom component UI rendering performance. I used the following test case to check the rendering performance.
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;
}
使用此代码,我可以测试布局渲染时序.这样我更改了布局设计以获得更好的性能.现在,我正在创建一个具有多个布局和组件(如图像,文本视图等)的Custom视图类.我将在运行时附加该类,并且组件将在运行时根据服务器的响应进行创建.我不会在XML中附加此自定义组件.现在,我要测试此自定义视图的渲染性能.请向我建议使用任何工具或任何方式来计算自定义视图的UI呈现时间.
Using of this code I can test the layout render timing. So that I changed the layout design for better performance. Now I am creating a Custom view class with multiple layouts and components like images, textviews and etc. I will attach the class at run time and components will create on the run time based on the server response. I will not attach this custom component in XML. Now I want to test the rendering performance of this custom view. Please suggest me any tools or any way to calculate the UI rendering time for the custom view.
推荐答案
嗯,有很多方法可以做到这一点:
Well, there are many ways to do this:
- 您可以在此处 查看系统跟踪文档.
- 您应该查看 Jetpack基准测试库
- You may check the system tracing documentation here
- You should check out the Jetpack Benchmarking Library
如果您只想要与您正在做的事情类似的东西,那么我想这就是您正在寻找的东西(只需创建一个CustomView对象而不是夸大XML布局即可):
If you just want something similar to what you are doing, then i guess this is what you are looking for (just create a CustomView object instead of inflating an XML layout) :
private long getLayoutTime() {
final Context targetContext = getInstrumentation().getTargetContext();
final long startTime = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
final View view = CustomView(targetContext)
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;
}
这篇关于如何测试自定义视图的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!