0

I try to make the game of spin the wheel in Android. I find it hard to rotate the circle in the right way. I'd love help how to rotate the circle and stop it so I get a different result each time. At the moment it turns a defined amount of times and returns to the beginning. If I stop in the middle of it by this msmallWheelBack.clearAnimation() it returns to the start of the round. Thank you in advance for your help.

In my code:

    Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_around_center_point);
    mframeWheelBig.startAnimation(animation);

The anim xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<rotate
    android:duration="2500"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:toDegrees="360"
    android:fromDegrees="0"
    android:fillAfter="true"
    android:fillBefore="true"
    android:fillEnabled="true"/>
</set>

And like I said this is how I stop the animation

    mframeWheelBig.clearAnimation();
gal
  • 71
  • 1
  • 12

1 Answers1

0

You could programmatically set a random value for 'toDegrees'. I guess you can set more than 360 degrees too, to get more spins.

However, I didn't find a setter method for that value, so I guess you'll have to create the animation programmatically, using RotateAnimation, but still it should be quite easy to to.

This would rotate between 3 and 6 turns and could stop at any angle.

final static int MIN_TURNS = 3;
final static int MORE_TURNS = 3;
float toDegrees = 360 * MIN_TURNS + Math.random() * 360f * MORE_TURNS;
Animation anim = new RotateAnimation(0, toDegrees);
Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • The big problem is that it is stopping in the place it started – gal Oct 14 '15 at 09:35
  • Your animation stops at the place where it started, because you defined an end position of 360 degrees, which is exactly one complete turn around. By providing a random value for the end position this will not happen. – Ridcully Oct 14 '15 at 09:39
  • int MIN_TURNS = 3; int MORE_TURNS = 3; float toDegrees = 360 * MIN_TURNS + (float)Math.random() * 360f * MORE_TURNS; Log.i("toDegrees"," "+toDegrees); Animation animation = new RotateAnimation(0, toDegrees,200,200); animation.setInterpolator(new AccelerateDecelerateInterpolator()); animation.setDuration(1000); animation.setFillAfter(true); – gal Oct 14 '15 at 09:56