1

im trying to make a simple wheel of fortune in my android application. This will be a rounded image with person names. The image will start rotating(arround its center) when the button below it is pressed. The rotation needs to be stopped after a random time, so its not always the same person name ofcourse. for now i just use an image with 1-2-3-4, se below.

Output example

Ive looked at nearly every topic regarding this, but cant figure it out how to make it random. At the moment it always stops at the same angle, for instance always at number 1.

The code i have sofar:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_who, container, false);


   final ImageView alberto = (ImageView)view.findViewById(R.id.alberto);
    Button spin =(Button)view.findViewById(R.id.spin);

    final RotateAnimation anim = new RotateAnimation(0f,generateRandomNumber(), Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    anim.setFillAfter(true);
    anim.setDuration(1000);


    spin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alberto.startAnimation(anim);
        }
    });

    return view;
}

public float generateRandomNumber() {

    Random rand = new Random();
    int randomNum = rand.nextInt((360 - 0) + 1);

    return (float)randomNum;
}

}

So basicaly i give the RotateAnimation a random number, so its not always the same where it stops rotating. But this is not working becaus like i said it always stops on nr 1 for example. i figured out that if i change the duration of the animation the output is not the same! Therefor i tryed put a random number in in the duration but thats not working aswell.btw i have checked other posts that say its random but its not.

In the ideal situation the animation starts fast and slows down and stops.

Thanx allot in advanced!!!

1 Answers1

1

You could try the following:

private static final float BASE_ROTATION_DEGREES = 3600;
private static final int DURATION = 1000;

//...

spin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        float deg = alberto.getRotation() + BASE_ROTATION_DEGREES + ((float)Math.random() * 360F);
        alberto.animate().rotation(deg).setDuration(DURATION)
            .setInterpolator(new AccelerateDecelerateInterpolator());
    }
});

You could change the interpolator to something else as well to get a different effect.

Edit: Edited to fix the problem whereby only the first rotation was more than one revolution. Note that this means the rotation value will get high pretty quickly! Shouldn't pose an issue but worth keeping in mind.

Another Edit: Sample app/source code here if you wanted to take a look. This runs fine on my emulator.

PPartisan
  • 8,173
  • 4
  • 29
  • 48
  • Hello PPartisan, thanx allot for replying to my question! you helped me with my problem and the spinning is now random. I had to change the duration to 4000 to notice the Interpolator better ;) The second time i press the spin button it's not spinning anymore tho, just a little bit. Any ideas on why that would be??? it shoud just do the same not? – user2455311 Nov 03 '15 at 07:55
  • @user2455311 Hi, could you paste the code for your implementation of this method into your OP please, and leave a comment so I can take a look? I've tested this out my end and it seems to be working as expected, thanks. – PPartisan Nov 03 '15 at 11:21
  • hi, im using your code, only thing ive changed is the BASE_ROTATION_DEGREES to 5000 and the DURATION to 5500. Normaly It should just do the same animation again when the button is pressed again. perhaps becaus im doing this inside a fragment(in the oncreateView)? – user2455311 Nov 03 '15 at 12:19
  • @user2455311 Ah ok, I think I see what's going on. Because the animation is actually effecting the view's rotation property, you need to add the next rotation to the rotation that's already present (or reset the rotation to zero before initiating a spin). I'll update my answer. – PPartisan Nov 03 '15 at 16:44
  • @user2455311 I actually put together a sample app to further test the issue you described and figured I may as well put it on GitHub in case you wanted to download it or take a look at the source code. [Here's the link](https://github.com/PPartisan/WheelyGreatApp "WheelyGreatApp") – PPartisan Nov 03 '15 at 16:52