Roulette anim rotation get the location that it stop android - android

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

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

Related

Android animation rotate infinite, without pausing

I can rotate my image infinitely. But my problem is that the image pauses shortly when it reaches 360º and then starts rotating again. It happens the same even when I applied "linear_interpolator".
What I want to do is that the image does not pause at all when it starts the next round. So it has to rotate infinitely with same speed at any degree.
Here is my - code. Thanks
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:interpolator="#android:anim/linear_interpolator"
android:duration="1400"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="360"
android:repeatMode="restart"
android:repeatCount="infinite" />
</set>
How I call it on my code
rotate= AnimationUtils.loadAnimation(context, R.anim.loop_rotate)
binding.imgSecondLayout.startAnimation(rotate)
Thanks for help! :)
Add animation.setRepeatCount(Animation.INFINITE) to your java class where animation is called.
My final code is given here:
Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.loop_rotate);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(Animation.INFINITE);
animation.setDuration(1400);
youractivity.startAnimation(animation);
This is due to the small delay after the animation completes its duration (1400 ms in ur case). you can remove this delay for smooth animation.
Remove repeatMode attribute and instead add this line :
android:startOffset="0" //Delay in milliseconds before the animation runs
The animation will be smooth without any delays

How to rotate image slowly in android?

I want to rotate an Image in a slow way on Android.
I can do this by creating a Bitmap and by the
help of of Matrix class. But i don't know how to make it slow, like it should take 3 seconds to rotate.
Rotate
Rotate animation uses tag. For rotate animation required tags are android:fromDegrees and android:toDegrees which defines rotation angles.
Clock wise – use positive toDegrees value
Anti clock wise – use negative toDegrees value
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="600"
android:repeatMode="restart"
android:repeatCount="infinite"
android:interpolator="#android:anim/cycle_interpolator"/>
</set>
Save in anim folder
public class AnimationActivity extends Activity{
ImageView img;
Animation rotate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fadein);
img = (ImageView) findViewById(R.id.myimageid);
// load the animation
rotate = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.rotate);
img.startAnimation(rotate);
}
}
Complete Tutorial
In Kotlin you can use the ObjectAnimator do this super easily. For example:
ObjectAnimator.ofFloat(view, "rotationX", 180f).apply {
duration = 2000
start()
}
view: the view you want to rotate
"rotationX": the propertyName you want to alter.
"rotationX" gives you rotation into and out of the screen
"rotationY" gives you clockwise/counterclockwise rotation
180f: how many degrees you want to rotate the view
duration: the number of milliseconds the animation should take to complete
You can use rotate animation to achieve this.
Create anim folder under res directoy inside that place this xml.
rotate_around_center_point.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:repeatMode="restart"
android:toDegrees="360" />
</set>
Set animation to view like this.
ImageView animationTarget = (ImageView) this.findViewById(R.id.testImage);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_around_center_point);
animationTarget.startAnimation(animation);

can only perform rotation of image view without holding it

Hi guys i uses the code below to perform rotation but it only rotates one time and goes back to its original position, how to let the image stay at the rotated position?
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:startOffset="0"
/>
xml file of my code
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate);
a.startAnimation(rotation);
use:
anim.setFillAfter(true);
If fillAfter is true, the transformation that this animation performed
will persist when it is finished. Defaults to false if not set. Note
that this applies to individual animations and when using an
AnimationSet to chain animations.

Rotation to the right and left, like a pendulum - animation

I am creating an application and I'm including an animation. It is a rotation to the right, and what I am looking for is that in the same way that rotates clockwise, rotate left and return to the starting point as a pendulum (all with the same speed), only one time.
This is the animation:
<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromDegrees="0"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="50"
android:fillEnabled="true"
android:fillAfter="true"
/>
This animation begins when a previous animation ends:
AnimationListener animationInListener = new AnimationListener(){
#Override
public void onAnimationEnd(Animation animation) {
tv2.setText("GAME OVER");
count=count-count;
duracionAnim=2000;
tv1.startAnimation(animationRotateR);
tv1.startAnimation(animationRotateL);
}
I tried to make another animation with android: fromDegrees = "50" and android: toDegrees = "0", but not how to make it work one after the other.
I searched but did not find anything. Someone could help me?

android rotate animation, make it fluid

I have an ImageView that should rotate for 4 times. This is my xml of animation:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromDegrees="0"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="3"
android:repeatMode="restart"
android:toDegrees="360" />
but rotation is not fluid: there's a little delay from end of a rotation, and begin of next one. How can i fix it?
Try to rotate from 0 to 359. 0 and 360 is the same in that context.
why not create an animation the usual way, and if you want it to rotate four times, then create a loop that iterates four times, and for each iteration, it calls the animation. You have to optimize your code so that are no visible drags.

Categories

Resources