简体   繁体   English

Android中的ValueAnimator以及如何使其每秒增加一

[英]ValueAnimator in Android and how to make it increase by one each second

I'm working on this android app...and I have a textview, where I'd like to count UP. 我正在使用这个android应用程序...并且我有一个textview,在这里我想数一数。 In this textview I have the statement "This page was refreshed X seconds ago". 在此文本视图中,我声明“此页面X秒钟前已刷新”。 Every 20 seconds I'd like it to restart its countup from 0. 我希望每20秒从0重新开始其计数。

I've implemented a function which uses the ValueAnimator class. 我已经实现了一个使用ValueAnimator类的函数。 However, I'm not sure how to make the ValueAnimator increase the integer value by 1 every second. 但是,我不确定如何使ValueAnimator每秒钟将整数值增加1。 I'd like it to count up like a clock's second hand 1...2..3...4..5......20. 我希望它像钟表的秒针一样计数1 ... 2..3 ... 4..5 ...... 20。 Right now its counting up incredibly fast...and I want to slow it waaayyyy down. 现在,它的计数速度非常快...我想放慢它waaayyyy。 1 count per second. 每秒计数1次。

Please how do I do this? 请我怎么做? I've looked at some of the method calls for ValueAnimator, and I'm not sure how to slow it down...right now it's so fast. 我已经看过一些对ValueAnimator的方法调用,但是我不确定如何降低它的速度……现在它是如此之快。

Also, where would I add a method call, lets say, everytime the counter gets to 20 seconds? 另外,我可以在每次计数器达到20秒时在哪里添加方法调用? Lets say I want to call refreshView() when it gets to 20, before it starts all over. 可以说我想在它重新开始之前在达到20时调用refreshView()。

Thank you for your time, and thanks for your help. 感谢您的时间,也感谢您的帮助。

The ValueAnimator class: https://developer.android.com/reference/android/animation/ValueAnimator ValueAnimator类: https : //developer.android.com/reference/android/animation/ValueAnimator

In my method I have: 用我的方法,我有:

    ValueAnimator valueAnimator = ValueAnimator.ofInt(initialValue, finalValue);
    valueAnimator.setDuration(10000);

    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            textview.setText("This page was refreshed " + valueAnimator.getAnimatedValue().toString() + " seconds ago");
        }
    });
    valueAnimator.start();
    // Repeat 100 times
    valueAnimator.setRepeatCount(100);

ValueAniamtor is used for animation. ValueAniamtor用于动画。 Animation are refreshed at very high rate. 动画刷新速度非常快。 I don't think that you can count seconds with a valueAnimator. 我不认为您可以使用valueAnimator数秒。

In one of my apps i use a Timer to refresh periodically a textView. 在我的一个应用程序中,我使用计时器定期刷新textView。

final TextView textview=findViewById(R.id.tv);
    new Timer().scheduleAtFixedRate(new TimerTask() {
        int value=0; //start at 0
        @Override
        public void run() {
            value++ ; 
            runOnUiThread(new Runnable() { //only the main thread can touch his views
                @Override
                public void run() {
                    textview.setText("This page was refreshed " + value + " seconds ago"); //refresh text

                }
            });
        }
    }, 0, 1000); //reschedule every 1000 milliseconds


}

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

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