Set animation listener to Activity animations

后端 未结 4 435
清歌不尽
清歌不尽 2020-12-16 11:42

I am using overridePendingTransition method to perform custom Activity animations.

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

相关标签:
4条回答
  • 2020-12-16 12:13

    overridePendingTransition does 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).

    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

    0 讨论(0)
  • 2020-12-16 12:26

    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.

    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);                 
        }
      }
    
    0 讨论(0)
  • 2020-12-16 12:26

    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

    @Override
    public void onEnterAnimationComplete() {
            super.onEnterAnimationComplete();
    }
    
    0 讨论(0)
  • 2020-12-16 12:30

    @Hambug solution is nice. But there is problem. onEnterAnimationComplete will work on Lollipop and above 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.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
            //write code here.
        }
    
    0 讨论(0)
提交回复
热议问题