2014-07-06 57 views
1

我试图在Andriod中从左向右设置动画文本。这是我的代码从左到右动画文本在TextView Android中修剪文本

<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 

它动画,但它修剪文本。我试图放入水平滚动视图,但它修剪从对面(右)的文本。

我需要在Java中制作动画,因为动画完成后我需要更改文本并开始另一个。

+0

你想达到什么目的?文本的水平滚动行? – Simas

+0

是的。文本的水平滚动行 – MSaudi

+0

一些更多的信息会很好。例如,文本是否更长,并且不适合屏幕,因此需要滚动?应该滚动循环? – Simas

回答

2

下面是一个快速'肮脏的方式来循环模仿字幕的字符串数组。由于它不是100%,你的字符串比屏幕宽度&更长,所以你想循环1,我知道的唯一方法是在TextView中为每个字符串设置动画。

这使它们循环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); 
    } 
} 

编辑-NOTE:

如果你的字符串不能在屏幕适合再使用相对布局一个巨大的layout_width

+0

它工作。谢谢 – MSaudi

+0

如果文字长度大于屏幕宽度,则不起作用 – MSaudi

+0

是的,您是对的。一个快速和肮脏的修复将是增加你的父宽度(对我来说它是'RelativeLayout')到5000dp这样的奇怪事情。还要确保你的TextView的宽度设置为'wrap_content'。 – Simas