简体   繁体   English

Android-在活动之间纠正(基于堆栈,来回纠正)动画

[英]Android - correct (stack based, correct back and forth) animations between activities

Consider I have following hierarchy: 考虑我有以下层次结构:

  • level 1 activity (eg Main Menu) 1级活动(例如主菜单)
    • level 2 activity (eg Calendar) 2级活动(例如日历)
      • level 3 activity (eg Calendar Day) 3级活动(例如日历天)

Following use cases exist: 存在以下用例:

  1. Going into the deep 深入
  2. Going back 回去
  3. Bring current activity back to the screen (after screen turned off or from the running background activities after the application was left) 将当前活动返回到屏幕(关闭屏幕后或离开应用程序后从正在运行的后台活动)

Following transition behaviour is desired: 需要以下过渡行为:

Use case 1 + 3: I want the old activity to slide out to the left and the new activity to slide in from the right 用例1 + 3:我希望旧活动向左滑动,新活动从右滑动

use case 2: I want the old activity to slide out to the rightand the new activity to slide in from the left 用例2:我希望旧活动向右滑动,新活动从左滑动

I tried so many ways to achieve that with overridePendingTransition but no combination worked correctly... I got stuck with something like the following: 我尝试了许多方法来使用overridePendingTransition实现此目标,但是没有任何组合可以正常工作...我陷入了以下困境:

public class BaseActivity extends Activity
{
    @Override
    protected void onPause()
    {
        adjustWindowAnimation(this, false);
        super.onPause();
    }

    @Override
    protected void onResume()
    {
        adjustWindowAnimation(this, true);
        super.onResume();
    }

    protected void adjustWindowAnimation(Activity parent, boolean start)
    {
        if (start)
            parent.overridePendingTransition(R.anim.slide_from_right_in, R.anim.slide_to_left_out);
        else
            parent.overridePendingTransition(R.anim.slide_from_left_in, R.anim.slide_to_right_out);
    }

//     @Override
//     protected void onCreate(Bundle savedInstanceState)
//     {
//         adjustWindowAnimation(this, true);
//         super.onCreate(savedInstanceState);
//     }

//     @Override
//     protected void onRestoreInstanceState(Bundle savedInstanceState)
//     {
//         adjustWindowAnimation(this, true);
//         super.onRestoreInstanceState(savedInstanceState);
//     }

//     @Override
//     protected void onStart()
//     {
//         super.onStart();
//         adjustWindowAnimation(this, true);
//     }

//     @Override
//     protected void onNewIntent(Intent intent)
//     {
//         adjustWindowAnimation(this, true);
//         super.onNewIntent(intent);
//     }

//     @Override
//     protected void onStop()
//     {
//         adjustWindowAnimation(this, false);
//         super.onStop();
//     }

//    @Override
//    public void finish()
//    {
//        adjustWindowAnimation(this, false);
//        super.finish();
//    }

//    @Override
//    protected void onDestroy()
//    {
//        Crouton.clearCroutonsForActivity(this);
//        super.onDestroy();
//    }

//     @Override
//     public void onBackPressed()
//     {
//         adjustWindowAnimation(this, false);
//         super.onBackPressed();
//     }

}

Animations (these should work correctly): 动画(这些应该正常工作):

SlideFromLeftIn: SlideFromLeftIn:

<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
    <translate android:fromXDelta="-100%" android:toXDelta="0"
        android:duration="@android:integer/config_shortAnimTime" />
</set>

SlideFromRightIn: SlideFromRightIn:

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

SlideToLeftOut: SlideToLeftOut:

<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
    <translate android:fromXDelta="0%" android:toXDelta="-100%"
        android:duration="@android:integer/config_shortAnimTime" />
</set>

SlideToRightOut: SlideToRightOut:

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

To achieve such behaviour you need to know the reason of pause and resume. 要实现这种行为,您需要知道暂停和继续的原因。 Like if its another activity is going to be presented then app plays one animation in onPause() but if its not (so its any other reason) then you play another animation. 就像如果要显示其另一个活动一样,则应用程序会在onPause()播放一个动画,但是如果不显示(因此有其他原因),则您将播放另一个动画。 Same in onResume() - if it was some activity started and now its finished and that's why onResume() triggered then you play one corresponding animation but if onResume( ) is not triggered by finishing activity (returning to app after a phone call for instance) then you play another animation. 同样在onResume() -如果某个活动开始并且现在已经完成,这就是为什么触发了onResume()然后您播放了一个相应的动画,但是如果onResume( )不是由完成活动触发的(例如在通话后返回到应用),然后播放另一个动画。
I'd done something similair in the following way: 我通过以下方式做了类似的事情:

public class MainMenuActivity extends Activity{
...
    protected boolean isGoingToAlarmActivity;
    protected boolean isGoingToSettingsActivity;
    @Override
    protected void onPause() {
        // for animation choose
        if (isGoingToAlarmActivity){
            isGoingToAlarmActivity = false;
            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
        }
        else if (isGoingToSettingsActivity){
            isGoingToSettingsActivity = false;
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }

        super.onPause();
    }

public void onAlarmButtonClick (View button){
    isGoingToAlarmActivity = true;
    startActivity(new Intent(this, AlarmActivity.class));
}

public void onSettingsButtonClick (View button){
    isGoingToSettingsActivity = true;
    startActivity(new Intent(this, SettingsActivity.class));
}

...
}

There are only 3 activities in the project Menu, Alarm and Settings. 项目菜单,警报和设置中只有3个活动。 So It goes to settings with animation "from right to left" and reversed on returning to Menu and AlrmActivity is vice versa. 因此,它使用“从右到左”动画进行设置,并在返回菜单时反转,AlrmActivity反之亦然。 In AlarmActivity: 在AlarmActivity中:

@Override
protected void onPause() {
    // for animation choose
    if (isFinishing())
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

    super.onPause();
}

and in SettingsActivity: 并在SettingsActivity中:

@Override
protected void onPause() {
    // for animation choose
    if (isFinishing())
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
    super.onPause();
}

note: isFinishing() method of Activity indicates that activity is finishing like when user taps Back button (there are could be other reasons but in most cases it is so). 注意:Activity的isFinishing()方法表明活动正在完成,就像用户点击“后退”按钮时一样(可能还有其他原因,但在大多数情况下是这样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM