TextView的文字大小动画

13

我想能够在TextView中进行文本动画并改变文本的大小。我了解到Android中有属性动画,但如果有人知道可以为我完成此操作的简单代码或示例,我将深表感激。提前谢谢!


与Android TextView的文本大小相关的动画,而不是整个TextView。 - Tushar
3个回答

21

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>

将一个函数转化为Activity:
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);
}

提取并修改自此处

1
谢谢,这对我很有帮助。但我也在想,能否使用文本的属性动画来完成,而不是整个文本视图? - Sandra
2
scale.xml 应放置在 /res/anim/ 目录下。 - Gene Bo

6
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(boolean hasFocus)来开始动画。

6

在安卓中使用ValueAnimator类

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

另一种解决方法是在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);

1
这个答案复制了代码。 - Tushar
2
即使在OnePlus3T上也无法流畅运行。 - imike

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接