I want to add animation so that my imageview slide towards the left,leave the screen and enter from the right ,sliding back to its original position. I tried doing something like this ..
<translate
android:duration="1000"
android:fromXDelta="0"
android:toXDelta="-100%p" />
<translate
android:startOffset="1000"
android:duration="1000"
android:fromXDelta="100%p"
android:toXDelta="0" />
But the animation is not as per my wish.. Can anyone help me out
Edit: Okay so what you are trying to do is a pain in the a** (ye another one of those android things that should have been simple)! Having two animations after each other just doesn't pan out too well on earlier versions of android. On never versions you can use animationset from api lvl 11. Example here. Alternatively I'd go with a simpler animation.
Here is how to do slide in/out for activity (old answer):
Slide in left activity:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100%p" android:toXDelta="0"
android:duration="#android:integer/config_shortAnimTime"/>
Slide in right activity:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-100%p" android:toXDelta="0"
android:duration="#android:integer/config_shortAnimTime"/>
Slide out left activity:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="#android:integer/config_shortAnimTime" />
Slide out right activity:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0" android:toXDelta="100%p"
android:duration="#android:integer/config_shortAnimTime" />
Example usage:
Intent intent = new Intent(this, YourNewActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
Example usage on back:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
super.onBackPressed();
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
}
return super.onKeyDown(keyCode, event);
}
As Warpzit said, its a bug and is a known issue... as in here
I wasted my time thinking it was a mistake from my side.An alternative specified by the developer is--
"You can achieve it by using two Animations. Start the first one and
when it ends (using an animation listener to be notified), start the
second one. It's not as nice and easy but it should work."
and another thing i learned is that android honeycomb has more animation features than the old versions..Inorder to use these features in pre-honeycomb versions we may use nineoldandroids
Related
I have many activities with each having a group of textViews. what i want to
1) when activity loaded all the textViews should slide in.
2) when second activity loads the textViews of first activity slides out.
However, I already figured out the transition/animation, and also achieved the solution for first by calling the animation in onWindowFocusChanged(boolean hasFocus).
but I am unable to find the correct method through which I have to called slide out part.
this is my slide_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500"/>
</set>
this is my slide_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="0%" android:toXDelta="-100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500"/>
</set>
Here I call slide in and slide out but slide out is not working.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
animShow();
}
else
{
animHide();
}
}
this is my animHide method
private void animHide()
{
etname.startAnimation(animHide[0]);
etmail.startAnimation(animHide[1]);
etmobile.startAnimation(animHide[2]);
etaddress.startAnimation(animHide[3]);
etlanguage.startAnimation(animHide[4]);
}
When starting the second Activity from you first one, call overridePendingTransition immediately after you call startActivity.
I suggest watching Custom Activity Animations from Google's DevBytes series.
An expert from Google, Chet Haase, explains in details how to deal Activity transition animations. Below the video you will also find link to the source code of the presented example.
If you are interested in more Android animations, checkout the rest of DevBytes series episodes.
I know that are some example over the internet and also here on Stackoverflow I found many examples but belive it or not, none of them supply me needs. Even I have asked a similar question few time ago and I have stuck again into this problem. Basically is the same question but in the opposite direction. I can do whatever animation I want with Activity B but the problem here is Activity A which I could animate just in few scenarios. Basically ActivityA play enter_left only in this combination:
overridePendingTransition(R.anim.enter_from_right, R.anim.exit_on_left);
What I want to do is to animate(move) just the Activity A either on startActivity() and onBackPressed() while Activity B stay unmoved on the screen. Activity A swould allways be drawn on top(as a sliding menu, I could do this with Activity B).
I really thought that the above snippet will do the work:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivityForResult(intent, 500);
overridePendingTransition(R.anim.stay_still, R.anim.exit_on_left);
but this does not even play any animation, while
//this is the animation for onBackPressed()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
overridePendingTransition(R.anim.enter_from_left, 0);
}
animates Activity A as I want but Activity B suddenly disappears from screen and I want to stay (setting (R.anim.enter_from_left, R.anim.stay_still) does nothing).
I have prepared all 5 necessary animations:
enter_from_left
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="500"
android:fromXDelta="-100%"
android:toXDelta="0%" />
</set>
exit_on_left
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="500"
android:fromXDelta="0%"
android:toXDelta="-100%" />
</set>
enter_from_right
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="500"
android:fromXDelta="100%"
android:toXDelta="0%" />
</set>
exit_on_right
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="500"
android:fromXDelta="0%"
android:toXDelta="100%" />
</set>
stay_still
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="500"
android:fromXDelta="0%"
android:toXDelta="0%" />
</set>
I have tried a lot of combinations but non of them worked. Could you please tell me if is this animation possible and if can it be done in this way?
I will post an image, to be more clear what I want to do:
So, first step: on startActivity(), ActivityA should leave the screen from the left side and while moving, the Activity B shoull allready "be there", "below it".
Then, onBackPressed() Acyivity B should "come back", entering from the left side of the screen and overlapping the ActivityB that stay unmoved.
Not sure if you tried this combination, but should work.-
startActivityForResult(intent, 500);
overridePendingTransition(R.anim.enter_from_left, R.anim.stay_still);
I could find that the next one works very nice with fragments fragmentTransaction.setCustomAnimations(R.anim.stay_still,R.anim.exit_on_left);
while overridePendingTransition(R.anim.stay_still, R.anim.exit_on_left); does not work with activity.
On the other hand the reverse,
overridePendingTransition(R.anim.enter_from_left, 0); or
fragmentTransaction.setCustomAnimations(R.anim.enter_from_left, 0);
does not work with activity neither with fragments. The most close approach that works was
transaction.setCustomAnimations(R.anim.stay_still,R.anim.exit_on_left, R.anim.enter_from_left,R.anim.exit_on_right);
If anyone has any other solution, I'm still waiting to hear it and it will be appreciated.
5 years later but there exists a solution for this.
These two functions needs be implemented in the activity which will override the default transition.
#Override
public void startActivity(Intent intent) {
super.startActivity(intent);
overridePendingTransition(R.anim.from_right_in, R.anim.from_left_out);
}
#Override
public void startActivityForResult(Intent intent, int requestCode) {
super.startActivityForResult(intent, requestCode);
overridePendingTransition(R.anim.from_right_in, R.anim.from_left_out);
}
from_right_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:interpolator="#android:interpolator/accelerate_decelerate" android:duration="300"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
</set>
from_left_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="300"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" />
</set>
Try this
Add animations for activity transition in/out, in intent call like
startActivity(new Intent(this,toActivity));
overridePendingTransition( R.anim.slide_in, R.anim.slide_out);
Here is animations file, add this in anim folder
slide_in.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="100%p" android:toYDelta="0%p"
android:duration="300"/>
slide_out.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_longAnimTime"
android:fromYDelta="0%p"
android:toYDelta="100%p" />
Thanks
I don't know if i really understand what you want, but if you want to create a sliding Menu, you could use this tutorial to implement it, it works just fine - It's an alternative to resolve your problem -:
http://developer.android.com/training/implementing-navigation/nav-drawer.html
But your have to use FragmentActivity instead of Activity. If you want an example, just ask for it and i will post it. Good luck.
It should be a pretty simple question that I cannot find through google or the docs for the life of me. How do I change the transition style of my activity?
From what I've heard, there are the grow, left/right and up/down transitions for presenting and dismissing activities, but I don't have a clue how to implement them.
WHen you are doing:
startActivity(intent);
just put:
startActivity(intent);
overridePendingTransition(animIn, animOut);
animIn and animOut are ints that you can just define in anim resources folder for example:
slideInLeft.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100%p" android:toXDelta="0%p"
android:duration="#android:integer/config_longAnimTime" />
slideOutLeft.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="#android:integer/config_longAnimTime" />
And when you want to return to the first activity you need to do the same but in finish method of activity:
finish();
overridePendingTransition(animIn, animOut);
You can use activity.overridePendingTransition. It takes an enter and exit animation resource ids.
I need to start an activity in an animated way..can anyone help me?
Creating an intent and starting an activity in normal way will show new activity.I need to start it from one side,say left side..how to animate it near creating intent..
Use the following:
this.overridePendingTransition(R.anim.slidein_left, R.anim.slideout_right);
Where R.anim.* are Animation XML files in your /res/anim/ folder.
The following is an example of my slidein_left:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromXDelta="-100%"
android:toXDelta="0%" />
</set>
And slideout_right:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromXDelta="0%"
android:toXDelta="100%" />
</set>
What this will do is slide both activities to the left, making the new activity slide in from the left, pushing the old activity out to the right.
Also, as stated by #njzk2, please attempt to make an effort yourself before asking questions, and provide us with things that you may have already tried.
i need to understand animation on Android.
For example, my application starts with an activity with a button in the bottom, when the user click on the button i want that another activity appears with an animation from bottom to top and i want that the button becomes the "header" of this second activity.
How can i achieve this?
Thank You
Daniele
Thank you to DecodeGnome for the answer! It works!
But i have some problem with the animation when i want to close this activity, i create a anim_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%p"
android:fromYDelta="0%p"
android:toXDelta="0"
android:toYDelta="100%p"
android:duration="300" />
</set>
but this doesn't work (the second parameter of overridePendingTransition what is used for?).
I try to call a new overridePendingTransition in onStop() function:
public void onStop(){
super.onStop();
overridePendingTransition(R.anim.top_to_bottom, R.anim.top_to_bottom);
}
But when i call finish to the second activity, i still see the default animation (from left to right)!
Thank you again to who'll help me.
1) Create a folder called anim in res folder
2) Add 2 new XML animations there (example, anim_in.xml & anim_out.xml)
3) put this line of code in the new activities onCreate:
overridePendingTransition(R.anim.anim_in, R.anim.anim_out);
Anim_in.xml example:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%p"
android:fromYDelta="100%p"
android:toXDelta="0"
android:toYDelta="0%p"
android:duration="300" />
</set>
4) Place the button (header) in the top of the layout of the second activity.
Use this code:
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.top_to_bottom, R.anim.top_to_bottom);
}