活动幻灯片动画仅在按下后退按钮时有效

时间:2019-01-01 14:56:56

标签: android animation overridependingtransition

我正在尝试在进入和退出活动时添加滑动动画,但仅在按下电话后退按钮时才起作用。当按下工具栏中的向上按钮或单独的按钮时,它将不起作用。

我尝试将overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);行以及onPause()方法和按钮OnClick方法添加到finish()方法中,但这行不通:/

no_anim.xml:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="0"
        android:fromXDelta="0"
        android:toXDelta="0" />
</set>

slide_out_right.xml:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_longAnimTime"
        android:fromXDelta="0"
        android:toXDelta="100%p" />
</set>

Java:

public void button(View view) {
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}

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

@Override
protected void onPause() {
    super.onPause();
    overridePendingTransition(R.anim.no_anim, R.anim.slide_out_right);
}

2 个答案:

答案 0 :(得分:1)

按下工具栏中的向上按钮时,您可以呼叫onBackPressed()

答案 1 :(得分:0)

设置工具栏导航单击侦听器:

// compiler complains because it expects Array<'one'> | Array<'two'>
const y: X<'one' | 'two'> = ['one', 'two'];

这肯定会起作用!