Android 重复动画时更改图像

Android 重复动画时更改图像,android,animation,Android,Animation,我想旋转一个imageView 10次,每次,imageView都会在可绘制资源中随机加载一个图像。例如:有6个像img1到img6这样的图像。我这样做的代码,但它不工作 public void clockwise(View view){ for (int i=1; i<=6; i++){ ImageView image = (ImageView)findViewById(R.id.imageView); Animation animation =

我想旋转一个imageView 10次,每次,imageView都会在可绘制资源中随机加载一个图像。例如:有6个像img1到img6这样的图像。我这样做的代码,但它不工作

public void clockwise(View view){

    for (int i=1; i<=6; i++){
        ImageView image = (ImageView)findViewById(R.id.imageView);
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise);
        animation.setRepeatCount(10);

        int max=6, min=1;
        int randNum = min + (int)(Math.random()*((max-min)+1));//
        if (randNum==1) image.setImageDrawable(getResources().getDrawable(R.drawable.die1));
        else if (randNum==2) image.setImageDrawable(getResources().getDrawable(R.drawable.die2));
        else if (randNum==3) image.setImageDrawable(getResources().getDrawable(R.drawable.die3));
        else if (randNum==4) image.setImageDrawable(getResources().getDrawable(R.drawable.die4));
        else if (randNum==5) image.setImageDrawable(getResources().getDrawable(R.drawable.die5));
        else if (randNum==6) image.setImageDrawable(getResources().getDrawable(R.drawable.die6));

        image.startAnimation(animation);


    }
public void顺时针(视图){

对于(inti=1;i,您可以像下面这样使用动画侦听器

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

}

@Override
public void onAnimationEnd(Animation animation) {

}

@Override
public void onAnimationRepeat(Animation animation) {

}
});)

因此,在onAnimationRepeat方法中,您可以更改图像。

使用动画侦听器代替循环

ImageView image = (ImageView)findViewById(R.id.imageView);
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise);
        animation.setRepeatCount(10);

 image.startAnimation(animation);
重写的方法

@Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {

    }

    @Override
    public void onAnimationRepeat(Animation animation) {

int max=6, min=1;
        int randNum = min + (int)(Math.random()*((max-min)+1));//
        if (randNum==1) image.setImageDrawable(getResources().getDrawable(R.drawable.die1));
        else if (randNum==2) image.setImageDrawable(getResources().getDrawable(R.drawable.die2));
        else if (randNum==3) image.setImageDrawable(getResources().getDrawable(R.drawable.die3));
        else if (randNum==4) image.setImageDrawable(getResources().getDrawable(R.drawable.die4));
        else if (randNum==5) image.setImageDrawable(getResources().getDrawable(R.drawable.die5));
        else if (randNum==6) image.setImageDrawable(getResources().getDrawable(R.drawable.die6));


    }

谢谢KDeogharkar和Ragesh的建议

我试着用你的方式改变AnimationRepeat()上的图像,但它仍然不起作用。我想这是因为动画仍在使用图像,因此它不允许更改图像。因此,我编写了一个递归函数,等待动画结束时更改图像,并调用相同的动画,因此它最终起作用。这是我的代码

public void rotate(int count){

        //
        final int nextCount = count - 1;
        ImageView image = (ImageView)findViewById(R.id.imageView);
        int max = 6, min = 1;
        int randNum = min + (int) (Math.random() * ((max - min) + 1));//
        if (randNum == 1)
            image.setImageDrawable(getResources().getDrawable(R.drawable.die1));
        else if (randNum == 2)
            image.setImageDrawable(getResources().getDrawable(R.drawable.die2));
        else if (randNum == 3)
            image.setImageDrawable(getResources().getDrawable(R.drawable.die3));
        else if (randNum == 4)
            image.setImageDrawable(getResources().getDrawable(R.drawable.die4));
        else if (randNum == 5)
            image.setImageDrawable(getResources().getDrawable(R.drawable.die5));
        else if (randNum == 6)
            image.setImageDrawable(getResources().getDrawable(R.drawable.die6));


        Animation animation;
        animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise);

        animation.setRepeatCount(1);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                if (nextCount >=0) rotate(nextCount);

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        image.startAnimation(animation);
    }
之后,我只需要在main()函数中调用rotate(animationTimes)