In trying to add animation to ImageViews, I don't seem to get the multithreading right which is giving me incorrect results.
This is the part where I am calling the animation >>
startAnimation(gameController.getSecondImage()); // 1
...
startAnimation(gameController.getFirstImage()); // 2
...
startAnimation(gameController.getSecondImage()); // 3
...
The problem here is that when I was not creating a separate thread, for the startAnimation()
function, all 3 calls happened simultaneously and everything got messed up. But still now it is not working perfectly and the 1st & 3rd calls are jumbled up.
public void startAnimation(final ImageView image1) {
final ImageView image2 = cardViewMap.get(cardSwapMap.get(image1.getId()));
Runnable animate = new Runnable() {
@Override
public void run() {
animationController.animate(image1, image2);
}
};
Thread animationThread = new Thread(animate);
animationThread.start();
try { //
animationThread.join(); //
} catch (InterruptedException e) { // Added Later
Log.d("ERROR", e.toString()); //
} //
}
I tried to experiment with join()
but it didn't work out, it is giving more radical outcomes now. I thought that using join()
will ensure excution of the previous calls to startAnimation()
before starting a new.
EDIT: I have tracked the issue and the issue is that my startAnimation()
function is being called when the main Looper is running. So it is being executed after all three calls to the function has been made. I need to ensure that startAnimation()
runs at the time when I make the call, and is executed before the next call to the same function happens. Any ideas how to ensure this?
I tried and failed using Android tools. I went back and fixed it with multithreading.
Though there is a lot of other code that I changed, this is one of the important one w.r.t. my issue-
package thakur.rahul.colourmemory.controller;
import thakur.rahul.colourmemory.view.Animation.DisplayNextView;
import thakur.rahul.colourmemory.view.Animation.Flip3dAnimation;
import android.app.Activity;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
public class AnimationController implements Runnable {
private Activity activity;
private ImageView image1;
private ImageView image2;
private boolean isFirstImage = true;
private volatile static int numThread = 1;
private volatile static int threadAllowedToRun = 1;
private int myThreadID;
private volatile static Object myLock = new Object();
private volatile static boolean hasNotSlept = true;
public volatile static boolean fixForMatch = false;
public AnimationController(Activity activity, ImageView image1, ImageView image2) {
this.activity = activity;
this.image1 = image1;
this.image2 = image2;
this.myThreadID = numThread++;
}
@Override
public void run() {
synchronized (myLock) {
while (myThreadID != threadAllowedToRun)
try {
myLock.wait();
} catch (InterruptedException e) {
} catch (Exception e) {
}
animate();
if (myThreadID % 2 == 0)
if (hasNotSlept || fixForMatch)
try {
Thread.sleep(1000);
hasNotSlept = !hasNotSlept;
} catch (InterruptedException e) {
}
else
hasNotSlept = !hasNotSlept;
myLock.notifyAll();
threadAllowedToRun++;
}
}
public void animate() {
if (isFirstImage) {
applyRotation(0, 90);
isFirstImage = !isFirstImage;
} else {
applyRotation(0, -90);
isFirstImage = !isFirstImage;
}
}
private void applyRotation(float start, float end) {
final float centerX = image1.getWidth() / 2.0f;
final float centerY = image1.getHeight() / 2.0f;
final Flip3dAnimation rotation = new Flip3dAnimation(start, end, centerX, centerY);
rotation.setDuration(300);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(isFirstImage, image1, image2));
if (isFirstImage)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
image1.startAnimation(rotation);
}
});
else
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
image2.startAnimation(rotation);
}
});
}
}
This is being called from the main controller as-
public void startAnimation(ImageView image1) {
ImageView image2 = cardViewMap.get(cardSwapMap.get(image1.getId()));
animationController = new AnimationController(activity, image1, image2);
animationThread = new Thread(animationController);
animationThread.start();
}
Basically, every time I need to run the animation, I make and tag the thread so I can run it in order.
Please add your answer if you have a better solution.