This issue is simple to operate into because animator frequently have multiple moving parts. Suppose you are establishing an animation with three moving parts:
Should you set just one layer around the parent ViewGroup you’d really have nonstop cache invalidation because (in general) the ViewGroup is continually altering because of its children. Each one of the individual Sights, however, are simply converting. Within this situation, it would be better to set a hardware layer on each one of the child Sights (and none around the parent).
To reiterate, since i have did not appreciate this initially: it’s frequently appropriate to create hardware layers on multiple sights so that not one of them invalidate throughout the animation.
“Show hardware layers updates” is a superb developer tool for monitoring lower this problem. It flashes sights eco-friendly each time a view renders a hardware layer. It ought to only flash once once the animation starts (i.e. the first layer render). However, in case your view is solid eco-friendly the whole animation, then you need invalidation problems.
Third, hardware layers occupy GPU memory and clearly you wouldn’t want any memory leaks. Therefore, you just use hardware layers at the appropriate interval, like during animator.
All having said that – there is no hard rules here. The Android rendering product is complex and frequently surprises me. As with every performance problems, measurement is essential. The “profile GPU rendering” and “show hardware layers updates” developer configurations are ideal for figuring out if layers are helping or harming your cause.
I authored an example application that demonstrates fundamental hardware layer usage. You will get the origin code here.
Here you go running on my small Universe Nexus (a classic, slow device) with profile GPU rendering enabled:
Without hardware layers this straightforward animation is terrible. It’s constantly over the eco-friendly line meaning it appears as though a janky mess. In comparison, the version with hardware layers stays underneath the eco-friendly line the whole time – great!
The 3rd situation shows the possibility of invalidation throughout an animation with hardware layers. Many of the performance gains are wiped out because of bad use of hardware layers.
(There’s some oddity here – whether it’s invalidating, why is not it as being slow as not getting hardware layers whatsoever? I do not completely understand it myself, but apparently there are several optimizations hardware layers make entirely possible that are beneficial even when they have to redraw each and every step. Still – it’s better for their services properly.)
The moral from the story: hardware layers could be ideal for animation but rely on them sensibly!