2016-11-17 26 views
2

我試圖創建一個動畫,它將從左向右移動TextView並無限循環。這是TextView我想動畫:連續動畫從左到右的文字

<TextView 
    android:id="@+id/txtTitle" 
    android:layout_width="280dp" 
    android:layout_height="wrap_content" 
    android:textSize="16sp" 
    android:textStyle="italic" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:layout_marginTop="20dp" 
    android:ellipsize="end" 
    android:maxLines="1" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@id/cardView" /> 

這我怎麼想動畫TextView

Animation animation = new TranslateAnimation(0, -280, 0, 0); 
animation.setDuration(9000); 
animation.setRepeatMode(Animation.RELATIVE_TO_SELF); 
animation.setRepeatCount(Animation.INFINITE); 
textView.setAnimation(animation); 

我想實現的是對文本中的中心開出屏幕向右移動,一旦第一個字母離開屏幕,它應該重新出現在另一側。

+0

正如一個側面說明所謂:你真的應該使用'Animator' API代替的視圖動畫。 「Animator」API是一個更新,更出色的動畫API,它得到了廣泛的支持(99%的設備)。作爲一個經驗法則:每個在其名稱中包含「Animation」的類(例如'TranslateAnimation')都是舊視圖動畫API的一部分,其名稱中包含'Animator'的所有內容都是更新的'Animator' API的一部分。 –

回答

7

想要做的事情可以通過簡單的ValueAnimator輕鬆實現。

你首先要做的是把兩個相同版本的TextView你想動畫到你的佈局。在這個例子中我的佈局是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/first" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textSize="32sp" 
     android:text="@string/hello_word"/> 

    <TextView 
     android:id="@+id/second" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textSize="32sp" 
     android:text="@string/hello_word"/> 

</FrameLayout> 

然後 - 我已經講過了 - 你用ValueAnimator動畫都Views的translationX屬性,但偏向一個由屏幕(寬度,因爲上面的TextViews使用match_parent作爲寬度,它們的寬度等於屏幕的寬度,這就是我將用來抵消其中一個位置的位置)。你的代碼應該是這個樣子:

final TextView first = (TextView) findViewById(R.id.first); 
final TextView second = (TextView) findViewById(R.id.second); 

final ValueAnimator animator = ValueAnimator.ofFloat(0.0f, 1.0f); 
animator.setRepeatCount(ValueAnimator.INFINITE); 
animator.setInterpolator(new LinearInterpolator()); 
animator.setDuration(9000L); 
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
    @Override 
    public void onAnimationUpdate(ValueAnimator animation) { 
     final float progress = (float) animation.getAnimatedValue(); 
     final float width = first.getWidth(); 
     final float translationX = width * progress; 
     first.setTranslationX(translationX); 
     second.setTranslationX(translationX - width); 
    } 
}); 
animator.start(); 

而且結果應該是這個樣子:

looing text demo

+0

這工作完美,謝謝。 – Julia

0

如果有人試圖尋找在遊戲畫布關於文本字幕不斷這裏是代碼在你的遊戲循環中實現這一點。 StartingLine.java類返回畫布上的遊戲要繪製表面的其功能正在被gameview

public class StartingLine { 
private int screenWidth; 
private int screenHeight; 
private int xStartPosition; 
private int yStartPosition; 
private int xEndPosition; 
private int yEndPosition; 
private int width = 50; 
private int tv1y = 0; 
private int tv2y = 0; 
private int tv3y = 0; 
private Paint paint; 


public StartingLine(Context context, int screenX, int screenY, int laneCount) { 
    screenWidth = screenX; 
    screenHeight = screenY; 
    this.xStartPosition = screenWidth - width - (screenHeight/laneCount) * 2; 
    this.yStartPosition = 0; 
    this.xEndPosition = screenWidth - width - (screenHeight/laneCount) * 2; 
    this.yEndPosition = screenHeight; 
    paint = new Paint(); 
} 

public void update(int color) { 
    paint.setColor(color); 
    paint.setStrokeWidth(2); 
    paint.setTextSize(30); 
    paint.setTextAlign(Paint.Align.CENTER); 
} 

public void draw(Canvas canvas) { 
    Rect tb = new Rect(); 
    paint.getTextBounds("STOP AFTER THIS POINT", 0, "STOP AFTER THIS POINT".length(), tb); 
    if (tv1y == 0 && tv2y == 0 && tv3y == 0) { 
     tv1y = 0; 
     tv2y = tb.width() + width;//tb.width()+width = gap between two texts 
     tv3y = 2 * (tb.width() + width); 
    } else { 
     if (tv3y >= (3 * (tb.width() + width))) { 
      tv3y = 2 * (tb.width() + width); 
     } 

     tv1y = tv3y - 2 * (width + tb.width()); 
     tv2y = tv1y + width + tb.width(); 
     tv3y = tv2y + width + tb.width(); 
    } 

    canvas.drawLine(xStartPosition, yStartPosition, xEndPosition, yEndPosition, paint); 
    canvas.drawLine(xStartPosition + width, yStartPosition, xEndPosition + width, yEndPosition, paint); 
    canvas.save(); 
    canvas.rotate(270, screenWidth/2, screenHeight/2); 

    canvas.drawText("STOP AFTER THIS POINT", tv1y += 5, yEndPosition - 20, paint); 
    canvas.drawText("STOP AFTER THIS POINT", tv2y += 5, yEndPosition - 20, paint); 
    canvas.drawText("STOP AFTER THIS POINT", tv3y += 5, yEndPosition - 20, paint); 
    canvas.restore(); 
} 
}