WheelView类里面
` /**
- 计算最大length的Text的宽高度
*/
private void measureTextWidthHeight() {
Rect rect = new Rect();
for (int i = 0; i < adapter.getItemsCount(); i++) {
String s1 = getContentText(adapter.getItem(i));
paintCenterText.getTextBounds(s1, 0, s1.length(), rect);
int textWidth = rect.width();
if (textWidth > maxTextWidth) {
maxTextWidth = textWidth;
}
}
paintCenterText.getTextBounds("\u661F\u671F", 0, 2, rect); // 星期的字符编码(以它为标准高度)
maxTextHeight = rect.height() + 2;
itemHeight = lineSpacingMultiplier * maxTextHeight;
}
在数据量较大的情况下效率太低,测试列表20000条数据出现明显延迟,是否可以优化成
private void measureTextWidthHeight() {
String temp = "";
Rect rect = new Rect();
int count = adapter.getItemsCount();
for (int i = 0; i < count; i++) {
String s1 = getContentText(adapter.getItem(i));
if (s1.length() > temp.length()) {
temp = s1;
}
}
paintCenterText.getTextBounds(temp, 0, temp.length(), rect);
maxTextWidth = rect.width();
paintCenterText.getTextBounds("\u661F\u671F", 0, 2, rect); // 星期的字符编码(以它为标准高度)
maxTextHeight = rect.height() + 2;
itemHeight = lineSpacingMultiplier * maxTextHeight;
}
`
减少宽度测量次数。
1条答案
按热度按时间6yjfywim1#
也可以添加一个设置最大宽度的接口,这样也可以不进行遍历计算。