Android onAnimationEnd被两次调用 [英] Android onAnimationEnd called twice

查看:296
本文介绍了Android onAnimationEnd被两次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图上创建了一个SLIDE UP动画,我再次在onAnimationEnd上重复此动画,但是我的onAnimationEnd触发了两次,我在onAnimationEnd处用计数器检查了它,我将发布我的代码,您可以检查onAnimationEnd中的计数器将同时增加两次,我将在onAnimationEnd方法中再次启动动画,请指导我我做错了什么地方?

i have created a SLIDE UP animation on view and i am repeating this animation again onAnimationEnd but my onAnimationEnd fired twice , i have checked it with counter at onAnimationEnd , i will post my code, you can check that the counter in onAnimationEnd will incremented twice at same time , I am starting the animation again in onAnimationEnd method , please guide me where i am doing wrong?

    private Animation animSlideUp;
        animSlideUp = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up);
                // set animation listener
                animSlideUp.setAnimationListener(this);
                animSlideUp.setDuration(500);
                animSlideUp.setStartOffset(5000);

                tickerView.startAnimation(animSlideUp);

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (animation == animSlideUp) {
                ticker_counter++;
                Log.e("onAnimationEnd=", "ticker_counter="+ticker_counter);
                tickerView.startAnimation(animSlideUp);

            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

slide_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXScale="1.0"
        android:toYScale="0.0"/>

</set>
LOGCAT

    11-19 17:06:54.375   E/onAnimationEnd=﹕ ticker_counter=1
    11-19 17:06:54.392   E/onAnimationEnd=﹕ ticker_counter=2
    11-19 17:06:59.912   E/onAnimationEnd=﹕ ticker_counter=3
    11-19 17:06:59.928   E/onAnimationEnd=﹕ ticker_counter=4
    11-19 17:07:05.453   E/onAnimationEnd=﹕ ticker_counter=5
    11-19 17:07:05.470   E/onAnimationEnd=﹕ ticker_counter=6
    11-19 17:07:10.991   E/onAnimationEnd=﹕ ticker_counter=7
    11-19 17:07:11.008   E/onAnimationEnd=﹕ ticker_counter=8

推荐答案

如果您希望动画播放一次,则必须删除tickerView.startAnimation(animSlideUp);.来自onAnimationEnd方法.
避免使用xml动画,这在Java代码中要容易得多.
如果要为两个属性设置动画:

If you want your animation to play once, you have to remove tickerView.startAnimation(animSlideUp); from the onAnimationEnd Method.
Avoid using xml animations, it's much easier in java code.
If you're trying to animate two properties :

PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(tickerView, pvhX, pvhY);
        animator.setDuration(500);
        animator.start();

在您的情况下,您只将scaleY值从1.0更改为0,所以请使用以下方法:

In your case, you're only changing the scaleY value from 1.0 to 0, so use this :

ObjectAnimator animator = ObjectAnimator.ofFloat(tickerView, "scaleY",0);
animator.setDuration(500);
//To repeat twice if you want to
animator.setRepeatCount(1);
animator.start();

默认使用LinearInterpolator.

LinearInterpolator is used by default.

每5秒重复一次

    final ObjectAnimator animator = ObjectAnimator.ofFloat(tickerView, "scaleY", 0);
    animator.setDuration(500);
    animator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            ticker_counter++;
            animation.setStartDelay(5000);
            animator.start();
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    });
    animator.start();

这篇关于Android onAnimationEnd被两次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆