带有数组变量的 ObjetctAnimator
ObjetctAnimator with array variable
我正在构建一个包含 3 个进度条的自定义视图,因此我有一个数组变量,如下所示:
float[] progress = new float[3];
并且我想使用 'ObjectAnimator' 更新特定的进度条目;以下是相关方法:
public void setProgress(int index, float progress) {
this.progress[index] = (progress<=100) ? progress : 100;
invalidate();
}
public void setProgressWithAnimation(int index, float progress, int duration) {
PropertyValuesHolder indexValue = PropertyValuesHolder.ofInt("progress", index);
PropertyValuesHolder progressValue = PropertyValuesHolder.ofFloat("progress", progress);
ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(this, indexValue, progressValue);
objectAnimator.setDuration(duration);
objectAnimator.setInterpolator(new DecelerateInterpolator());
objectAnimator.start();
}
但我收到此警告:Method setProgress() with type int not found on target class
我也尝试过 setter 包含一个数组 (setProgress (float[] progress)
) 但仍然出现错误:Method setProgress() with type float not found on target class
所以我很高兴知道如何在 ObjectAnimator 中使用数组变量,
谢谢
经过多次尝试,似乎可以使用 ObjectAnimator
来完成此操作。我还在 doc:
中找到了这个
The object property that you are animating must have a setter function
(in camel case) in the form of set(). Because the
ObjectAnimator automatically updates the property during animation, it
must be able to access the property with this setter method. For
example, if the property name is foo, you need to have a setFoo()
method. If this setter method does not exist, you have three options:
Add the setter method to the class if you have the rights to do so.
Use a wrapper class that you have rights to change and have that
wrapper receive the value with a valid setter method and forward it to
the original object.
Use ValueAnimator instead.
作为 Google 的建议,我尝试使用 ValueAnimator
并且效果很好:
public void setProgressWithAnimation(float progress, int duration, final int index) {
ValueAnimator valueAnimator = ValueAnimator.ofFloat(progress);
valueAnimator.setDuration(duration);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
setProgress((Float) valueAnimator.getAnimatedValue(), index);
}
});
valueAnimator.start();
}
我正在构建一个包含 3 个进度条的自定义视图,因此我有一个数组变量,如下所示:
float[] progress = new float[3];
并且我想使用 'ObjectAnimator' 更新特定的进度条目;以下是相关方法:
public void setProgress(int index, float progress) {
this.progress[index] = (progress<=100) ? progress : 100;
invalidate();
}
public void setProgressWithAnimation(int index, float progress, int duration) {
PropertyValuesHolder indexValue = PropertyValuesHolder.ofInt("progress", index);
PropertyValuesHolder progressValue = PropertyValuesHolder.ofFloat("progress", progress);
ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(this, indexValue, progressValue);
objectAnimator.setDuration(duration);
objectAnimator.setInterpolator(new DecelerateInterpolator());
objectAnimator.start();
}
但我收到此警告:Method setProgress() with type int not found on target class
我也尝试过 setter 包含一个数组 (setProgress (float[] progress)
) 但仍然出现错误:Method setProgress() with type float not found on target class
所以我很高兴知道如何在 ObjectAnimator 中使用数组变量,
谢谢
经过多次尝试,似乎可以使用 ObjectAnimator
来完成此操作。我还在 doc:
The object property that you are animating must have a setter function (in camel case) in the form of set(). Because the ObjectAnimator automatically updates the property during animation, it must be able to access the property with this setter method. For example, if the property name is foo, you need to have a setFoo() method. If this setter method does not exist, you have three options:
Add the setter method to the class if you have the rights to do so.
Use a wrapper class that you have rights to change and have that wrapper receive the value with a valid setter method and forward it to the original object.
Use ValueAnimator instead.
作为 Google 的建议,我尝试使用 ValueAnimator
并且效果很好:
public void setProgressWithAnimation(float progress, int duration, final int index) {
ValueAnimator valueAnimator = ValueAnimator.ofFloat(progress);
valueAnimator.setDuration(duration);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
setProgress((Float) valueAnimator.getAnimatedValue(), index);
}
});
valueAnimator.start();
}