How to create a count-up effect for a textView in Android

后端 未结 6 1496
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 07:17

I am working on an app that counts the number of questions marks in a few paragraphs of text.

After the scanning is done (which takes no time at all) I would love to

相关标签:
6条回答
  • 2020-12-13 07:53

    I've used a more conventional Android-style animation for this:

            ValueAnimator animator = new ValueAnimator();
            animator.setObjectValues(0, count);
            animator.addUpdateListener(new AnimatorUpdateListener() {
                public void onAnimationUpdate(ValueAnimator animation) {
                    view.setText(String.valueOf(animation.getAnimatedValue()));
                }
            });
            animator.setEvaluator(new TypeEvaluator<Integer>() {
                public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
                    return Math.round(startValue + (endValue - startValue) * fraction);
                }
            });
            animator.setDuration(1000);
            animator.start();
    

    You can play with the 0 and count values to make the counter go from any number to any number, and play with the 1000 to set the duration of the entire animation.

    Note that this supports Android API level 11 and above, but you can use the awesome nineoldandroids project to make it backward compatible easily.

    0 讨论(0)
  • 2020-12-13 07:56

    Use a worker thread to do the waiting and update your UI thread.

    You could use an AsyncTask, though it might be an overkill for this job. If you use this, In the doInBackground() loop over the number of sleep periods and after every sleep period, update the count in the UIthread.

    There you go! Slukain just gave you the working code :P

    0 讨论(0)
  • 2020-12-13 08:01

    Maybe try changing the for loop to something like:

    int count = 0;
    while (count != sent) {
        if ((SystemClock.uptimeMillis() - freezeTime) > 500) {
            count++;
            sentScore.setText("" + count);
            freezeTime = SystemClock.uptimeMillis();
        }
    }
    
    0 讨论(0)
  • 2020-12-13 08:02

    Use TextSitcher

    for the best effects. It will transform the text softly.

    When coming to the change of the text use the below Code.

    > int number = 0;
    >         Timer obj = new Timer();
    >         TimerTask tt = new TimerTask() {
    >                       @Override           public void run() {
    >               // TODO Auto-generated method stub
    >               textView.setText(number++);
    >               if(number < score)
    >               obj.schedule(tt, 200);          }       };
    >         obj.schedule(tt, 200);
    
    0 讨论(0)
  • 2020-12-13 08:03

    The question is very old, but I am posting this so that it would help someone else. I have used these 2 amazing libraries.

    Try them

    1. https://github.com/MasayukiSuda/CountAnimationTextView

    Or 2. https://github.com/robinhood/ticker

    Hope this helps :) Happy Coding!!!

    0 讨论(0)
  • 2020-12-13 08:08

    Try this:

    private int counter = 0;
    private int total = 30; // the total number
    //...
    //when you want to start the counting start the thread bellow.
        new Thread(new Runnable() {
    
                    public void run() {
                        while (counter < total) {
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            t.post(new Runnable() {
    
                                public void run() {
                                    t.setText("" + counter);
    
                                }
    
                            });
                            counter++;
                        }
    
                    }
    
                }).start();
    
    0 讨论(0)
提交回复
热议问题