Android 将动画侦听器设置为活动动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3386116/
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 09:58:58  来源:igfitidea点击:

Set animation listener to Activity animations

androidanimationandroid-activity

提问by akc

I am using overridePendingTransitionmethod to perform custom Activity animations.

我正在使用overridePendingTransition方法来执行自定义活动动画。

I would like to know when the animation ends ( a callback/listener ).

我想知道动画何时结束(回调/侦听器)。

Is there any direct way of achieving this, if not please suggest me some work around.

有没有直接的方法可以实现这一点,如果没有,请建议我一些解决方法。

采纳答案by Curtain

overridePendingTransitiondoes not have a listener. Like I wrote an earlier post you rather wanna use a normal animation instead for the overridePendingTransition(that is just for Android 2.0 and above).

overridePendingTransition没有听众。就像我之前写的一篇文章,你宁愿使用普通动画来代替overridePendingTransition(仅适用于 Android 2.0 及更高版本)。

You can get a similiar effect and you can also do more cool stuff with an ordinary animation. My earlier post here: Load XML slowly

你可以得到类似的效果,你也可以用普通的动画做更多很酷的东西。我之前的帖子:缓慢加载 XML

回答by Michael Biermann

I use this method to start any animation (resID of the animation XML). If nextPuzzleOnEnd is true, the method "nextPuzzle" is called when the animation has finished.

我使用此方法启动任何动画(动画 XML 的 resID)。如果 nextPuzzleOnEnd 为真,则在动画完成时调用方法“nextPuzzle”。

The method is part of my puzzle-apps and I use it to display any success animation and afterwards (after anim has finished) continue with the next puzzle.

该方法是我的拼图应用程序的一部分,我用它来显示任何成功动画,然后(动画完成后)继续下一个拼图。

 /*
 * start animation (any view)
 */
 private void startAnimation(View v, int resId, Boolean nextPuzzleOnEnd){
    Animation anim;

    if(v!=null){    // can be null, after change of orientation
            anim = AnimationUtils.loadAnimation(this.getContext(),resId);
            anim.setFillAfter(false);
            v.setAnimation(anim);
            if( nextPuzzleOnEnd ){
                anim.setAnimationListener(new AnimationListener() {
                    public void onAnimationStart(Animation anim)
                    {
                    };
                    public void onAnimationRepeat(Animation anim)
                    {
                    };
                    public void onAnimationEnd(Animation anim)
                    {
                        nextPuzzle();
                    };
                });                     
            }
            v.startAnimation(anim);                 
    }
  }

回答by Hamburg is nice

After unsuccessfully browsing Google for this question, I've found a solution by going through all override-methods.

在为这个问题浏览谷歌失败后,我通过所有的覆盖方法找到了一个解决方案。

So what I did, was overriding this method in the activity that is entering the screen:

所以我所做的是在进入屏幕的活动中覆盖这个方法:

Requires API level 21

需要 API 级别 21

@Override
public void onEnterAnimationComplete() {
        super.onEnterAnimationComplete();
}

回答by Nauman Ash

@Hambug solution is nice. But there is problem. onEnterAnimationComplete will work on Lollipop and above API (21).

@Hambug 解决方案很好。但是有问题。onEnterAnimationComplete 将适用于 Lollipop 及以上 API (21)。

@Override
public void onEnterAnimationComplete() {
    super.onEnterAnimationComplete();
    //write code here.
 }

whatever code you write in above method, won't execute on prelolipop devices. So you should put a version check and write it according to you need e.g. in onCreate.

您在上述方法中编写的任何代码都不会在 prelolipop 设备上执行。因此,您应该进行版本检查并根据需要编写它,例如在 onCreate 中。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
        //write code here.
    }