在Animationlistener中使用Variable

时间:2012-06-24 18:47:57

标签: android events animation

如何在animationlistener中使用变量

离。

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错误:不能引用在不同方法中定义的内部类中的非final变量

(指i)

1 个答案:

答案 0 :(得分: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);
}