我在此代码中多次定义了动画。有没有更简单的方法可以做到这一点?

时间:2018-07-06 14:43:02

标签: android animation

我在这段代码中反复定义了动画。有没有更简单的方法可以做到这一点?代码按照我想要的方式工作,但是我想简化编码。能帮我吗?我应该如何遵循一种方法。 How to add wait time? 我编写了此标题中无法做到的代码,但是编写的代码要短得多

resimyukle();

            hediye=getDrawable(R.drawable.hediye);
            final ImageView[] imajlar={img1,img2,img3,img4,img5,img6,img7,img8};

                  Animation fadein=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
                  imajlar[0].setBackground(hediye);
                  imajlar[0].startAnimation(fadein);

                  Animation res=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
                  Animation res1=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
                  Animation res2=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
                  Animation res3=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
                  Animation res4=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
                  Animation res5=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
                  Animation res6=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);


                  anima(fadein,res,imajlar[1]);
                  anima(res,res1,imajlar[2]);
                  anima(res1,res2,imajlar[3]);
                  anima(res2,res3,imajlar[4]);
                  anima(res3,res4,imajlar[5]);
                  anima(res4,res5,imajlar[6]);
                  anima(res5,res6,imajlar[7]);
}


public void anima(final Animation aa, final Animation bb, final ImageView img)
{


    aa.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

            animation.cancel();
            img.setBackground(hediye);
            img.startAnimation(bb);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

}

1 个答案:

答案 0 :(得分:0)

hediye=getDrawable(R.drawable.hediye);
final ImageView[] imajlar={img1,img2,img3,img4,img5,img6,img7,img8};
Animation res=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
for (int i = 0; i < imajlar.size; i++) {
  anima(res, imajlar[i], i);
}

以及您的方法:

public void anima(Animation aa, ImageView img, int id)
{
    aa.setStartOffset(id * 500);
    aa.setDuration(1000);
    img.startAnimation(aa);
    img.setBackground(hediye);
}