Making a flipping Android animation - java

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

Related

Android overridePendingTransition() not working

I am trying to animate the transition between two Activities with a simple fade animation.
When I use the standard android animations it works fine, but when I define my own animations, despite them being the exact same thing, it does not work and i don't see a transition.
Working code
Method call
((Activity) context).overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
Android animation (fade_in.xml)
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#interpolator/decelerate_quad"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="#android:integer/config_longAnimTime" />
Non working code
Method call
((Activity) context).overridePendingTransition(R.anim.activity_fade_in, R.anim.activity_fade_out);
Custom animation (res/anim/activity_fade_in.xml)
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromAlpha="0.0"
android:interpolator="#android:interpolator/decelerate_quad"
android:toAlpha="1.0"/>
For some reason, unplugging my test device and coming back an hour later seems to have resolved the issue. I'm not quite sure what happened but everything seems to be working properly now.
Change your code this way
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/bounce_interpolator" >
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromAlpha="0.0"
android:interpolator="#android:interpolator/decelerate_quad"
android:toAlpha="1.0"/>
</set>

how do I animate transitions between multiple XML files?

I have one activity for my app, which uses multiple XML files and setContentView to transition between each page. I want to add animations to the transition, however setContentView always puts the layout ontop of the other (and removes the other one). I've googled for about an hour, and I can't find anything that works with multiple XML files. I found a way to use addContentView to add the layout to the page, but I forgot how to and have been searching for that again, with no success.
How would I go about animating the transition between XML files?
You can use below example:
slide_in_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<translate android:fromXDelta="-100%"
android:toXDelta="0%" android:fromYDelta="0%"
android:toYDelta="0%" android:duration="400">
</translate>
</set>
slide_out_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true" >
<translate
android:duration="400"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="-100%"
android:toYDelta="0%" >
</translate>
</set>
In startActivity() use following code:
startActivity(new Intent(CurrentActivity.this, TargetActivity.class));
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
finish();

Animate a TextView to slide off the screen

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

XML animation accelerates and decelerates at beginning and end, despite using linear_interpolator

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.

android activity open with swap motion

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

Categories