如何在Android中使文本从左到右移动?

7
我使用这段代码使文本移动,并且它可以从右到左正确地运动。
不过,我想让TextView中的文本从左到右移动。
这是我的当前代码:
<TextView
    android:id="@+id/mylinenews"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="5"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:singleLine="true"

    android:text="textmoving textmoving textmoving textmoving textmoving textmoving textmoving textmoving textmoving textmoving "
    android:textColor="#fff"
/>

我应该如何修改这段代码,让文字从左到右移动?


你想要抖动文本还是只是想要移动? - Subhalaxmi
6个回答

4

将以下 XML 代码放置在 res 文件夹下的 anim 文件夹中:

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

   <translate
        android:fromXDelta="-3%p"
        android:toXDelta="3%p"
        android:duration="1200" />
</set>

并且,只需将这段代码添加到您的TextView中:

yourTextView.startAnimation(AnimationUtils.loadAnimation(activity, R.anim.move));

您可以在XML属性fromXDeltatoXDelta下处理移动位置。 - The Holy Coder

3
如果您想抖动文本,那么在您的活动中使用以下代码:
Animation shake = AnimationUtils.loadAnimation(YourActivity.this, R.anim.shake);
            YOUR_TEXT_VIEW.startAnimation(shake);

在 res 文件夹下创建一个名为 anim 的文件夹,即 res..> anim,并在 anim 文件夹中创建两个 XML 文件: 1)shake.xml 2)cycle.xml
在 shake.xml 中编写以下内容:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0" android:toXDelta="20" android:duration="7000"
    android:interpolator="@anim/cycle" /> 

在你的cycle.xml文件中编写如下内容:

<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="schemas.android.com/apk/res/android" android:cycles="10" />

在安卓文本视图中享受动态文本吧 :)


这是针对文本的抖动,但我只需要将文本从左到右移动,而不是从右到左。 - Hamdan Hejazi
是的,我写了。如果你想把文本从左边晃动到右边,那么上面的代码就是答案 :) - Subhalaxmi

1

从左到右的动画:

    TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)

    animation.setDuration(1500); // animation duration
    animation.setRepeatCount(1); // animation repeat count
    animation.setFillAfter(false);
    your_view .startAnimation(animation);//your_view for mine is imageView     

重复动画(例如从左到右和从右到左):

    TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)
    animation.setDuration(1500); // animation duration
    animation.setRepeatCount(4); // animation repeat count
    animation.setRepeatMode(2); // repeat animation (left to right, right to left)

    animation.setFillAfter(true);
    your_view .startAnimation(animation);//your_view for mine is imageView 

1
这是上下移动。 - basha

0

实现文本视图从左向右移动的工作代码为:

TextView tv2 = (TextView) findViewById(R.id.textscroll);
TranslateAnimation animation = new TranslateAnimation(0.0f, 1500.0f, 0.0f, 0.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)

animation.setDuration(7500); // animation duration, change accordingly
animation.setRepeatCount(1); // animation repeat count
animation.setFillAfter(false);
tv2 .startAnimation(animation);//your_view for which you need animation

0
     animation.setDuration(1500); // animation duration
                           animation.setRepeatCount(4); // animation repeat count
                           animation.setRepeatMode(2); // repeat animation (left to right, right to left)
 animation.setFillAfter(true);
                        textview.startAnimation(animation);

0

我知道我回答晚了,但最简单的方法是在XML文件中添加到您的TextView。

android:layoutDirection="rtl"

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