【问题标题】:Android studio - animating adding to 10 using textview?Android studio - 使用 textview 动画添加到 10?
【发布时间】:2021-12-28 12:02:55
【问题描述】:

我可以在 1 秒内使用 textview 制作分数变量增加的动画吗?

【问题讨论】:

    标签: android-studio animation textview


    【解决方案1】:

    我强烈建议您使用 this question 签出,对于不同的情况,有多种不同的方法。

    你可以试试:

    //You can animate from a specified starting point till the final score
    val valueAnimator = ValueAnimator.ofInt(initialScore, finalScore)
    
    //Here you set the duration of the animation in milliseconds
    valueAnimator.duration = 1000 
    
    
    valueAnimator.addUpdateListener { valueAnimator -> 
        //The textview gets updates as the value animator's value updates
        textView.text = valueAnimator.animatedValue.toString() 
    
    }
    
    //Here you just start the animation
    valueAnimator.start() 
    

    这也可以重新用于它自己的方法;)

    【讨论】:

      【解决方案2】:

      另一种效果很好的方法是使用 new Handler(Looper.getMainLooper()).postDelayed(new Runnable() ... 例如:

      private void wonBlockIncreaseScore() {
              int oldScore = score;
              score = score + 50;
              int timeOverWhichAnimationShouldRun = 1000; //ms
              int equalGap = (timeOverWhichAnimationShouldRun / (score-oldScore));
              //equal gap...
              for (int i = oldScore; i < score; i++) {
                  int finalI = i;
                  new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                      @Override
                      public void run() {
                          scoreTextView.setText(String.valueOf(finalI+1));
                      }
                  }, (equalGap * i));
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-31
        • 1970-01-01
        • 2021-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-27
        相关资源
        最近更新 更多