重复动画时更改图像

时间:2016-03-26 06:43:25

标签: android animation

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

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);


    }

它只加载我在xml文件中设置的一个图像,重复10次

3 个答案:

答案 0 :(得分:1)

您可以使用下面的动画监听器

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

}

@Override
public void onAnimationEnd(Animation animation) {

}

@Override
public void onAnimationRepeat(Animation animation) {

}
});)

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

答案 1 :(得分:1)

而不是循环使用动画侦听器。

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));


    }

答案 2 :(得分:0)

感谢KDeogharkar和Ragesh的建议。

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

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)。