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();
Related
I'm a beginnner in Xamarin Android programming as well as in Android programming in general. I successfully run the following code with expected effect:
//in the context of the main Activity
StartActivity(someIntent);
OverridePendingTransition(Android.Resource.Animation.SlideInLeft,
Android.Resource.Animation.SlideOutRight);
Now I would like to create my own animations for sliding from left and sliding out from right using XML declaration. I put my XML files under the anim folder with names slideInLeft.xml and slideOutRight.xml respectively. Here are the files' content:
slideInLeft.xml:
<?xml version="1.0" encoding="utf-8" ?>
<translate xmlns:android="http://schemes.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="-100%"
android:toXDelta="0%">
</translate>
slideOutRight.xml:
<?xml version="1.0" encoding="utf-8" ?>
<translate xmlns:android="http://schemes.android.com/apk/res/android"
android:duration="350"
android:fromXDelta="0%"
android:toXDelta="100%">
</translate>
Now the code is just simply changed to this:
//in the context of the main Activity
StartActivity(someIntent);
OverridePendingTransition(Resource.Animation.SlideInLeft,
Resource.Animation.SlideOutRight);
But the animation is not working, the new Activity is just shown after a short of delay (looks like being equal the duration of sliding in which is 300ms).
This is confusing me. I don't know why and how to make this work.
Try changing your xml files to:
slideInLeft
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0"
android:duration="350"/>
</set>
AND
slideOutRight
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="100%p"
android:duration="350"/>
</set>
This should work.
You try to set duration more longer to see the transition :)
slideInLeft.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="5000"
android:fromXDelta="-100%"
android:toXDelta="0%"/>
<alpha android:duration="5000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
slideOutRight.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="5000"
android:fromXDelta="0%"
android:toXDelta="100%"/>
<alpha android:duration="5000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
I am creating an Android application. I want to open the new activity with zoom in transition. I have some buttons in my current activity. When I click one button new activity will open from the button's position. I try to use the following code. But no transition will appear. I am using the following xml and java codes.
zoom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="70%"
android:pivotY="70%"
android:toXScale="3"
android:toYScale="3" >
</scale>
</set>
and zoom_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="70%"
android:pivotY="70%"
android:toXScale="0.5"
android:toYScale="0.5" >
</scale>
</set>
And in my java code I call it like
overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);
With these code no effect will appear. Could someone please help me to finds out what is the issue?
Try to add the attribute: android:fillAfter="true"
Like this:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="3"
android:toYScale="3" >
</scale>
</set>
place both animation xml files zoom_in.xml and zoom_out.xml in res/anim
directory and start animation like this :
overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);
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 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);