Android:onAnimationStart第一次跳过

时间:2015-11-16 10:25:55

标签: android animation

第一次点击图标时,onAnimationStart()不会播放,但onAnimationEnd()会播放。在随后的点击中,两者都有效。谁知道为什么?

private void animateOnAwesome(final ImageButton awesomeIcon) {
    Animation animateOnAwesome = AnimationUtils.loadAnimation(getContext(),
            R.anim.hold);
    awesomeIcon.startAnimation(animateOnAwesome);
    animateOnAwesome.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            switch (ParseApplication.currentPosition) {
                case 0:
                    Glide.with(getContext()).load(R.raw.awesomeddd_light).asGif().into(awesomeIcon);
                    break;
                case 1:
                    Glide.with(getContext()).load(R.raw.awesomeddd_dark).asGif().into
                            (awesomeIcon);
                    break;
                default:
                    Log.d("NO_THEME", "No theme selected.");
                    break;
            }
        }

        public void onAnimationEnd(Animation anim) {
            awesomeIcon.setImageResource(R.drawable.awesomeddd);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
}

1 个答案:

答案 0 :(得分:0)

您在设置侦听器之前启动动画。这应该可以解决问题:

animateOnAwesome.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        switch (ParseApplication.currentPosition) {
            case 0:
                Glide.with(getContext()).load(R.raw.awesomeddd_light).asGif().into(awesomeIcon);
                break;
            case 1:
                Glide.with(getContext()).load(R.raw.awesomeddd_dark).asGif().into
                        (awesomeIcon);
                break;
            default:
                Log.d("NO_THEME", "No theme selected.");
                break;
        }
    }

    public void onAnimationEnd(Animation anim) {
        awesomeIcon.setImageResource(R.drawable.awesomeddd);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});

awesomeIcon.startAnimation(animateOnAwesome);