Android中如何让文字从左向右移动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21474858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:38:12  来源:igfitidea点击:

How make text moving from left to right in Android?

androidtextview

提问by Hamdan Hejazi

I use this code to make text move and it moves correctly from right to left.

我使用此代码使文本移动并且它从右向左正确移动。

Nevertheless, I want to make the text in TextViewmove from left to right.

尽管如此,我想让TextView 中的文本从左向右移动。

Here is my current code:

这是我当前的代码:

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

How can I modify this code so the text moves from left to right?

如何修改此代码以使文本从左向右移动?

回答by The Holy Coder

Use below xml code in anim folder under res :

在 res 下的 anim 文件夹中使用以下 xml 代码:

<?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>

And, just add this to your textview :

而且,只需将此添加到您的 textview :

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

回答by Subhalaxmi

If you want to shake the text than In Your Activity Use

如果你想摇动文本而不是在你的活动中使用

Animation shake = AnimationUtils.loadAnimation(YourActivity.this, R.anim.shake);
            YOUR_TEXT_VIEW.startAnimation(shake);

Create a folder name animin res i.e. res..> anim and create two xml in anim folder 1) shake.xml2) cycle.xml

在 res ie res..> anim 中创建一个文件夹名 anim并在 anim 文件夹中创建两个 xml 1) shake.xml2)cycle.xml

In Your shake.xml write

在你的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" /> 

and in your cycle.xml write

并在您的 cycle.xml 中写入

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

Enjoy Animated Text in android text view :)

在 android 文本视图中享受动画文本 :)

回答by Andrew Coder

Left to right animation:

从左到右动画:

    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     

Repeated animation (for eg. left to right and right to left):

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

    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 

回答by ghumdan16

I know i am a too late to answer but the simplest way is to add to your textview in the xml file

我知道我回答得太晚了,但最简单的方法是在 xml 文件中添加到您的 textview

android:layoutDirection="rtl"

回答by Avinash

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

回答by Praveen Kashyap

The working code for the moving textview left to right is

从左到右移动文本视图的工作代码是

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