如何在初始估算值和反向重复之间延迟

发布于 2025-01-21 21:46:38 字数 1758 浏览 5 评论 0原文

我有一个两行TextView,我试图执行以下操作:

  1. 当数据到达时,淡出第二行
  2. 更改第二行中显示的数据
  3. 淡出的数据淡入

我当前正在使用的 第二行。以下代码来实现这一目标:

ValueAnimator alphaAnim = ValueAnimator.ofInt(255,0).setDuration(1000);
alphaAnim.setRepeatMode(ValueAnimator.REVERSE);
alphaAnim.setRepeatCount(1);
alphaAnim.setStartDelay(500);

alphaAnim.addUpdateListener(valueAnimator -> {
            int alpha = (int) valueAnimator.getAnimatedValue();
            int newColor = binding.ForegroundSpanText.getCurrentTextColor() & 0x00ffff | (alpha <<24);
            SpannableString tempStringHolder = binding.getAnimString();
            if(fadingSpan !=null){
                tempStringHolder.removeSpan(fadingSpan);
            }
            fadingSpan = new ForegroundColorSpan(newColor);
            tempStringHolder.setSpan(fadingSpan, tempStringHolder.toString().indexOf("\n"), tempStringHolder.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            if(Integer.toHexString(newColor).equals("1000000") && !tempStringHolder.toString().equals("This is my test text with lots of text\n and this is the new text that will animate in!!")){
                animStringHolder = new SpannableString("This is my test text with lots of text\n and this is the new text that will animate in!!");
                binding.setAnimString(animStringHolder);
                binding.executePendingBindings();
            }else{
                binding.setAnimString(tempStringHolder);
            }
        });

我当前遇到的问题是:

  • 初始文本和我更改的文本之间的更改仍然可见逆转。我意识到这可以使用第二个动画师来完成,但希望有人可能对没有这样做有所了解。
  • 另外,如果有人有更好的方法来设置数据/检查第一个动画仪的终点是否达到了很棒,我试图覆盖onAnimationEnd()函数,但是当整个动画结束时发生,这意味着在反向重复。

I have a two line textview where I am attempting to do the following:

  1. when data arrives, fade out the second line only
  2. change the data presented in the second line
  3. fade the second line back in

I am currently using the following code to accomplish this:

ValueAnimator alphaAnim = ValueAnimator.ofInt(255,0).setDuration(1000);
alphaAnim.setRepeatMode(ValueAnimator.REVERSE);
alphaAnim.setRepeatCount(1);
alphaAnim.setStartDelay(500);

alphaAnim.addUpdateListener(valueAnimator -> {
            int alpha = (int) valueAnimator.getAnimatedValue();
            int newColor = binding.ForegroundSpanText.getCurrentTextColor() & 0x00ffff | (alpha <<24);
            SpannableString tempStringHolder = binding.getAnimString();
            if(fadingSpan !=null){
                tempStringHolder.removeSpan(fadingSpan);
            }
            fadingSpan = new ForegroundColorSpan(newColor);
            tempStringHolder.setSpan(fadingSpan, tempStringHolder.toString().indexOf("\n"), tempStringHolder.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            if(Integer.toHexString(newColor).equals("1000000") && !tempStringHolder.toString().equals("This is my test text with lots of text\n and this is the new text that will animate in!!")){
                animStringHolder = new SpannableString("This is my test text with lots of text\n and this is the new text that will animate in!!");
                binding.setAnimString(animStringHolder);
                binding.executePendingBindings();
            }else{
                binding.setAnimString(tempStringHolder);
            }
        });

The issues I am currently running into are:

  • The change between the initial text and the text I change to is still visible because the reverse animation happens immediately, so I am looking for a way to add a delay between the reversal. I realize that this could be done with a second animator but was hoping someone might have some idea of how to accomplish this without.
  • Also if anyone has a better way to set the data/check if the end point of the first animator has been reached that would be awesome, I tried to override the onAnimationEnd() function but that occurs when the entire animation finishes, meaning after the reverse repeat.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴意少年 2025-01-28 21:46:38

In further research I found onAnimationRepeat() which is part of AnimationListenerAdapter 。因此,将其作为我最初发布的一部分的代码将是:

alphaAnim.addListener(new AnimatorListenerAdapter(){
            @Override
            public void onAnimationEnd(Animator animation){
                System.out.println("Finished");
            }
            @Override
            public void onAnimationRepeat(Animator animation){
                animStringHolder = new SpannableString("This is my test text with lots of text\n and this is the new text that will animate in!!");
                binding.setAnimString(animStringHolder);
            }
        });

In further research I found onAnimationRepeat() which is part of AnimationListenerAdapter. So the code to make this a part of what I posted originally would look like:

alphaAnim.addListener(new AnimatorListenerAdapter(){
            @Override
            public void onAnimationEnd(Animator animation){
                System.out.println("Finished");
            }
            @Override
            public void onAnimationRepeat(Animator animation){
                animStringHolder = new SpannableString("This is my test text with lots of text\n and this is the new text that will animate in!!");
                binding.setAnimString(animStringHolder);
            }
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文