I have an app with a digital clock in the corner of the page in the format HH:mm:ss
e.g. 10:35:23
This this done with a label that has its text property updated every second.
It all works fine but, using the Android GPU profiler and 'show view update' settings, I can see that the whole page is being redrawn every second!
Playing around further the whole page even gets redrawn with a seemingly harmless colour change!!
I have tried taking the Label out into a AbsoluteLayout in the hopes that the measure/layout phase didn't spill out into all the other parent views - but it does
Does anyone know of a nice way to stop this?
The only alternative I have found is drawing the text in a SKCanvas and having it update that way - only the canvas is redrawn in this instance.
Answers
Hi @BenMagistris
On Android, if you update the
TextView
in the Activity,TextView
will request it's parent to Measure/Layout/Draw,and it's parent will call theTextView
's grandparent to Measure/Layout/Draw, we know that there is a view tree in the activity. So, if you update the label, the whole page will be updated. So you can't stop it.Hi @robbit,
Thank you for your answer.
Do you know of any alternatives to stop the whole page redrawing? If the view hierarchy is complex a Measure/Layout/Draw cycle has a performance impact if it is called every second. Do you know of any structures I could draw text in and the update not spill out into the rest of the view?
I have only found one alternative which is drawing the text in a SKCanvas view.
I've never had to do it (although might in future when I get to optimising pages), but I recall this being discussed either in a Xamarin University class, Lightning Lecture, Guest Lecture or Evolve session (sorry, cannot recall which). In that discussion, it was said that it's possible to override part of the Measure/Layout cycle to prevent it from iterating all the way up the UI hierarchy. By doing that, unnecessary measurement/layout/draw can be avoided. I wonder if it was Jason Smith's session on performance (see this ), but I don't have time to check now.
@JohnHardman
Funnily enough I have watched that video by Jason Smith, but unfortunately nothing is said about overriding the Measure/Layout cycle. The only thing mentioned that could provide a solution is creating a custom view. I'd imagine it would allow you to control how the view handled updates, but it seems a little over-kill for my example, which is just for a bit of text.
I wonder where I saw it - it might have been Xamarin University, but I had a sneaking suspicion it was Jason Smith. Will update if I remember where it was.