android - 更改 TextView 时的动画

标签 android animation textview

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

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

是否可以在一个 Activity 中做到这一点?有两个内容完全相同的 Activity 只是为了让我可以使用动画,这有点烦人;)

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

最佳答案

您可以使用 TextSwitcher在 TextView 中更改文本时有动画。

TextSwitcher 只是一种特殊的 ViewSwitcher ,因此,它允许您提供两个 View ,在它们之间进行动画处理。当您调用 setText() 时,它会更新下一个 TextView 的文本,然后将其动画化到屏幕上,并将当前的动画化。然后将旧的 TextView 指定为“下一个”TextView,并重复该过程。

您可以使用 setFactory(...) 指定 View 或者只是简单地使用 addView(...) 添加两个 TextViews .

// 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");

关于android - 更改 TextView 时的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7573870/

相关文章:

android - TextView 显示 "false"而不是资源文本

android - 调整 TextView 的大小不会在 Android 3.1 上缩小其高度

android - Android 和 iPhone 之间的 Xamarin 可移植性

c# - C# android 中的回调

android - 如何将pt转换为sp?

javascript - SetTimeout 的执行速度比预期的要快

javascript - 使元素自动适应父级 w/o 表/javascript 的宽度

iphone - 如何为 iPhone 应用程序制作文本动画以逐字显示?

android - react-native-maps:如何定期更新从远程服务器收到的用户位置

java - Android TextView 没有完全更新