ValueAnimator不会触发属性方法

时间:2014-11-12 05:28:07

标签: android

我整天都调试了以下代码,并且不知道为什么它不能按预期工作。

import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.PropertyValuesHolder;
import com.nineoldandroids.animation.ValueAnimator;

private void animateThumbCoorToMatchCurrentIndixes() {
    float newThumbLeftCoor = this.indexToCoor(this.thumbLeftIndex);
    float newThumbRightCoor = this.indexToCoor(this.thumbRightIndex);

    PropertyValuesHolder thumbLeftPropertyValuesHolder = PropertyValuesHolder.ofFloat("thumbLeftCoor", this.thumbLeftCoor, newThumbLeftCoor);
    PropertyValuesHolder thumbRightPropertyValuesHolder = PropertyValuesHolder.ofFloat("thumbRightCoor", this.thumbRightCoor, newThumbRightCoor);

    Log.i("CHEOK", "Animate " + this.thumbLeftCoor + " -> " + newThumbLeftCoor);
    Log.i("CHEOK", "Animate " + this.thumbRightCoor + " -> " + newThumbRightCoor);

    ValueAnimator valueAnimator = ObjectAnimator.ofPropertyValuesHolder(this, thumbLeftPropertyValuesHolder, thumbRightPropertyValuesHolder);
    valueAnimator.setDuration(getResources().getInteger(android.R.integer.config_shortAnimTime));
    valueAnimator.setRepeatCount(0);
    valueAnimator.setInterpolator(new DecelerateInterpolator());

    Log.i("CHEOK", "Animation duration = " + valueAnimator.getDuration());

    valueAnimator.start();
}

// Do not remove this code. It is used for reflection call for animation.
@SuppressWarnings("unused")
private void setThumbLeftCoor(float thumbLeftCoor) {
    this.thumbLeftCoor = thumbLeftCoor;
    Log.i("CHEOK", "SET thumbLeftCoor " + thumbLeftCoor);
    this.postInvalidate();
}

// Do not remove this code. It is used for reflection call for animation.
@SuppressWarnings("unused")
private void setThumbRightCoor(float thumbRightCoor) {
    this.thumbRightCoor = thumbRightCoor;
    Log.i("CHEOK", "SET thumbRightCoor " + thumbRightCoor);
    this.postInvalidate();
}

我的控制台日志如下:

Animate 636.0985 -> 0.0
Animate 679.27057 -> 578.0
Animation duration = 200

但是,方法setThumbLeftCoorsetThumbRightCoor永远不会被触发。

任何人都知道为什么会这样?

1 个答案:

答案 0 :(得分:0)

setThumbLeftCoorsetThumbRightCoor作为私有方法适用于我的案例。

就在最近,一个子类来自上面的类。

要使上述代码按原样运行,需要将setThumbLeftCoorsetThumbRightCoor更改为 public 方法。 (即使受保护也没有工作)

我的猜测是,这是由于"反射"

的限制