TextView文本大小的动画

San*_*dra 9 size animation android text textview

我希望能够在TextView中进行文本动画并更改文本的大小.我读到android中有属性动画,但如果有人知道一个简单的代码可以为我做这个或某个地方的例子我会深深体会到它.提前谢谢你!

A.Q*_*oga 19

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>
Run Code Online (Sandbox Code Playgroud)

一个活动的功能:

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);
}
Run Code Online (Sandbox Code Playgroud)

这里提取和修改

  • `scale.xml` 位于 **/res/anim/** 下 (2认同)

小智 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..
Run Code Online (Sandbox Code Playgroud)

如果您使用按钮事件来显示动画,则将代码放在onClick()中,否则使用覆盖方法onWindowFocusChanged(boolean hasFocus)来启动动画


Nid*_*hin 5

在android中使用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();
Run Code Online (Sandbox Code Playgroud)

请参阅此链接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);
Run Code Online (Sandbox Code Playgroud)

  • 即使在 OnePlus3T 上也运行不流畅( (2认同)