Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View to leave from left and enter from right

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

like image 519
coderock Avatar asked Oct 07 '22 01:10

coderock


1 Answers

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);
}
like image 175
Warpzit Avatar answered Oct 10 '22 04:10

Warpzit