简体   繁体   中英

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?

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!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM