'Animating a specific line of a textview using android Animations
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");
}
});
Solution 1:[1]
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()
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cheticamp |

