0

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.

Zip
  • 809
  • 2
  • 14
  • 30
  • You might need to show the java code to illustrate the problem better. – Elye Dec 06 '16 at 02:19
  • @Elye Updated! Thanks. – Zip Dec 06 '16 at 09:36
  • where is your setFactory code and in/out animation code? – Elye Dec 06 '16 at 09:49
  • @Elye updated those methods. – Zip Dec 06 '16 at 10:09
  • The layout seems incorrect. Why do you have TextViews wrapped by TextSwitcher? We only need TextSwitcher – Elye Dec 06 '16 at 10:36
  • You don't need to clearAnimation, and seInAnimation and setOutAnimation twice. – Elye Dec 06 '16 at 10:39
  • @Elye Sorry I pasted my older version of code. I'm updating now. I only have TextSwitcher in xml, and only setAnimation once. The flicker seems to me is more like a random thing, like 30-40% of time i can see it. – Zip Dec 06 '16 at 18:11
  • What cause the change of state? Is it automatically, or manually? – Elye Dec 07 '16 at 10:56
  • @Elye It's automatically, that's why I have a check here `if (!textView.getText().toString().equals(text))`, I don't want it to setText() twice. – Zip Dec 07 '16 at 18:36
  • In this case, perhaps you could share the code of how this is done automatically, as the time and delay sequence could have impact on the animation. – Elye Dec 08 '16 at 04:55

0 Answers0