How can i stop a animation after it has completed?
My animation is the image fades in
code:
Java:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageView= (ImageView)findViewById(R.id.tCat);
Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.anim);
imageView.startAnimation(fadeInAnimation );
xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="5000"
android:repeatCount="infinite"/>
</set>
Try removing the android:repeatCount="infinite", since you do not want it to repeat.
If it is a non repeating animation it will end when it is done otherwise you can just set the repeat counter to 0 to let it end after the current execution.
fadeInAnimation.setRepeatCount( 0 );
Related
I'm animating a TextView with this code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:toYDelta="100%"
android:fromYDelta="0%" />
</set>
and doing this:
textView.startAnimation(slideOutAnim);
But this code only makes the TextView animate about 1/4 of the screen length. What should I set the toYDelta to so that it ensures that the TextView is out of the screen? I know I could just set it to a really high number but is there a more efficient/effective way to get this done? Thanks
change your animation to this:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:toYDelta="100%p"<!--relative to parent-->
android:fromYDelta="0%" />
</set>
note that this work if it's parent have full screen height, actually it animate to 100% of height of it's parent
I need my two animations to repeat continuously, with no pause in between. However, every time, it accelerates at the beginning of the animation, and decelerates towards the end.
My XMLs:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="10000"
android:fromYDelta="-100%p"
android:interpolator="#android:anim/linear_interpolator"
android:repeatCount="infinite"
android:startOffset="0"
android:toYDelta="-1%p" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="10000"
android:fromYDelta="0%p"
android:interpolator="#android:anim/linear_interpolator"
android:repeatCount="infinite"
android:toYDelta="99%p"
android:startOffset="0" />
</set>
Any help appreciated.
Note: The Deltas were originally at 100 instead of 99, and 0 instead of 1, but it didn't help the case.
Put the android:interpolator tag up in the main set tag.
< set xmlns:android="schemas.android.com/apk/res/android"; android:interpolator="#android:anim/linear_interpolator" >
So if you look the source code the AnimationSet(context, resource file) constructor, defaultly sets the shared interpolator to true ie. Pass true if all of the animations in this set should use the interpolator associated with this AnimationSet. Pass false if each animation should use its own interpolator. Hence you need to put the interpolator on the set and not the animation piece itself.
I want to do a multiple animations on my image (appear -> rotate -> disappear). I've got this code:
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false" >
<alpha
android:duration="1"
android:fromAlpha="0"
android:toAlpha="100" />
</set>
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false" >
<alpha
android:duration="1"
android:fromAlpha="100"
android:toAlpha="0" />
</set>
image_rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false" >
<rotate
android:duration="2500"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="120" />
</set>
Also in my java code:
animRotate= AnimationUtils.loadAnimation(context, R.anim.image_rotate);
animRotate.setDuration((long) duration);
fade_in = AnimationUtils.loadAnimation(context, R.anim.fade_in);
fade_out = AnimationUtils.loadAnimation(context, R.anim.fade_out);
AnimationSet s = new AnimationSet(false);
s.addAnimation(fade_in);
s.addAnimation(animRotate);
s.addAnimation(fade_out);
image.startAnimation(s);
but unfortunately it doesn't work correctly...
you have severals errors in your animation xml files:
the duration property is in milliseconds, so 1ms is way too short for a noticeable fade in/fade out animation
the alpha property is a float between 0 et 1, 100 is way too much.
you don't need a set in your xml files if there is only one animation : just add the alpha or rotate tag as a root
So, you should have now these files:
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1" />
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="1"
android:toAlpha="0" />
image_rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2500"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="120" />
Then, in your code, you need a add an offset between each animation. Otherwise, all the animations will be triggered at the same time. Moreover, the fillAfter flag must be set on the root animation object (here, your AnimationSet)
Animation animRotate= AnimationUtils.loadAnimation(context, R.anim.image_rotate);
Animation fade_in = AnimationUtils.loadAnimation(context, R.anim.fade_in);
Animation fade_out = AnimationUtils.loadAnimation(context, R.anim.fade_out);
AnimationSet s = new AnimationSet(false);
s.addAnimation(fade_in);
animRotate.setDuration((long) duration);
animRotate.setStartOffset(fade_in.getDuration());
s.addAnimation(animRotate);
fade_out.setStartOffset(fade_in.getDuration() + animRotate.getDuration());
s.addAnimation(fade_out);
s.setFillAfter(true);
image.startAnimation(s);
I am very weak with animations in Android. I need to make my Fragments "flip" just 100% equal to the ViewPager animation.
Here is my slide_in_left.xml code
<?xml version="1.0" encoding="utf-8"?>
<set>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-100%"
android:toXDelta="0"
android:interpolator="#android:anim/decelerate_interpolator"
android:duration="5000"/>
</set>
Here is my **slide_out_right.xml** code
<?xml version="1.0" encoding="utf-8"?>
<set>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="-100%"
android:interpolator="#android:anim/decelerate_interpolator"
android:duration="5000"/>
</set>
But this is not equal to ViewPager sliding animation. Instead, the existing Fragment exists from left and the new Fragment enters from left.
Apart from that, I need this animation to exit the existing fragment from left and new one to enter from right. How can I fix this issue?
Don't do any changes in slide_out_right.xml. But in slide_in_left.xml, just replace
android:fromXDelta="-100%"
with
android:fromXDelta="100%"
First, in your slide_out_right.xml, change
android:toXDelta="-100%p"
to
android:toXDelta="100%p"
And I think you should as well set animation for slide to right, a slide_in_right.xml and slide_out_left.xml
see FragmentTransaction#setCurrentAnimations
Can I open an android activity with something like a swap motion instead of the default one.
By default when I call one activity from another the new one is open from the middle and kind of grows to the match the screen size, I want the activities in my application to open from the side and cover the activity before them.
Is it possible?
yes you can do this by cutom animation, as i have done that here:
put activity_push_up_in.xml in your anim folder :-
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0" />
</set>
now, put another xml push_up_out.xml again in your anim folder :-
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromYDelta="0"
android:toYDelta="-100%p" />
</set>
Now put this code in your java file to start activity with this animation :-
overridePendingTransition(R.anim.activity_push_up_in, R.anim.push_up_out);
Short answer: Yes, it is possible
In API level 5 you can call the overridePendingTransition method specify an explicit transition animation.
startActivity();
overridePendingTransition(R.anim.hold, R.anim.some_animation);
You of course will need to create the animation you wish to use in an animation xml.
use this
overridePendingTransition(0, android.R.anim.slide_out_right);
You need to use animations and override the current animation when a new Activity is started.
Take a look at this answer Slide right to left Android Animations
as i have done Like:
put activity_push_up_in.xml in your anim folder :-
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="200"
android:fromYDelta="100%p"
android:toYDelta="0" />
</set>
now, put another xml push_up_out.xml again in your anim folder :-
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="200"
android:fromYDelta="0"
android:toYDelta="-100%p" />
</set>
Now put this code in your java file to start intent activity with this animation :-
overridePendingTransition(R.anim.activity_push_up_in, R.anim.push_up_out);