0

I have a class called GraphView.java and a activity called MainActivity.java. I want to run a timer of 40 miliseconds on MainActivity.java to control invalidate(); on GraphView.java means it should call onDraw() within 40 milisecond.

how this one is possible ?

I have tried with postinvalidate() it doesn't work in my case.

Jarrod
  • 9,349
  • 5
  • 58
  • 73
Tejas
  • 433
  • 1
  • 4
  • 17

1 Answers1

0

You can do this using timer

Timer timer = new Timer();
timer.schedule(new TimerTask() {
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                view.invalidate;
            }
         });
     } 
}, 40);
user2041902
  • 593
  • 1
  • 6
  • 21
  • dont use any Timer, it creates a new Thread under the hood, use Handler instead – pskink Jul 30 '13 at 10:37
  • @pskink Timer will be executed only after the interval is elapsed, so doesnt create performance issues I guess. I am not very sure though, please can you reason out why its not suitable? – user2041902 Jul 30 '13 at 10:42
  • because you create a new Thread and more important: every 40 ms you create a new Runnable – pskink Jul 30 '13 at 10:49
  • @pskink This won't be repeated every 40ms. I have tested it, it gets executed only once after 40ms. – user2041902 Jul 30 '13 at 10:54
  • ok, my fault, it will be run only once, but still its a bad idea to use a Timer when you can sumply call Handler.postDelayed – pskink Jul 30 '13 at 11:08
  • 1
    Yes it is correct Handler.postDelayed is better idea rather timer as and simpler – Yogesh Tatwal Jul 30 '13 at 11:24
  • @Yogesh Tatwal But there is a problem with Handler.postDelayed() If i will my plotfuntion within 40 milisecond . Total time it will take ie: Time of Execution of that function + 40 milisecond Means after executing that function it will wait for 40 miliseconds more .That I don't want . Hope you have understood my problem – Tejas Jul 30 '13 at 15:53
  • @user1844130 Please give some code. Why are you getting a TimeOutException? Unless you provide some code how can we understand why and when you are calling your plotFunction() which gives exception – user2041902 Jul 31 '13 at 05:25