在 Android 中调用另一个活动时如何提供动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2651360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:46:49  来源:igfitidea点击:

How to provide animation when calling another activity in Android?

androidanimationandroid-activity

提问by sunil

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.

我有两个活动 A 和 B。我想在活动 A 调用 B 时使用收缩动画,并在活动 B 调用 A 时最大化动画。为此我不需要动画 xml 文件。

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

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

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

我想要的是默认动画不应该发生,而我想要的动画应该发生。

Can we actually give the animation when calling another Activity?

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

回答by whlk

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

从 API 16 开始,您可以在调用Context.startActivity(Intent, Bundle)或相关方法时提供活动选项包。它是通过ActivityOptions构建器创建的:

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());

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

如果您正在使用支持库,请不要忘记查看 ActivityOptions 构建器和ActivityOptionsCompat的其他方法。





API 5+:

API 5+:

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

对于面向 API 级别 5+ 的应用程序,有活动overridePendingTransition方法。传入和传出动画需要两个资源 ID。一个 id0将禁用动画。通话后立即调用此方法startActivity

i.e.:

IE:

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


API 3+:

API 3+:

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

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

i.e.:

IE:

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

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

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

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

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

回答by Praveen

You must use OverridePendingTransition method to achieve it, which is in the Activity class. Sample Animations in the apidemos example's res/anim folder. Check it. More than check the demo in ApiDemos/App/Activity/animation.

您必须使用 OverridePendingTransition 方法来实现它,该方法在Activity 类中。Apidemos 示例的 res/anim 文件夹中的示例动画。核实。不仅仅是查看ApiDemos/App/Activity/animation 中的演示。

Example:

例子:

@Override
public void onResume(){
    // TODO LC: preliminary support for views transitions
    this.overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
}

回答by Blundell

Wrote a tutorial so that you can animate your activity's in and out,

写了一个教程,这样你就可以动画你的活动进出,

Enjoy:

享受:

http://blog.blundellapps.com/animate-an-activity/

http://blog.blundellapps.com/animate-an-activity/

回答by phreakhead

Jelly Bean adds support for this with the ActivityOptions.makeCustomAnimation()method. Of course, since it's only on Jelly Bean, it's pretty much worthless for practical purposes.

Jelly Bean 使用ActivityOptions.makeCustomAnimation()方法添加了对此的支持。当然,因为它只在 Jelly Bean 上,所以它对于实际用途几乎没有价值。