Android 中的 Activity 过渡动画 [英] Activity Transition Animations in Android

查看:44
本文介绍了Android 中的 Activity 过渡动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想应用像 HTC 手机那样的 Activity 转换:当你开始一个新的 Activity 时,有一个从右到左的滑动,但是当你按下后退按钮返回上一个 Activity 时,有从左到右的幻灯片动画.

I want to apply an activities transition like the one that HTC phones have: When you start a new Activity there is a slide right-to-left, but when you press the back button to return to the previous Activity, there is a slide left-to-right animation.

我在我的活动的 onResume 中使用了 overridePendingTransition 来模拟从右向左滑动的动画,但是当我按下后退按钮时,会执行相同的动画是错误的(从结果的角度来看).

I have used overridePendingTransition in the onResume of my Activities to simulate the slide right to left animation, but when I press the back button, the same animation is executed which is wrong (from a result perspective).

因此我想问一下如何管理一个 Activity 的不同动画,一个用于创建时的动画,另一个用于用户按下返回键时的动画.

Thus I would like to ask how to manage having different animations for an Activity, one for when it is created, and one for when the user press back key.

非常感谢!

推荐答案

要定义用户按下后退按钮时的动画,您必须在 Activity<中重写 onBackPressed()/code> 并在其中使用 overridePendingTransition() :

To define an animation when the user presses the back button, you have to override onBackPressed() in your Activity and use overridePendingTransition() in there:

public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
}

所以这个动画只会在按下后退按钮时显示.

So this animation will only be shown when the back button gets pressed.

要在新 Activity 打开时设置动画,您只需在调用 startActivity() 或类似方法后定义动画:

To set an animation when a new Activity opens, you just have to define the animation after you called startActivity() or similar:

startActivity(some_intent);
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);

这篇关于Android 中的 Activity 过渡动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆