2

I have a gridView with 20 custom components shown on screen at a time (as the grid view items). Scrolling the view is very choppy, and I'm trying to determine why. My systrace (shown below) shows a huge amount of time spent in performTraversals and getDisplayList (about 40ms), but I can't tell what they're doing. I've added custom tracing for my onMeasure, onLayout, and onDraw. Those are all tiny by comparison.

I'm not sure where to go next to determine what's taking up all the time...

enter image description here

jww
  • 97,681
  • 90
  • 411
  • 885
rsteckler
  • 305
  • 2
  • 8
  • Go to the AOSP, look at the code of those functions, and figure out what they're doing and why it takes so long? Then see if you can optimize to avoid it? – Gabe Sechan Jan 16 '15 at 02:15
  • That's all View hierarchy work. Have you looked at your UI with hierarchyviewer? (http://developer.android.com/tools/debugging/debugging-ui.html) – fadden Jan 16 '15 at 16:47

1 Answers1

5

So this ended up being pretty specific. The problem was that I need to invalidate items in a GridView when the GridView is scrolled because they're custom controls that need to redraw themselves. From the GridView OnScroll, I was calling GridView.invalidateViews(). That, as it turns out, is VERY slow. I replaced it with the following:

for(int visiblePosition = 0; visiblePosition <= mGridView.getLastVisiblePosition() - mGridView.getFirstVisiblePosition(); visiblePosition++) {
    mGridView.getChildAt(visiblePosition).invalidate();
}

That little loop is insanely faster than invalidateViews. I didn't look through the code to determine why.

So, how did I determine this was the problem? All of these fancy tools didn't really help. Instead, I used some good ol' fashioned commentjitsu. I kept paring down everything that happened when I scrolled, and that's the line that made the difference. My frames went from ~50ms to ~8ms.

rsteckler
  • 305
  • 2
  • 8