How to set timer in android?

后端 未结 21 942
渐次进展
渐次进展 2020-11-22 00:51

Can someone give a simple example of updating a textfield every second or so?

I want to make a flying ball and need to calculate/update the ball coordinates every se

21条回答
  •  自闭症患者
    2020-11-22 01:31

    If you have delta time already.

    public class Timer {
        private float lastFrameChanged;
        private float frameDuration;
        private Runnable r;
    
        public Timer(float frameDuration, Runnable r) {
            this.frameDuration = frameDuration;
            this.lastFrameChanged = 0;
            this.r = r;
        }
    
        public void update(float dt) {
            lastFrameChanged += dt;
    
            if (lastFrameChanged > frameDuration) {
                lastFrameChanged = 0;
                r.run();
            }
        }
    }
    

提交回复
热议问题