Shashank Setty
Shashank Setty

Reputation: 138

ImageView onClick animation doesn't work after change in resource

I'm new to android, and I am currently working on a simple connect three game, I have nine ImageViews which contain transparent images, when the ImageView is clicked, the resource is changed to x or o.

I've tried adding more animation, and setting resource to null rather than a transparent image, but it didn't work, only restarting the activity seems to fix it.

public void oneClick (View view)
{ 
    //image view onClick
    ImageView one = (ImageView) findViewById(R.id.one); 

    one.setImageResource(R.drawable.x);                   //setting x or y
    one.animate().rotation(180).setDuration(500);         //animation
} 

Here's how I reset the images,

one.setImageResource(R.drawable.transp)

After this, if the onClick activity is called again, the image is set. However the animation doesn't seem to work.

What am I doing wrong?

Upvotes: 0

Views: 181

Answers (2)

Shashank Setty
Shashank Setty

Reputation: 138

My image was rotated 180, so it wasn't rotating again, the simple solution was to reset the orentation of the image then run the rotate animation again, which seems to have fixed it!

clicked.setImageResource(R.drawable.x);
clicked.animate().rotation(180).setDuration(500);

after this

clicked.animate().rotation(0);

now if I use the rotation animation again, it seems to work!

Upvotes: 1

B. Plüster
B. Plüster

Reputation: 654

This is a simple fix. Simply add .start() to your animation. It should look like this afterward:

one.animate().rotation(180).setDuration(500).start();

Upvotes: 0

Related Questions