Skip to content Skip to sidebar Skip to footer

Ondraw For A Custom View Is Looping Infinitely Android

OnDraw function for my custom View is being called infinitely and is looping !! What could be possible reason?? Here is my custom view:- public class Balls extends View{ priva

Solution 1:

There isn't an infinite loop here. What is going on is the OS is redrawing your activity as fast as possible. When your activity gets redrawn it redraws all of its children views. As your code has very little computation, from what I can see here, it is running very fast and is probably redrawing at a rate >30 FPS. Your log message makes it appear as if there is an infinite loop when there isn't. In fact there isn't even a loop inside your onDraw method.

To illustrate what is going on try this. Add a protected member drawCount to your Balls class and set it to 0:

protect int drawCount = 0;

Then append drawCount to the end of your onDraw log message

publicvoidonDraw(Canvas canvas){
       drawCount++;
       Log.w(this.getClass().getName(),"onDraw of Balls called. Total draws:" + Integer.toString(drawCount));
  ...
  }

What you should see is each log message will display a different drawCount.

If you want to get fancy and calculate the framerate of your app you could measure the time since the first draw and then divide the drawCount by how much time has passed which would give you a estimate of your activities framerate.

Solution 2:

onDraw(Canvas) called too often like in an infinite loop, is not normal. normally it should be called 1 to 3 times, if there is no following invalidate or layout changes. reasons for infinite loop maybe: 1, you called invalidate or postInvalidate some where. 2, parent or sibling layout is changing all the time. 3, View.LAYER_TYPE_SOFTWARE is used, es. setLayerType(View.LAYER_TYPE_SOFTWARE, null). it is notable that LAYER_TYPE_SOFTWARE will cause onDraw() to be called, like in a loop.

Solution 3:

onDraw() get's call at invalidate. Invalidate() get's called when the view or it's parent feel the need to change and have to change it's state.

Solution 4:

For me that was happening because i had some code that was modifying UI inside overridden DispatchDraw..

Post a Comment for "Ondraw For A Custom View Is Looping Infinitely Android"