更改TextView时的动画效果

35

我目前使用一种主要的解决方法,每当我更改TextView上的文本时,就切换两个活动。我正在使用此代码:

Weeklytext.this.overridePendingTransition( 
                    R.anim.slide_in_left, 
                    R.anim.slide_out_right
            );

是否可以在一个Activity中完成这个操作?因为只是为了使用动画而拥有两个完全相同的Activity有点让人不爽.

谢谢! 如果您不明白我的问题,请询问!

1个回答

69

你可以使用TextSwitcher来在改变TextView的文本时添加动画效果。

TextSwitcher仅是一种特殊类型的ViewSwitcher,因此它允许您提供两个视图以进行动画切换。当您调用setText()方法时,它会更新下一个TextView的文本,然后将其动画显示到屏幕上,并将当前的TextView移出屏幕。旧的TextView随后被指定为“下一个”TextView,该过程不断重复。

您可以使用setFactory(...)指定视图,或者只需使用addView(...)将两个TextView添加到其中。

// get a TextSwitcher view; instantiate in code or resolve from a layout/XML
TextSwitcher textSwitcher = new TextSwitcher(context);

// specify the in/out animations you wish to use
textSwitcher.setInAnimation(context, R.anim.slide_in_left);
textSwitcher.setOutAnimation(context, R.anim.slide_out_right);

// provide two TextViews for the TextSwitcher to use
// you can apply styles to these Views before adding
textSwitcher.addView(new TextView(context));
textSwitcher.addView(new TextView(context));

// you are now ready to use the TextSwitcher
// it will animate between calls to setText
textSwitcher.setText("hello");
...
textSwitcher.setText("goodbye");

这里有一个很好的例子:http://www.learn-android-easily.com/2013/06/android-textswitcher.html - Gunnar Bernstein
1
好的例子:http://mobile.antonio081014.com/2013/08/how-to-use-textswitcher-with-animation.html - Voora Tarun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接