在 Android 中调用另一个活动时如何提供动画? [英] How to provide animation when calling another activity in Android?

查看:23
本文介绍了在 Android 中调用另一个活动时如何提供动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动 A 和 B.我想在以下情况下使用收缩动画Activity A 调用 B 并在 Activity B 调用 A 时最大化动画.为此我不需要动画 xml 文件.

I have two Activities A and B. I want to have the shrink Animation when Activity A calls B and maximize animation when Activity B calls A. I don't need the animation xml files for this.

当我们在 Android 中调用另一个 Activity 时,它会给出默认动画,然后调用收缩动画.

When we call another Activity in Android it gives its default animation and then it calls shrink animation.

我想要的是默认动画不应该出现,而我想要的动画应该出现.

What I want is that the default animation should not occur and the animation that I want should occur.

我们真的可以在调用另一个 Activity 时给出动画吗?

Can we actually give the animation when calling another Activity?

推荐答案

自 API 16 起,您可以在调用 Context.startActivity(Intent, Bundle) 或相关方法.它是通过 ActivityOptions 构建器创建的:

Since API 16 you can supply an activity options bundle when calling Context.startActivity(Intent, Bundle) or related methods. It is created via the ActivityOptions builder:

Intent myIntent = new Intent(context, MyActivity.class);
ActivityOptions options = 
   ActivityOptions.makeCustomAnimation(context, R.anim.fade_in, R.anim.fade_out);
context.startActivity(myIntent, options.toBundle());

不要忘记查看 ActivityOptions 构建器的其他方法和 ActivityOptionsCompat 如果您使用的是支持库.

Don't forget to check out the other methods of the ActivityOptions builder and the ActivityOptionsCompat if you are using the Support Library.


API 5+:

对于面向 API 级别 5+ 的应用,有活动 overridePendingTransition 方法.传入和传出动画需要两个资源 ID.0 的 id 将禁用动画.在 startActivity 调用后立即调用它.

For apps targeting API level 5+ there is the Activities overridePendingTransition method. It takes two resource IDs for the incoming and outgoing animations. An id of 0 will disable the animations. Call this immediately after the startActivity call.

即:

startActivity(new Intent(this, MyActivity.class));
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

<小时>

API 3+:

您可以使用 Intent 中的 Intent.FLAG_ACTIVITY_NO_ANIMATION 标志阻止默认动画(从右侧滑入).

You can prevent the default animation (Slide in from the right) with the Intent.FLAG_ACTIVITY_NO_ANIMATION flag in your intent.

即:

Intent myIntent = new Intent(context, MyActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivity(myIntent);

然后在您的 Activity 中,您只需指定自己的动画.

then in your Activity you simply have to specify your own animation.

这也适用于 1.5 API(级别 3).

This also works for the 1.5 API (Level 3).

这篇关于在 Android 中调用另一个活动时如何提供动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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