2016-11-29 31 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(); 
}