0

I have a simple graphics application which draws some stuff on the screen. Before, I had no problem when I was calling invalidate() in onDraw() without a check. In my application onDraw() is called many times each second (which I came to know through Logs), but I don't want to call it many times each second, as I want to update the screen after each second only. So I tried to compare previous and current seconds -- if they are the same, I won't call invalidate(); if the second changes, I will call invalidate(). Here is my code:

protected void onDraw(Canvas canvas) {         
    // Create date to check current second
    Date date = new Date();
    canvas.drawColor(Color.WHITE);

    drawArcs(canvas, mBigOval, mUseCenters[0], mPaints[0]); 

    //Get the current second 
    curSec = date.getSeconds();
    //compare if its same as lastSec which is updated in drawArcs() which i called before

    do {    
        Log.d("Graphics","curSec="+curSec+"  lastSec="+lastSec);
        curSec = date.getSeconds();
    } while(curSec == lastSec);

    Log.d("Graphics","Calling invalidate() and  count="+(++count));
    invalidate();
}  

With the above code, if my application gets launched at the 30th second then the log shows:

     06-25 10:25:52.257: D/Graphics(14711): curSec=30  lastSec=30

The above logs keep on repeating with no change in curSec and lastSec. Why is the second not updating even if I'm updating it every time the loop goes through.

Vins
  • 4,089
  • 2
  • 35
  • 50

2 Answers2

1

Your Date object was created and set to the current time when you instantiated it with:

Date date = new Date();

Every time you call date.getSeconds(), you're just getting the seconds on that same Date object, whose time hasn't changed.

That said, your real issue is that you're calling invalidate() from onDraw(), forcing another call to onDraw() (which forces another...). You don't need to do that; onDraw() is already called whenever the View becomes invalid. If you're trying to animate (at 1 fps?) you can use a Handler with postDelayed() or a SurfaceView and another thread.

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
  • Yeah , got that current time value got into that object and im using the same again,so im getting same value. But what i can do to get updated value? – Vins Jun 25 '12 at 05:16
  • You could call `new Date()` again, but that's really not the right way to solve your larger problem. – Darshan Rivka Whittle Jun 25 '12 at 05:18
  • yeah darshan i too tried that and its working. But if i dont check for this change in time i have to draw lot of times in each second.So this is better than that right? – Vins Jun 25 '12 at 05:23
  • Drawing many times per second isn't necessarily bad; it's often exactly what you want. In any case, looping for a full second in your `onDraw()` is much, much worse, especially now that you're creating a bunch of garbage (by calling `new` every time) as fast as the device is able. – Darshan Rivka Whittle Jun 25 '12 at 05:25
  • Hey darshan can i use Thread.sleep(); to suspend my execution for some time? rather than doing all the above? – Vins Jun 25 '12 at 11:14
  • @vins Yes, but that's still a bad idea: `onDraw()` is called on your UI thread. Are you trying to create an animation? If so, there are better ways. If not, then remove the `Date` stuff, the loop, and the call to `invalidate()`. (See the last paragraph of my answer, which I just updated.) – Darshan Rivka Whittle Jun 25 '12 at 20:39
  • ,my app is clock kind of application in which i should draw after each second. So i thought of using Thread.sleep(). And after using that the usage of cpu is reduced(checked with the option in ICS). So i think in my application using Thread.sleep for some half second is really useful as there is no IO at that time. – Vins Jun 26 '12 at 03:40
0

I changed the code by creating new object every time and its working now.

        do{ 
        Log.d("Filling","curSec="+curSec+"  lastSec="+lastSec);
        curSec = new Date().getSeconds();
        }while(curSec == lastSec);
Vins
  • 4,089
  • 2
  • 35
  • 50