Ataberk
Ataberk

Reputation: 615

Is there any way to reverse this animation?

I have a layout like this:

app design example

And I animated this buttons in linearlayouts with this code:

public void animation(){
 ust.animate().x(0).y(-5000).setDuration(500).start(); //My linear layout in the top of screen.     
alt.animate().x(0).y(4000).setDuration(500).start(); //My linear layout in the bottom of screen.
    }

I want to reverse this animation.

I tried this way but my linear layout in the bottom of screen, went to top.

Tried This Way:

public void animation(){
 ust.animate().x(0).y(0).setDuration(500).start(); //My relative layout in the top of screen.       
alt.animate().x(0).y(0).setDuration(500).start(); //My relative layout in the bottom of screen.
    }

When I want to try anything like this:

public void animation(){
 ust.animate().x(0).y(0).setDuration(500).start(); //My relative layout in the top of screen.       
alt.animate().x(0).y(500).setDuration(500).start(); //My relative layout in the bottom of screen.
    }

"alt" view dont coming to bottom of the screen in all devices.

And I want to reverse this animation. How can I do this?

Upvotes: 0

Views: 860

Answers (3)

Kai
Kai

Reputation: 15476

Before animation, remember the y locations of your views, then reapply the y locations when reversing:

float ustY;
float altY;
public void animation(){
    ustY=ust.getY();
    ust.animate().x(0).y(-5000).setDuration(500).start(); //My linear layout in the top of screen.     
    altY=alt.getY();
    alt.animate().x(0).y(4000).setDuration(500).start(); //My linear layout in the bottom of screen.
}
public void reverseAnimation(){
    ust.animate().x(0).y(ustY).setDuration(500).start(); //My linear layout in the top of screen.     
    alt.animate().x(0).y(altY).setDuration(500).start(); //My linear layout in the bottom of screen.
}

Upvotes: 1

MaggotSauceYumYum
MaggotSauceYumYum

Reputation: 402

that's how you give animations/transitions to layouts, views or widgets

public class MainActivity extends Activity {
Animation RL1, RL2, LR1, LR2, fadein, fadeout;
private View view1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.film1).setVisibility(View.GONE);
    // Define all animations
    new AnimationUtils();

    RL1 = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.slide_right_to_left_1);
    RL2 = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.slide_right_to_left_2);

    LR1 = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.slide_left_to_right_2);
    LR2 = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.slide_left_to_right_1);

    fadein = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.fadeout);
    fadeout = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.fix);
}

// **//

public void show(View view) {
    findViewById(R.id.film1).setVisibility(View.VISIBLE);
    view1 = (View) findViewById(R.id.film1);
    view1.setAnimation(LR1);
    view1.setAnimation(LR2);
}

//

}

create these xml files in the "anim" folder , these contain your animations.

slide_left_to_right_1.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
    android:duration="600"
    android:fromXDelta="-100%"
    android:toXDelta="0%" >
</translate>
</set>

slide_left_to_right_2.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
    android:duration="600"
    android:fromXDelta="0%"
    android:toXDelta="100%" >
</translate>
</set>

slide_right_to_left_1.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
    android:duration="600"
    android:fromXDelta="100%"
    android:toXDelta="0%" >
</translate>
</set> 

slide_right_to_left_2.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
    android:duration="600"
    android:fromXDelta="0%"
    android:toXDelta="-100%" >
</translate>
</set>

fadeout.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />

fix.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0" />

Upvotes: 1

Andrew Orobator
Andrew Orobator

Reputation: 8646

Assuming that the view originally had no x or y offsets and by reverse you mean restore the view to its original position, call yourView.animate().y(0).setDuration(500).start()

Upvotes: 0

Related Questions