2016-11-29 29 views
3

我建立一个自定义视图包含3个进度条,所以我有一个数组变量,像这样的:ObjetctAnimator与数组变量

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

,但我得到这样的警告:

我也试图与二传手包含数组(setProgress (float[] progress)),但仍得到了一个错误:Method setProgress() with type float not found on target class

所以我会很高兴知道如何使用ObjectAnimator数组变量,

感谢

+0

简单地用'ObjectAnimator#ofInt(对象目标,弦乐propertyName的,诠释.. 。values)' – pskink

+0

@pskink。谢谢,但我已经尝试它并得到:'方法setProgress()与类型浮动没有找到目标类',对不起,我没有写在问题,我也试过了... – AsfK

回答

0

一个LO后t的尝试,看起来可以使用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.

至于谷歌的意见,我试着用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(); 
}