草庐IT

Android TextView : is there a way to force the marquee animation with short text?

coder 2023-11-28 原文

我有一个 TextView,里面有一些文本,我希望它用滚动字幕动画来制作动画。我看到这个popular question关于强制字幕动画,但是答案中的代码仅在文本足够长以超出 TextView 的边界时才有效(因此文本被截断),我一直在寻找一种解决方案来永久使文本具有此功能无论文本的宽度如何,选取框动画;这可能吗?

最佳答案

制作你自己的动画。

anim/marquee.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="100%"
        android:toXDelta="-100%"
        android:duration="10000"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:interpolator="@android:anim/linear_interpolator"/>
</set>

在你的 Activity 中,

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);

    TextView myTextView = (TextView) findViewById(R.id.myTextView);
    Animation marquee = AnimationUtils.loadAnimation(this, R.anim.marquee);
    myTextView.startAnimation(marquee);
}

关于Android TextView : is there a way to force the marquee animation with short text?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28589049/

有关Android TextView : is there a way to force the marquee animation with short text?的更多相关文章

随机推荐