我整天都调试了以下代码,并且不知道为什么它不能按预期工作。
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
但是,方法setThumbLeftCoor
和setThumbRightCoor
永远不会被触发。
任何人都知道为什么会这样?
答案 0 :(得分:0)
让setThumbLeftCoor
和setThumbRightCoor
作为私有方法适用于我的案例。
就在最近,一个子类来自上面的类。
要使上述代码按原样运行,需要将setThumbLeftCoor
和setThumbRightCoor
更改为 public 方法。 (即使受保护也没有工作)
我的猜测是,这是由于"反射"
的限制