有什么办法可以反转这个动画吗?

Is there any way to reverse this animation?

我有这样的布局:

我使用以下代码在线性布局中为这些按钮设置动画:

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.
    }

我想反转这个动画。

我试过这种方式,但我在屏幕底部的线性布局,到了顶部。

尝试过这种方式:

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.
    }

当我想尝试这样的事情时:

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" 在所有设备上查看都不会出现在屏幕底部。

而且我想反转这个动画。我该怎么做?

假设视图最初没有 x 或 y 偏移量,反过来你的意思是将视图恢复到其原始位置,调用 yourView.animate().y(0).setDuration(500).start( )

这就是您为布局、视图或小部件提供 animations/transitions 的方式

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);
}

//

}

"anim" 文件夹中创建这些 xml 文件,其中包含您的动画。

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" />

在动画之前,记住视图的 y 位置,然后在反转时重新应用 y 位置:

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.
}