gpt4 book ai didi

java - Android onAnimationEnd 调用了两次

转载 作者:行者123 更新时间:2023-11-29 04:55:49 28 4
gpt4 key购买 nike

我已经在 View 上创建了一个 SLIDE UP 动画,我在 onAnimationEnd 上再次重复这个动画,但是我的 onAnimationEnd 触发了两次,我已经用 onAnimationEnd 的计数器检查了它,我会发布我的代码,你可以检查 onAnimationEnd 中的计数器是否会同时递增两次,我在onAnimationEnd方法中再次开始动画,请指导我哪里做错了?

    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 代码中更容易。
如果您尝试为两个属性设置动画:

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,因此请使用:

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

默认使用LinearInterpolator。

每 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();

关于java - Android onAnimationEnd 调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33803125/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com