在android中如何在AnimatorListener中的onAnimationEnd()方法中使用外部类的变量(不使其成为最终)

时间:2015-01-29 15:32:34

标签: android objectanimator

我正在尝试在另一个ImageView的翻译动画结束时更改ImageButton的图像。我有一个AnimatorListener用于动画,并希望在onAnimationEnd方法中传递ImageButton值,以便我知道要更改哪个ImageButton(在代码中进行一些计算以决定要更改哪个Imagebutton)。我的代码如下。请帮帮我!!!

//animation of an imageview
ObjectAnimator move =ObjectAnimator.ofFloat(myAnimation1,"translationX",20);

//calculating which image to change to for the ImageButton
int id2 = this.getResources().getIdentifier("r" + a[m], "drawable", this.getBaseContext().getPackageName());

//adding animtor listener

move.addListener(new Animator.AnimatorListener(ImageButton,id2) {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        // TODO Auto-generated method stub

                        ImageButton.setImageResource(id2); //Not able to use ImageButton and id2...says it has to be final but I don't want it to be final

                        ImageButton.setClickable(true);

                    }});

1 个答案:

答案 0 :(得分:0)

只需创建另一个最终变量吗?

int id2 = this.getResources().getIdentifier("r" + a[m], "drawable", this.getBaseContext().getPackageName());

final int idFinal = id2;

//adding animtor listener

move.addListener(new Animator.AnimatorListener(ImageButton,id2) {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        // TODO Auto-generated method stub

                        ImageButton.setImageResource(idFinal); //Not able to use ImageButton and id2...says it has to be final but I don't want it to be final

                        ImageButton.setClickable(true);

                    }});