如何使用overridePendingTransition()动画覆盖onBackPressed()

时间:2015-10-04 23:45:07

标签: android

当按下后退按钮时,

PlayPanel活动应以滑动面板的形式关闭到MainActivity。但是,没有动画。 PlayPanel活动只是正常关闭。

PlayPanel activity

public class PlayPanel extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_panel);
    }

    @Override
    public void onBackPressed() {

        Intent intent = new Intent(
             PlayPanel.this,
             MainActivity.class
             );

        startActivity(intent);

        overridePendingTransition(
             0, 
             R.anim.play_panel_close_background
             );
    }

    // onCreateOptionsMenu goes here

    // onOptionsItemSelected goes here
}

play_panel_close_background.xml

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

the solution:

 @Override
    public void onBackPressed() {
        super.onBackPressed();
        //startActivity(new Intent(this, MainActivity.class));
        overridePendingTransition(
                0,
                R.anim.play_panel_close_outgoing_activity
        );
    }

5 个答案:

答案 0 :(得分:11)

之前我实现了相同的动画。 这段代码有效。请试一试。感谢。

DetailActivity

public class DetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        overridePendingTransition(R.anim.activity_slide_start_enter, R.anim.activity_scale_start_exit);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.activity_scale_finish_enter, R.anim.activity_slide_finish_exit);
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}

activity_slide_start_enter.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="false">
    <translate
        android:duration="200"
        android:fromXDelta="100%"
        android:fromYDelta="0%"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="0%"
        android:toYDelta="0%" />
</set>

activity_scale_start_exit.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="false">

    <scale
        android:duration="200"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.9"
        android:toYScale="0.9" />

</set>

activity_scale_finish_enter.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="false">

    <scale
        android:duration="200"
        android:fromXScale="0.9"
        android:fromYScale="0.9"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>

activity_slide_finish_exit.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="false">
    <translate
        android:duration="200"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="100%"
        android:toYDelta="0%" />
</set>

答案 1 :(得分:5)

根据documentationoverridePendingTransition的第一个参数是用于传入活动的动画,即。你的MainActivity。第二个参数用于传出活动,即。你的PlayPanel

<强> play_panel_close_background.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:zAdjustment="top">
    <translate
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="600"
        />
</set>
@Override
public void onBackPressed() {
    //super.onBackPressed();
    startActivity(new Intent(this, MainActivity.class));
    overridePendingTransition(
            0,
            R.anim.play_panel_close_background
    );
}

答案 2 :(得分:2)

  

此解决方案适合我

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}

答案 3 :(得分:1)

我的问题是我不能使用“on pending pressed”来覆盖“覆盖暂停转换”

setXMLNamespace

答案 4 :(得分:0)

我尝试过的最佳解决方案

@Override
public void onBackPressed() {
    finish();
    overridePendingTransition(R.anim.anim_right_to_left_enter, R.anim.anim_right_to_left_exit);
}