在AnimationListener中使用For循环更改ImageViews - Android Studio

时间:2016-01-29 04:24:49

标签: java android animation android-studio

我想制作一个动画,其中一组ImageViews在用户按下按钮后一个接一个地淡入/淡出。我制作了一个名为list的图像(r1,r2和r3)数组,但它表示索引" i"必须宣布最终。有没有办法遍历数组,因此每个ImageView都是动画的?

  @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   button = (ImageButton) findViewById(R.id.button);
   r1 = (ImageView) findViewById(R.id.r1);
   r2 = (ImageView) findViewById(R.id.r2);
   r3 = (ImageView) findViewById(R.id.r3);
   final ImageView[] list = {r1, r2, r3};

      button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            button.setVisibility(View.INVISIBLE);
            animation1 = new AlphaAnimation(0.0f, 1.0f);
            animation1.setDuration(1000);
            animation1.setStartOffset(2000);

              for (int i = 0; i < list.length; i++) {    
                animation1.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationEnd(Animation arg0) {                     
                        list[i].startAnimation(animation2);
                    }

                    @Override
                    public void onAnimationRepeat(Animation arg0) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void onAnimationStart(Animation arg0) {
                        // TODO Auto-generated method stub

                    }

                });

                animation2 = new AlphaAnimation(1.0f, 0.0f);
                animation2.setDuration(1000);
                animation2.setStartOffset(2000);

                animation2.setAnimationListener(new Animation.AnimationListener() {

                    @Override
                    public void onAnimationEnd(Animation arg0) {
                        list[i].startAnimation(animation1);
                    }

                    @Override
                    public void onAnimationRepeat(Animation arg0) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void onAnimationStart(Animation arg0) {
                        // TODO Auto-generated method stub

                    }

                });

                list[i].startAnimation(animation1);

        }
    });
}}

}

0 个答案:

没有答案