5

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

Warpzit
  • 27,966
  • 19
  • 103
  • 155
coderock
  • 143
  • 13

2 Answers2

4

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);
}
Community
  • 1
  • 1
Warpzit
  • 27,966
  • 19
  • 103
  • 155
  • thanks Warpzit for your response, but i want to add the animation to my image view. – coderock Nov 08 '12 at 06:08
  • thats the reason i used two "translate"s together with the second one beginning with startOffset for a delay.. – coderock Nov 08 '12 at 06:10
  • Warpzit,i got ur point , but i have only one activity.. i want my imageview to leave and the same view to return back... – coderock Nov 08 '12 at 06:21
  • Actually.. i hav a unity 3d thing inside my view and so this is the most appropriate animation... i found out that , after completing animation my view will always return back to its previous location.. but still i wonder why the animations dont run as specified in the code... Its very strange that the view works fine when only one translate is used ...LEFT to OUT and RIGHT to IN ..but when both of them are together... the view appears sliding from right moves through the screen and leaves through the left...before coming back to its original position.. – coderock Nov 08 '12 at 09:00
  • 1
    @kirananil Its simply a bug. I actually spent some time trying to make it work but its just not that easy ;) When I used the right parameters it didn't animate at all, it just disappeared and came back when time had run out. But with 1 animation it worked perfect :) – Warpzit Nov 08 '12 at 09:04
0

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

coderock
  • 143
  • 13