How to rotate image slowly in android? - 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);

Related

Animating custom view(leaf node in ViewHierarchy ) using ObjectAnimator is not smooth in android

I am working in one slides based application and have created a shape using custom RelativeLayout and that is is very deep node in my application(position is about 90th level in viewHierarchy).And the shape is as content of the slides.
And while trying to animate translate , rotate or scale property of the shape using ObjectAnimator or ViewPropertyAnimator.The animation is not smooth. its flickering.
Here is the sample code snip
PropertyValuesHolder pvhTX,pvhTY;
PropertyValuesHolder.ofFloat(View.TRANSLATION_X,translateXFrom,translateXTo);
pvhTY = PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, translateYFrom, translateYTo);
anim1 = ObjectAnimator.ofPropertyValuesHolder(shape, pvhTX, pvhTY);
anim1.setDuration((long) (animData.getDetail().getDuration() * 1000));
// Adding listener to override onAnimationStart,onAnimationEnd according to my requirement.
anim1.addListener(new AnimationListener(shape, animData, slideEventHandler));
anim1.start();
In the above code
Shape is a customeView(custome relativlayout) it may be any geometrical shape like line circle cube triangle etc.
Please help me!!!
Probably you are using some images in view you are animating, or it moves some images that are animating. If you used drawable folder for them then try to move them to folder drawable-nodpi. Coz when u use drawable dir it every time makes dpi transformation so it can cause low performance when animating.
here is sample:
public static void applyAnimation(Context context, View view, int animationResource, int animationOffsetMilisec){
Animation anim = android.view.animation.AnimationUtils.loadAnimation(context, animationResource);
anim.setStartOffset(animationOffsetMilisec);
anim.setFillAfter(true);
if(view != null) {
view.setAnimation(anim);
view.startAnimation(anim);
}
}
then sample_animation.xml add to res.anim.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="4000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:startOffset="0"
android:toDegrees="360"
android:interpolator="#android:anim/linear_interpolator"/>
<scale
android:duration="100"
android:fromXScale="0.0"
android:toXScale="1.1"
android:toYScale="1.1"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="#android:anim/accelerate_interpolator"/>
<!--<alpha-->
<!--android:fromAlpha="0.0"-->
<!--android:toAlpha="1.0"-->
<!--android:duration="1000" />-->
<scale
android:duration="100"
android:fromXScale="1.1"
android:toXScale="0.91"
android:toYScale="0.91"
android:fromYScale="1.1"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="100"
android:interpolator="#android:anim/accelerate_interpolator"/>
</set>
usage:
applyAnimation(context, yourview, R.anim.sample_animation, 0);

Rotate animation to an ImageView in FrameLayout does not take effect

I have one ImageView in FrameLayout, and apply rotate animation to it. However, the animation does not take effect.
The animation resource file anim_blog.xml(located in res/anim):
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator" >
<rotate
android:fromDegrees="0.0"
android:toDegrees="360.0"
android:pivotX="50%p"
android:pivotY="50%p"
android:repeatCount="infinite"
android:duration="1200" />
</set>
The code in Activity is listed below:
Animation rotateAnim = AnimationUtils.loadAnimation(mCtx, R.anim.anim_blog);
rotateAnim.setDuration(Integer.MAX_VALUE);
mProgressIV.startAnimation(rotateAnim);
You are setting animation duration too large, change it as follows :
Animation rotateAnim = AnimationUtils.loadAnimation(mCtx, R.anim.anim_blog);
mProgressIV.startAnimation(rotateAnim);
please try what has done successfully from my end:
ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_blog);
image.startAnimation(animation);
I created example here, you can check it out.

Roulette anim rotation get the location that it stop 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);

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?

Categories

Resources