2014-07-06 61 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