animation of TextView's text size

2019-01-22 15:43发布

I want to be able to do a text animation and change the size of the text in a TextView. I read that there are property animations in android but if someone knows a simple code that can do this for me or an example somewhere I will deeply appreciate it. Thank u in advance!

4条回答
祖国的老花朵
2楼-- · 2019-01-22 16:31

Use ValueAnimator class in the 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();

refer this link ValueAnimator

Another solution is that apply scale animation on Textview or its parent layout

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);
查看更多
爷、活的狠高调
3楼-- · 2019-01-22 16:32

How about this link? In particular the scale examples. It provides source code and videos for several different types of Android animations.

查看更多
Animai°情兽
4楼-- · 2019-01-22 16:34
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..

If you are using button event to show animation then put the code inside onClick() otherwise use override method onWindowFocusChanged(boolean hasFocus) to start animation

查看更多
Summer. ? 凉城
5楼-- · 2019-01-22 16:38

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>

A function into an 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);
}

extracted and modified from here

查看更多
登录 后发表回答