2011-10-06 82 views
6

我希望能夠進行文字動畫並更改TextView中文字的大小。我讀過在android中有屬性動畫,但如果有人知道一個簡單的代碼可以爲我做這個或一個例子,我會深深地感激它。提前感謝你!TextView的文字大小動畫

+0

相關信息[android textview的文本大小而不是整個TextView的動畫](// stackoverflow.com/a/30324368) – Tushar

回答

15

scale.xml

<?xml version="1.0" encoding="utf-8"?> 
<set 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <scale 
      android:fromXScale="1.0" 
      android:fromYScale="1.0" 
      android:toXScale="2.0" 
      android:toYScale="2.0" 
      android:duration="3000"></scale> 
</set> 

函數到活動:

private void RunAnimation() 
{ 
    Animation a = AnimationUtils.loadAnimation(this, R.anim.scale); 
    a.reset(); 
    TextView tv = (TextView) findViewById(R.id.firstTextView); 
    tv.clearAnimation(); 
    tv.startAnimation(a); 
} 

提取,如果您正在使用按鈕事件,顯示動畫從here

+1

謝謝你,這對我幫助很大。但是我也在流浪,可以在文本上使用屬性動畫來完成,而不是整個文本視圖? – Sandra

+0

'scale.xml'進入**/res/anim/** – gnB

3

this link怎麼樣?特別是規模的例子。它爲幾種不同類型的Android動畫提供源代碼和視頻。

+1

謝謝你的快速回復esponse和鏈接。我會看看。 – Sandra

+0

不要依賴鏈接,因爲它可能會在未來下降。將相關鏈接的部分添加到答案中。 –

5
Animation animation=new TranslateAnimation(0,480,0,0); 
animation.setDuration(5000); 
animation.setRepeatMode(Animation.RESTART); 
animation.setRepeatCount(Animation.INFINITE); 
text.startAnimation(animation); 
// applying animation to textview object.. 

修改然後把onClick()中的代碼否則使用覆蓋方法onWindowFocusChanged (布爾hasFocus)啓動動畫

1
在Android

final float startSize = o; // Size in pixels 
    final float endSize = 30; 
    final int animationDuration = 1000; // Animation duration in ms 

    ValueAnimator animator = ValueAnimator.ofFloat(startSize, endSize); 
    animator.setDuration(animationDuration); 

    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(ValueAnimator valueAnimator) { 
      float animatedValue = (float) valueAnimator.getAnimatedValue(); 
      tv.setTextSize(animatedValue); 
     } 
    }); 

    animator.start(); 

使用ValueAnimator類是指該鏈接ValueAnimator

另一種解決方案是,在TextView的或其父佈局應用規模的動畫

ScaleAnimation scaleAnimation = new ScaleAnimation(0.7f, 1.1f, 0.7f, 1.1f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, 
        ScaleAnimation.RELATIVE_TO_SELF, 0.5f); 
scaleAnimation.setDuration(600); 
viewZoom.startAnimation(scaleAnimation); 
+0

已複製的代碼來自[此回答](http://stackoverflow.com/a/30324368/2025923) – Tushar

+0

即使在OnePlus3T上工作也不順利( –