有没有办法扭转这个动画?

时间:2015-11-22 07:19:11

标签: android android-animation

我有这样的布局:

app design example

我使用以下代码在linearlayouts中为此按钮设置了动画:

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”视图不会在所有设备的屏幕底部显示。

我想要扭转这个动画。我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

您如何为布局,视图或小工具提供动画/过渡

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

答案 1 :(得分:1)

在动画之前,请记住视图的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.
}

答案 2 :(得分:0)

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