使用以下代码可以通过动画更改 Activity :

Bundle animation = ActivityOptions.makeCustomAnimation(App.getContext(), R.anim.enter_from_right, R.anim.exit_to_left).toBundle(); 
startActivity(intent, animation); 

对于片段,您可以在 FragmentTransaction 上执行类似的操作:

// ... 
transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left); 
// ... 

这有效!
但是我想在按下时有一个动画(从后台弹出)。
对于片段,您只需添加 2 个动画资源(popEnter 和 popExit):

transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right); 

如何为 Activity 创建相同的“背景动画”?

请您参考如下方法:

我发现了一种不同但简单的方法,它似乎工作得很好。 Activity 的动画也可以使用 overridePendingTransition 执行,因此当 Activity 完成时,您只需使用该方法。

在 BaseActivity 中实现这些覆盖将是最有效的,它由项目中的所有 Activity 扩展。现在,您的所有 Activity 都将在开始新 Activity 时自动包含退出动画和动画:

public abstract class BaseActivity extends AppCompatActivity { 
 
    @Override 
    public void finish() { 
        super.finish(); 
        onLeaveThisActivity(); 
    } 
 
    protected void onLeaveThisActivity() { 
        overridePendingTransition(R.anim.enter_from_left, R.anim.exit_to_right); 
    } 
 
    // It's cleaner to animate the start of new activities the same way. 
    // Override startActivity(), and call *overridePendingTransition* 
    // right after the super, so every single activity transaction will be animated: 
 
    @Override 
    public void startActivity(Intent intent) { 
        super.startActivity(intent); 
        onStartNewActivity(); 
    } 
 
    @Override 
    public void startActivity(Intent intent, Bundle options) { 
        super.startActivity(intent, options); 
        onStartNewActivity(); 
    } 
 
    @Override 
    public void startActivityForResult(Intent intent, int requestCode) { 
        super.startActivityForResult(intent, requestCode); 
        onStartNewActivity(); 
    } 
 
    @Override 
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) { 
        super.startActivityForResult(intent, requestCode, options); 
        onStartNewActivity(); 
    } 
 
    protected void onStartNewActivity() { 
        overridePendingTransition(R.anim.enter_from_right, R.anim.exit_to_left); 
    } 
} 

为了总结起来,我将包括 4 个动画资源:

enter_from_right

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

exit_to_left

<translate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="500" 
    android:fromXDelta="0%p" 
    android:toXDelta="-100%p"/> 

enter_from_left

<translate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="500" 
    android:fromXDelta="-100%p" 
    android:toXDelta="0%p"/> 

exit_to_right

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

附:您可能希望在开始/主要 Activity 中排除退出动画;-)

public class MainMenuActivity extends BaseActivity { 
    .... 
    @Override 
    protected void onLeaveThisActivity() { 
        // Don't use an exit animation when leaving the main activity! 
    } 
} 

2019 年 10 月 24 日编辑:

当从一个 Activity 导航到下一个 Activity 并完成流程中的当前 Activity 时,请注意 finish()应该在导航实现之前调用。如果这样做的顺序错误, onLeaveThisActivity将在 onStartNewActivity 之后调用,导致错误的动画。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!