Reputation: 480
I would like to rotate an imageView 10 times, every times, the imageView will load randomly an image in drawable resources. For example: there are 6 images like img1 to img6. I did the code like this, but it doesn't work
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);
}
It just load only one image that I set in xml file for 10 times of repeating
Upvotes: 1
Views: 328
Reputation: 480
Thanks KDeogharkar and Ragesh for your suggestion.
I've tried to used your way that is changing image onAnimationRepeat(), but it still doesn't work. I guess that is caused because the image is still being used by the animation, so it doesn't allow to change the image. Therefore, I write a recursive function that waits for the end of the animation to change image, and call the same animation, so finally it works. Here is my code
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);
}
After that, I just need to call rotate(animationTimes) in main() function.
Upvotes: 0
Reputation: 10959
instead of loop use Animation Listener.
ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise);
animation.setRepeatCount(10);
image.startAnimation(animation);
Overrided methods.
@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));
}
Upvotes: 1
Reputation: 3520
you can use animation listener like below
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});)
So in onAnimationRepeat method you can change your image.
Upvotes: 1