如何使TextView自动水平滚动并添加动画效果(非常长的文本)

9

我不太喜欢跑马灯,因为在跑马灯中你无法控制其速度。

我已经尝试过对TextView进行动画处理,但是即使所有父布局和视图组件的clipchildren = false,clipToPadding = false标志都设置了,父视图仍会在文本末端剪切文本。

我是否遗漏了什么或者有更好的解决方法?

xml代码如下:

<TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="66dp"
        android:maxLines="1"
        android:singleLine="true"
        android:textColor="#585858"
        android:textSize="32sp" >
    </TextView>

代码片段看起来像这样

TextView textView2 = (TextView)findViewById( R.id.textview1 );    
textView2.startAnimation((Animation)AnimationUtils.loadAnimation(this, R.anim.translate));

2
请告诉我您的问题是否解决了。如果已经解决,请告知具体步骤或者贴上一些代码。 - srinivas
11个回答

8
我很确定这一定能解决大量受众的问题。
问:使用跑马灯(至少尝试一次)以合理的速度水平无限地自动滚动单行长文本消息(可以使用硬编码或从string.xml中获取)。不要剪裁。
步骤1: 在activity_main.xml文件中:
<TextView
    android:text="either hard coding or from string.xml"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView2"  
    android:background="@color/colorPrimary"
    android:textSize="18sp"
    android:marqueeRepeatLimit="marquee_forever"
    android:textColor="@android:color/background_light" />

步骤2:在main_activity java文件中 public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = (TextView) findViewById(R.id.textView2);
    textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
    textView.setSelected(true);
    textView.setSingleLine(true);
    textView.setText("Oxfam says 8 men as rich as half the world. | Govt may set threshold for probe into deposits. | At least 32 dead after Turkish plane hits village.");}}

//如果已经输入了长输入,则可以删除最后一行。


7

我认为您可以使用翻译动画,类似于这样:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5000"
    android:fromXDelta="100"
    android:interpolator="@android:anim/linear_interpolator"
    android:repeatCount="infinite"
    android:repeatMode="restart"
    android:toXDelta="-100" />

并且像这样将其添加到您的文本视图中:

textview.startAnimation((Animation)AnimationUtils.loadAnimation(Context,R.anim.scroll_animation));

希望这能对您有所帮助。


3
我已经精确地这样做了,但父视图仍然裁剪文本。 - Shri
好的,我认为现在是你发布一些代码的时候了。你可以请发一下吗? - Hein

5
只需将此添加到您的TextView即可。
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:textSize="30dp"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="Your_Text" />

3

这是我的解决方案

为了让TextView中的长文本不被父视图或屏幕截断,我采取了两种措施。

首先,像下面的代码一样将TextView放在ScrollView中:

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_centerHorizontal="true">
    <TextView
        android:id="@+id/marquee_text"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:maxLines="1"
        android:textColor="@android:color/black"
        android:textSize="30sp"/>
</ScrollView>

然后,我测量我的文本大小,然后通过这样做来优化textview参数。
    marqueeText.setText("my long text");
    Paint textPaint = marqueeText.getPaint();
    String text = marqueeText.getText().toString();//get text
    int width = Math.round(textPaint.measureText(text));//measure the text size
    ViewGroup.LayoutParams params =  marqueeText.getLayoutParams();
    params.width = width;
    marqueeText.setLayoutParams(params); //refine

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
    int screenWidth = displaymetrics.widthPixels;

    //this is optional. do not scroll if text is shorter than screen width
    //remove this won't effect the scroll
    if (width <= screenWidth) {
        //All text can fit in screen.
        return;
    }
    //set the animation
    TranslateAnimation slide = new TranslateAnimation(0, -width, 0, 0);
    slide.setDuration(20000);
    slide.setRepeatCount(Animation.INFINITE);
    slide.setRepeatMode(Animation.RESTART);
    slide.setInterpolator(new LinearInterpolator());
    marqueeText.startAnimation(slide);

我希望这个解决方案能够帮助到其他遇到和我一样问题的人。我花了半天时间研究,希望这篇文章可以更好地让大家理解。


对我来说不起作用,文本幻灯片和动画都有文字截断,有人可以帮忙吗?! - Amal Kronz
@AmalKronz 尝试调试 marqueeText 使用的宽度,以查看文本是否被截断。此外,您可以使用工具层次结构视图来查看元素,以确保没有其他视图重叠在 marqueeText 上。祝好运 - Shengfeng Li
但它每次都重新启动动画,当动画结束时。我想让文本从右向左滚动,而不是从快的地方开始。 - Ahamadullah Saikat

2

可以尝试这个方法。这是使用TranslateAnimation创建自动滚动文本(水平滚动,从右到左)的解决方案(已在Android 8上测试通过)。

类:AnimationAutoTextScroller.java

最初的回答:

/**
 * A Class for automatically scrolling text horizontally from Right to Left 
 * using TranslateAnimation so that the scrolling speed can be controlled -Suresh Kodoor
 */

public class AnimationAutoTextScroller {

    Animation animator;
    TextView  scrollingTextView;
    int       duration = 50000;  // default value

    public AnimationAutoTextScroller(TextView tv, float screenwidth) {

        this.scrollingTextView = tv;
        this.animator = new TranslateAnimation(
                Animation.ABSOLUTE, screenwidth,
                Animation.RELATIVE_TO_SELF, -1f,
                Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0f
        );
        this.animator.setInterpolator(new LinearInterpolator());
        this.animator.setDuration(this.duration);
        this.animator.setFillAfter(true);
        this.animator.setRepeatMode(Animation.RESTART);
        this.animator.setRepeatCount(Animation.INFINITE);

        // setAnimationListener();
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public void setScrollingText(String text) {
        this.scrollingTextView.setText(text);
    }

    public void start() {
        this.scrollingTextView.setSelected(true);
        this.scrollingTextView.startAnimation(this.animator);
    }

    public void setAnimationListener() {

        animator.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationStart(Animation animation) {
            }
            public void onAnimationEnd(Animation animation) {
                // This callback function can be used to perform any task at the end of the Animation
            }
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }
}

布局XML:(将TextView保留在HorizontalScrollView下) "Original Answer"翻译成中文是“最初的回答”。让我们来谈谈布局XML。如果你想在TextView中显示大量文本,但不想让用户滚动浏览整个屏幕,那么可以将TextView放入HorizontalScrollView中。这样,用户就可以通过水平滚动来查看所有文本。记得使用正确的布局参数来避免布局问题。
 <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        app:layout_constraintBottom_toTopOf="@+id/hguide3"
        app:layout_constraintEnd_toStartOf="@+id/vguide2"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/vguide1"
        app:layout_constraintTop_toBottomOf="@+id/hguide2">

    <TextView
        android:id="@+id/translateanimatortextviewscroller"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:text=""
        android:singleLine="true"
        android:focusable="true"
        android:scrollHorizontally="true"
        android:background="#000000ff"
        android:textColor="#ff0000"
        android:textSize="55dp"
        android:textStyle="bold"
        android:typeface="sans" />

      </HorizontalScrollView>

Activity:

TextView scrollertextview = findViewById(R.id.translateanimatortextviewscroller);
textscroller = new AnimationAutoTextScroller(scrollertextview, screenwidth);
textscroller.setScrollingText(scrollertext);
textscroller.setDuration(60000);
textscroller.start();

1

Add this Animation file:

<translate
    android:duration="7000"
    android:fromYDelta="0%p"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:repeatCount="10"
    android:repeatMode="restart"
    android:toYDelta="-100%p" />

/*Put your text view inside scroll*/
        <ScrollView
            android:layout_width="@dimen/dp_size_220"
            android:layout_height="@dimen/dp_size_16"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/iv_myra_notification"
            app:layout_constraintRight_toLeftOf="@+id/iv_one_way"
            app:layout_constraintTop_toTopOf="parent">
            <TextView
                android:id="@+id/marquee_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="left"
                android:text="@{itemData.notification.message}"
                android:textColor="@android:color/black"
                android:textSize="12sp"
                tools:text="bfnfkjnvlen jknjkgnojeng"/>
        </ScrollView>

0
使用这个简单方法,利用 @rajath answer 中的 ellipsizemarquee 选项。
<TextView
    android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget" 
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit ="marquee_forever"
    android:focusable="true"
    android:focusableInTouchMode="true" 
    android:scrollHorizontally="true"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/>

0

这是对我有效的方法。 将您的TextView放置在ScrollView内,然后对ScrollView的子项(在我的情况下是LinearLayout)执行TranslateAnimation。 实际上,我正在动态地向这个LinearLayout中添加多个视图。

<ScrollView android:id="@+id/textScrollView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/textLayout"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </LinearLayout>
</ScrollView>

TranslateAnimation slide = new TranslateAnimation(0, 0, height, -textLayout.getHeight());
    slide.setDuration(movementSpeed);
    slide.setRepeatCount(-1);
    slide.setRepeatMode(Animation.RESTART);
    slide.setInterpolator(new LinearInterpolator());

textLayout.startAnimation(slide);

height --> The point start scrolling up (in my case device height (device bottom))
movementSpeed --> scrolling speed

0

尝试这段代码,它会帮助你解决问题 Shri n HERO

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

</translate>

0

这真的很令人沮丧,... 但答案很简单, 使用EditText而不是TextView,并将其包装在HorizontalScrollView中 同时,设置focusable: false

   <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:scrollbars="none">

        <EditText
            android:id="@+id/post_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:focusable="false"
            android:outlineAmbientShadowColor="@color/colorPrimaryDark"
            android:shadowColor="#000000"
            android:shadowDx="1.5"
            android:shadowDy="1.3"
            android:shadowRadius="1.6"
            android:singleLine="true"
            android:textAlignment="center"
            android:textColor="@color/colorPrimary"
            android:textSize="20dp" />
    </HorizontalScrollView>

感谢 Hein,添加动画代码

final EditText textView =  view.findViewById(R.id.post_message);
        textView.startAnimation((Animation) AnimationUtils.loadAnimation(context,R.anim.horizontal_animation));
        String message="LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLong Text.";
        textView.setText(message);

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