从左到右动画文本修剪TextView Android中的文本

时间:2014-07-06 09:06:02

标签: android animation textview marquee

我正试图在Andriod中从左到右动画文字。这是我的代码

<TextView
    android:id="@+id/scrolling_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#777777"
    android:singleLine="true"
    android:textSize="48sp" >
</TextView>



animation = new TranslateAnimation(-100.0f, screenWidth+300,
                    0.0f, 0.0f);          //  new TranslateAnimation(xFrom,xTo, yFrom,yTo)
            animation.setDuration(Constants.ANIMATION_SPEED);  // animation duration 
            animation.setAnimationListener(this);

            tv.startAnimation(animation);  // start animation 

它会动画,但会修剪文字。我尝试在Horizo​​ntal ScrollView中输入,但是它修剪了对面(右)的文本。

我需要用Java制作动画,因为动画完成后我需要更改文本并启动另一个。

1 个答案:

答案 0 :(得分:2)

这是一种快速的方法来循环模拟字幕的字符串数组。因为你的字符串长度超过屏幕宽度&amp;而不是100%你想逐个循环它们,我知道的唯一方法是在TextView中设置每个字符串的动画。

这将它们循环1乘1.每次迭代后,它会设置下一个字符串,重新计算宽度并相应地创建一个新动画。

package com.example.scrolling_text;

import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

/**
 * Created by Simon on 14.7.4.
 */
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    TextView textView;
    int screenWidth, currentMsg;
    String[] msgArray = new String[] {"Message 1", "Hello i'm message 2", "This is message 3"};
    Animation.AnimationListener myAnimationListener;

    @Override
    protected void onStart() {
        super.onStart();

        textView = (TextView) findViewById(R.id.scrolling_text);

        // Get the screen width
        Point size = new Point();
        getWindowManager().getDefaultDisplay().getSize(size);
        screenWidth = size.x;

        // Set the first message
        textView.setText(msgArray[0]);
        // Measure the size of textView
        textView.measure(0, 0);
        // Get textView width
        int textWidth = textView.getMeasuredWidth();
        // Create the animation
        Animation animation = new TranslateAnimation(-textWidth, screenWidth, 0, 0);
        animation.setDuration(5000);
        animation.setRepeatMode(Animation.RESTART);
        animation.setRepeatCount(Animation.INFINITE);

        // Create the animation listener
        myAnimationListener = new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // If out of messages loop from start
                if (++currentMsg >= msgArray.length)
                    currentMsg = 0;
                // Set the next msg
                textView.setText(msgArray[currentMsg]);
                // Measure the size of textView // this is important
                textView.measure(0, 0);
                // Get textView width
                int textWidth = textView.getMeasuredWidth();
                // Create the animation
                animation = new TranslateAnimation(-textWidth, screenWidth, 0, 0);

                animation.setDuration(5000);
                animation.setRepeatMode(Animation.RESTART);
                animation.setRepeatCount(Animation.INFINITE);
                animation.setAnimationListener(myAnimationListener);
                textView.setAnimation(animation);
            }
        };
        animation.setAnimationListener(myAnimationListener);

        textView.setAnimation(animation);
    }
}

EDIT-注:

如果你的字符串不适合屏幕,那么使用相对布局和巨大的layout_width