使用Android动画对文本视图的特定行进行动画
我想知道是否可以在文本视图的特定行上执行淡出/淡入动画。我有一个 两行文本视图,我希望“标题”保持可见,而“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在文本部分设置一系列 ForegroundColorSpan你想要褪色。每个连续的 ForegroundColorSpan 都会减少文本颜色的 Alpha,直到 Alpha 值为零。 (Alpha == 255 完全可见;alpha == 0 不可见。)
与 ForegroundColorSpan 关联的文本颜色的 alpha 值的动画可以通过 ValueAnimator。下面展示了这种技术。
TextViw 只是
出于演示目的将代码包装在按钮的单击侦听器中:
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
The code is wrapped in a button's click listener for demo purposes: