android activity open with swap motion - java

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

Related

New fragment transitioning over white background instead of old fragment

I am trying to transition between two fragments using this animation.
Problem I have is when I transition using the following code. The animation slides in the new fragment over a white background instead of the old fragment.
I want to either use .replace or create the exact same functionality as replace.
I also want the animation to slide the new fragment over the old fragment.
FragmentTransaction transaction = MainActivity.instance.getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right, R.anim.slide_in_right, R.anim.slide_out_right);
transaction.replace(R.id.main_frame_content, fragment);
transaction.commit();
try {
fragmentManager.executePendingTransactions();
} catch (Exception e) {
}
Animations:
Slide in right:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
slide out right:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>
The tricky part is you have to define all the Anim field in the action.
something like bellow:
<fragment
android:id="#+id/profileFragment"
android:name="sample.presentation.view.fragments.profile.ProfileFragment"
android:label="fragment_profile"
tools:layout="#layout/fragment_profile" >
<action
android:id="#+id/action_profileFragment_to_personalInfoFragment"
app:destination="#id/personalInfoFragment"
app:enterAnim="#android:anim/slide_in_left"
app:exitAnim="#android:anim/slide_out_right"
app:popEnterAnim="#android:anim/slide_in_left"
app:popExitAnim="#android:anim/slide_out_right"/>
</fragment>

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

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.

Making a flipping Android animation

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

Categories