Roulette anim rotation get the location that it stop android

1.3k views Asked by At

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();
1

There are 1 answers

3
Ridcully On

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);