简体   繁体   English

如何制作减少数值的函数(例如,从800毫秒减少到200毫秒)

[英]How can I make a function that decreases a number (from 800 milliseconds to 200 milliseconds for example)

the timer is set in a period of 800 milliseconds, so always RandomPositions() will execute the inner code every 800 milliseconds (the period). 计时器的设置时间为800毫秒,因此,总是RandomPositions()将每800毫秒(该时间段)执行一次内部代码。 Now, what I want is to decrease this number by 50 every 20 seconds or 20,000 milliseconds until it gets the period to 200 milliseconds. 现在,我想要的是每20秒或20,000毫秒将此数字减少50,直到周期达到200毫秒。

GOAL - IN THE GAME THIS MUST BE INCREASING THE SPEED GRADUALLY EVERY 20 SECONDS. 目标-在游戏中,必须每20秒逐步增加速度。

example: first execution - 800 milliseconds, second execution - 750 milliseconds, third execution - 700 milliseconds, and so on... 例如:第一次执行-800毫秒,第二次执行-750毫秒,第三次执行-700毫秒,依此类推...

What I think is that I could insert a function of type Integer (instead of 800), that can make this job of decreasing from 800 to 200 miliseconds. 我的想法是,我可以插入一个Integer类型的函数(而不是800),这会使这项工作从800毫秒减少到200毫秒。

How can I make this function? 我该如何做这个功能? or is there any other solution? 还是有其他解决方案?

public void RandomPositions() {

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask()
        {
            public void run() {

                getWindowManager().getDefaultDisplay().getMetrics(displaymatrics);
                float dx = r.nextFloat() * displaymatrics.widthPixels/1.2f;
                float dy = r.nextFloat() * displaymatrics.heightPixels/1.2f;
                button.animate().x(dx).y(dy).setDuration(0);


            }
        }, 0, 800);  // first value = Delay   , Second value = Period(what I need to change)

I wouldn't use a Timer for that, as the interval changes all the time. 我不会为此使用计时器,因为间隔一直在变化。

In vanilla Android I would probably use a Handler 在香草Android中,我可能会使用处理程序

Int interval = 800; // milliseconds
Handler handler = new Handler()
Runnable runnable = new Runnable() { 

   @Override void run() {

        //
        // do your logic here
        //

        // decrement interval by 20 milliseconds 
        // if interval - 20 is greater than 200
        interval = interval - 20 > 200 ? interval - 20 : interval;


        // Request an execution of this runnable with the new value of interval
        handler.postDelayed(runnable, interval);
   }
}


public void start() {
   handler.postDelayed(runnable, interval);
}

public void stop() { 
   handler.removeCallbacks(runnable);
}

Make sure you call removeCallbacks whenever you want to stop it, or whenever the user is leaving the activity/context where this is running. 确保无论何时要停止它,或者每当用户将活动/上下文离开运行时,都调用removeCallbacks

Here's a solution that uses a timer. 这是使用计时器的解决方案。 If you don't want to call the method in a loop then just subtract the delay parameter by 50 each time you call it. 如果您不想在循环中调用该方法,则每次调用时只需将delay参数减去50。

    public static void main(String[] args) {                
        Timer timer = new Timer();              
        for(int i = 800; i >= 200; i-=50)
        {
            runTimer(timer, i);         
        }   
    }   

    private static void runTimer(Timer timer, int delay) {
        timer.schedule(new TimerTask()
        {
            public void run() {
            }                
        }, 0, delay);  
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM