2012-06-24 60 views
-1

如何在animationlistener中使用變量在Animationlistener中使用變量

ex。

for(int i = 0; i<ids.length; i++) { 

    //Doing some animation stuff based on 
    //imageView = (ImageView) findViewById(ids[i]); 




//I want to play a sound on the specific views animationstart   
    scalePause.addListener(new AnimatorListenerAdapter() { 
    public void onAnimationStart(Animator animation) { 
    sounds.play(letterSounds[i], 1.0f, 1.0f, 0, 0, 1.0f); 
} 

}); }

Eclipse的錯誤:不能指非最終變量在不同的方法

(指i)所定義的內部類內部

回答

0

執行此

for(int i = 0; i<ids.length; i++) { 
    ... 
    ... 
    //I want to play a sound on the specific views animationstart 
    final int index = i;  // assign to a final variable and use it.  
    scalePause.addListener(new AnimatorListenerAdapter() { 
    public void onAnimationStart(Animator animation) { 
    sounds.play(letterSounds[index ], 1.0f, 1.0f, 0, 0, 1.0f); 
}