I'm using a TextSwitcher
to automatically switch text when certain broadcasts
are received, and I add slide in / out animation for this TextSwitcher. But sometimes (like 30% of time) when the TextSwitcher
switches from the textView before last one to the last one, there's a flicker happens.
Visually what I see is that State4
slided out, State5
slided in, when State5
was not sliding in completely, State4
came back again and slided out, then State5
slided in. I've checked the logs and print every broadcast, they're received in correct order. Seems to me it's the UI that acts weird.
Does anyone know why this happens? Thanks!!!
Some codes:
<TextSwitcher
android:id="@+id/ts1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
And I use setFactory
to set TextView
of the TextSwitcher
.
TextView textView = (TextView) textSwitcher.getCurrentView();
switch (state) {
case state1:
text = state1;
break;
case state2:
text = state2;
break;
case state3:
text = state3;
break;
case state4:
text = state4;
break;
case state5:
text = state5;
break;
default:
break;
}
if (!textView.getText().toString().equals(text))
textSwitcher.setText(text);
The text will switch from 1 to 5 by order, and 5 is ending state. Not sure why sometimes 5 will flicker back to 4 and then to 5 again. I also checked if current text in textSwitcher
is the same with next one, we don't do setText()
so that no animation will be performed.
private void setFactoryForTextSwitcher(final TextSwitcher textSwitcher) {
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public TextView makeView() {
TextView textView = new TextView(context);
textView.setSingleLine(true);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.MATCH_PARENT));
return textView;
}
});
}
private void loadAnimationForTextSwitcher() {
final Animation slideInAnimation = AnimationUtils.loadAnimation(context, R.animator.slide_in);
final Animation slideOutAnimation = AnimationUtils.loadAnimation(context, R.animator.slide_out);
textSwitcher.setInAnimation(slideInAnimation);
textSwitcher.setOutAnimation(slideOutAnimation);
}
These 2 methods are called during initialization.