Android onAnimationEnd调用了两次

时间:2015-11-19 11:49:44

标签: java android android-layout android-animation

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

func addFoodItem(calories: Double, completion: ((NSError?) -> Void)?){

    let nowDate: NSDate = NSDate()

    let energyQuantityConsumed: HKQuantity = HKQuantity(unit: HKUnit.kilocalorieUnit(), doubleValue: calories)
    let energyConsumedType: HKQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryEnergyConsumed)!
    let energyConsumedSample: HKQuantitySample = HKQuantitySample(type: energyConsumedType, quantity: energyQuantityConsumed, startDate: nowDate, endDate: nowDate)
    let energyConsumedSamples: Set<HKSample> = [energyConsumedSample]

    let foodType: HKCorrelationType = HKCorrelationType.correlationTypeForIdentifier(HKCorrelationTypeIdentifierFood)!

    let foodCorrelation: HKCorrelation = HKCorrelation(type: foodType, startDate: nowDate, endDate: nowDate, objects: energyConsumedSamples)

    let completion: (Bool, NSError?) -> Void = {
        (success, error) -> Void in

        if completion != nil {
            completion!( error)
        }            
    }

    self.healthStore.saveObject(foodCorrelation, withCompletion: completion)
}

func fetchSumOfSamplesTodayForType(quantityType : HKQuantityType , unit: HKUnit, options: HKStatisticsOptions, forDay : Int, completion: ((Double, NSError?) -> Void)?) {

    let predicate = self.predicateForSamplesToday(forDay)

    let query: HKStatisticsQuery = HKStatisticsQuery(quantityType: quantityType, quantitySamplePredicate: predicate, options: options) {
        (_query, result, error) -> Void in


         let sum: HKQuantity? = result?.sumQuantity()

        if completion != nil {
            let value: Double = sum?.doubleValueForUnit(unit) ?? 0.0

            completion!(value, error)
        }
    }

    self.healthStore.executeQuery(query)
}

4 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我删除了

android:animateLayoutChanges="true" 

从xml文件中获得帮助

答案 1 :(得分:0)

如果您希望动画播放一次,则必须删除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();

答案 2 :(得分:0)

我可能会找到解决方案:发布runnable以重新启动动画。

在您的情况下,在OnAnimationEnd()函数中,代码如下:

 @Override
    public void onAnimationEnd(Animation animation) {
        if (animation == animSlideUp) {
            ticker_counter++;
            Log.e("onAnimationEnd=", "ticker_counter="+ticker_counter);
            tickerView.post(new Runnable(){ //post an runnable to start
                tickerView.startAnimation(animSlideUp);
            });

        }
    }

答案 3 :(得分:0)

我遇到了同样的问题,只是将null设置为第二动画的侦听器对我有用。

setListener(null);

示例:

view.animate()
    .alpha(0F)
    .setDuration(200L)
    .setListener(new Animator.AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {

            view.animate()
                .alpha(1F)
                .setDuration(200L)
                .setListener(null)
                .start();
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }

    }).start();