如何连续放大 - 缩小TextView文本Android?

时间:2016-06-29 05:43:34

标签: android

我试图让TextView放大 - 在我的应用程序中连续缩小,我尝试了以下代码,但它只能运行一次,我想要无限时间。我的代码是---

private void setTvZoomInOutAnimation(final TextView textView) 
    {
        // TODO Auto-generated method stub

        final float startSize = 20;
        final float endSize = 13;
        final int animationDuration = 900; // 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();
                textView.setTextSize(animatedValue);
            }
        });

        animator.start();
    }

请帮忙。

2 个答案:

答案 0 :(得分:1)

尝试以下代码,您错过了以下行 -

<强> // animator.setRepeatCount(ValueAnimator.INFINITE); //将此行用于无限动画anmattionsanimator.setRepeatCount(2); //使用它来确定固定时间

private void setTvZoomInOutAnimation(final TextView textView) 
        {
            // TODO Auto-generated method stub

            final float startSize = 20;
            final float endSize = 13;
            final int animationDuration = 900; // 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();
                    textView.setTextSize(animatedValue);
                }
            });

            //animator.setRepeatCount(ValueAnimator.INFINITE);  // Use this line for infinite animations
            animator.setRepeatCount(2);
            animator.start();
        }

希望它会有所帮助..

答案 1 :(得分:0)

我试图解决你的问题。我希望,它可能对你有用。

public class MainActivity extends Activity {
    TextView scaleText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scaleText = (TextView) findViewById(R.id.txtView);
        setTvZoomInOutAnimation(scaleText);
     }
    private void setTvZoomInOutAnimation(final TextView textView) {
        final float startSize = 20;
        final float endSize = 13;
        final int animationDuration = 900;

    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();
            textView.setTextSize(animatedValue);
        }
    });
    animator.start();
}

}