As in the title, I want to make a rotating disc which will rotate when I hit play on mediaplayer. Here is the code for my ImageView (for now it moves onTouch) :
public class DjDiscsHandler {
public static double mCurrAngle = 0;
public static double mPrevAngle = 0;
public static void Discs(final ImageView djDisc, final String side) {
djDisc.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final float xc = djDisc.getWidth() / 2;
final float yc = djDisc.getHeight() / 2;
final float x = event.getX();
final float y = event.getY();
switch(side) {
case "left":
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
mCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));
break;
}
case MotionEvent.ACTION_MOVE: {
mPrevAngle = mCurrAngle;
mCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));
Log.i("Current Angle", "" + mCurrAngle);
animate(mPrevAngle, mCurrAngle, 0, djDisc);
break;
}
case MotionEvent.ACTION_UP: {
mPrevAngle = mCurrAngle = 0;
break;
}
}
break;
case "right":
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
mCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));
break;
}
case MotionEvent.ACTION_MOVE: {
mPrevAngle = mCurrAngle;
mCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));
animate(mPrevAngle, mCurrAngle, 0, djDisc);
break;
}
case MotionEvent.ACTION_UP: {
mPrevAngle = mCurrAngle = 0;
break;
}
}
break;
}
return true;
}
});
}
public static void animate(double fromDegrees, double toDegrees, long durationMillis, final ImageView djDiscLeft) {
final RotateAnimation rotate = new RotateAnimation((float) fromDegrees, (float) toDegrees,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(durationMillis);
rotate.setFillEnabled(true);
rotate.setFillAfter(true);
djDiscLeft.startAnimation(rotate);
}
}
I found the code for this here: How can i use RotateAnimation to rotate a circle?
Now I want to add it to my mediaplyer handler:
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Deck.mediaplayerReadyLeft) {
Deck.mediaplayerLeft.start();
double mCurrAngle = 0;
while(Deck.mediaplayerLeft.isPlaying()) {
DjDiscsHandler.animate(mCurrAngle, mCurrAngle + 1, 0, Deck.djDiscLeft);
mCurrAngle++;
}
}
}
});
Unfortunately the program freezes when I press play and crushes a few moments later.
Any suggestions?