使用Android动画对文本视图的特定行进行动画

发布于 2025-01-20 09:53:17 字数 1335 浏览 4 评论 0原文

我想知道是否可以在文本视图的特定行上执行淡出/淡入动画。我有一个 两行文本视图,我希望“标题”保持可见,而“ data”在发生变化时淡出或淡出。我试图限制片段上的视图数量,因此将两行分成单独的文本视图并不可取。我对动画相当陌生,不确定这是否可行。

在 Cheticamps 回答后更新,

我开发了自己的解决方案的 java 版本,如果其他人正在寻找这个,我想将其发布在这里。

    ValueAnimator alphaAnim = ValueAnimator.ofInt(255,0).setDuration(1000);
        alphaAnim.addUpdateListener(valueAnimator -> {
            int alpha = (int) valueAnimator.getAnimatedValue();
            int newColor = binding.ForegroundSpanText.getCurrentTextColor() & 0x00ffff | (alpha <<24);
            System.out.println("Color: "+ Integer.toHexString(newColor));
            SpannableString tempStringHolder = binding.getAnimString();
            if(fadingSpan !=null){
                tempStringHolder.removeSpan(fadingSpan);
            }
            fadingSpan = new ForegroundColorSpan(newColor);
            tempStringHolder.setSpan(fadingSpan, 38, animStringHolder.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            binding.setAnimString(tempStringHolder);
        });
        alphaAnim.addListener(new AnimatorListenerAdapter(){
            @Override
            public void onAnimationEnd(Animator animation){
                System.out.println("Finished");
            }
        });

I am wondering if it is possible to perform a fade out/fade in animation on a specific line of a textview. I have a two line textview which I would like the "title" to stay visible while the "data" fades out and in when it changes. I am attempting to limit the number of views on my fragment so separating the two lines into separate textviews is not preferable. I am fairly new to animations and was unsure if this is possible.

Update

After Cheticamps answer I developed my own java version of his solution and wanted to post it here if anyone else was looking for this.

    ValueAnimator alphaAnim = ValueAnimator.ofInt(255,0).setDuration(1000);
        alphaAnim.addUpdateListener(valueAnimator -> {
            int alpha = (int) valueAnimator.getAnimatedValue();
            int newColor = binding.ForegroundSpanText.getCurrentTextColor() & 0x00ffff | (alpha <<24);
            System.out.println("Color: "+ Integer.toHexString(newColor));
            SpannableString tempStringHolder = binding.getAnimString();
            if(fadingSpan !=null){
                tempStringHolder.removeSpan(fadingSpan);
            }
            fadingSpan = new ForegroundColorSpan(newColor);
            tempStringHolder.setSpan(fadingSpan, 38, animStringHolder.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            binding.setAnimString(tempStringHolder);
        });
        alphaAnim.addListener(new AnimatorListenerAdapter(){
            @Override
            public void onAnimationEnd(Animator animation){
                System.out.println("Finished");
            }
        });

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

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

发布评论

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

评论(1

甜妞爱困 2025-01-27 09:53:17

您可以在文本部分设置一系列 ForegroundColorSpan你想要褪色。每个连续的 ForegroundColorSpan 都会减少文本颜色的 Alpha,直到 Alpha 值为零。 (Alpha == 255 完全可见;alpha == 0 不可见。)

ForegroundColorSpan 关联的文本颜色的 alpha 值的动画可以通过 ValueAnimator。下面展示了这种技术。

TextViw 只是

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:bufferType="spannable"
    android:gravity="center"
    android:text="Hello\nWorld!"
    android:textColor="@android:color/black"
    android:textSize="48sp"/>

出于演示目的将代码包装在按钮的单击侦听器中:

var fadingSpan: ForegroundColorSpan? = null
val spannable = binding.textView.text as Spannable
binding.button.setOnClickListener {
    // android:bufferType="spannable" must be set on the TextView for the following to work.
    // Alpha value varies from the 255 to zero. (We are assuming the starting alpha is 255.)
    ValueAnimator.ofInt(255, 0).apply {
        duration = 3000 // 3 seconds to fade
        // Update listener is called for every tick of the animation.
        addUpdateListener { updatedAnimation ->
            // Get the new alpha value and incorporate it into the color int (AARRGGBB)
            val newAlpha = updatedAnimation.animatedValue as Int
            val newColor =
                binding.textView.currentTextColor and 0x00ffff or (newAlpha shl 24)
            if (fadingSpan != null) {
                spannable.removeSpan(fadingSpan)
            }
            fadingSpan = ForegroundColorSpan(newColor)
            spannable.setSpan(
                fadingSpan,
                6,
                12,
                SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
        start()
    }
}

在此处输入图像描述

You can set a series of ForegroundColorSpans on the section of the text that you want to fade. Each successive ForegroundColorSpan will decrease the alpha of the text color until the alpha value is zero. (Alpha == 255 is fully visible; alpha == 0 is invisible.)

Animation of the alpha value of the text color associated with the ForegroundColorSpan can be accomplished with a ValueAnimator. The following shows this technique.

The TextViw is simply

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:bufferType="spannable"
    android:gravity="center"
    android:text="Hello\nWorld!"
    android:textColor="@android:color/black"
    android:textSize="48sp"/>

The code is wrapped in a button's click listener for demo purposes:

var fadingSpan: ForegroundColorSpan? = null
val spannable = binding.textView.text as Spannable
binding.button.setOnClickListener {
    // android:bufferType="spannable" must be set on the TextView for the following to work.
    // Alpha value varies from the 255 to zero. (We are assuming the starting alpha is 255.)
    ValueAnimator.ofInt(255, 0).apply {
        duration = 3000 // 3 seconds to fade
        // Update listener is called for every tick of the animation.
        addUpdateListener { updatedAnimation ->
            // Get the new alpha value and incorporate it into the color int (AARRGGBB)
            val newAlpha = updatedAnimation.animatedValue as Int
            val newColor =
                binding.textView.currentTextColor and 0x00ffff or (newAlpha shl 24)
            if (fadingSpan != null) {
                spannable.removeSpan(fadingSpan)
            }
            fadingSpan = ForegroundColorSpan(newColor)
            spannable.setSpan(
                fadingSpan,
                6,
                12,
                SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
        start()
    }
}

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文