How To Randomly Rotate An Image In Android?
I'm new at Android Programing and I want to know how to rotate an image by a random angle. I need this for my 'Spin the Bottle' game. Here is my code so far: package example.com.b
Solution 1:
You can easily configure your animation in Java code as well as in xml. For your case you could use class RotateAnimation and its' constructor public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
So, try somehting like this:
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = (ImageView) findViewById(R.id.ivSpin);
bottle = (ImageView) findViewById(R.id.ivBottle);
floattoDegrees=newRandom().nextFloat() * Integer.MAX_VALUE % 360;
finalAnimationanimRotate=newRotateAnimation(0, toDegrees, 50, 50);
animRotate.setDuration(1000);
animRotate.setRepeatCount(1);
animRotate.setRepeatMode(Animation.REVERSE);
spin.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
bottle.startAnimation(animRotate);
}
});
}
I think, that no comments are required.
Post a Comment for "How To Randomly Rotate An Image In Android?"