Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Can I listen for animation updates using TransitionManager.beginDelayedTransition?

I'm using the TransitionManager.beginDelayedTransition outlined here to animate two views swapping positions within a RelativeLayout. I do this by simply swapping the RelativeLayout.LayoutParams of two the two views.

My question is how do I monitor the animation that is automatically created and executed by TransitionManager without having to create my own custom Transitions. I need to detect when the animation has ended so that I can make a change to the views that have been swapped.

Below is the method that swaps the two views. CollageCanvasAperture is an extension of View and mApertureGroup is the RelativeLayout that holds these views.

private void shuffle(int fromApertureInd, int toApertureInd) {
    final CollageCanvasAperture fromV = (CollageCanvasAperture) mApertureGroup.getChildAt(fromApertureInd);
    final CollageCanvasAperture toV = (CollageCanvasAperture) mApertureGroup.getChildAt(toApertureInd);

    if (null == fromV || null == toV) {
        return;
    }

    TransitionManager.beginDelayedTransition(mApertureGroup);

    RelativeLayout.LayoutParams fromLP = (RelativeLayout.LayoutParams) fromV.getLayoutParams();
    RelativeLayout.LayoutParams toLP = (RelativeLayout.LayoutParams) toV.getLayoutParams();

    fromV.setLayoutParams(toLP);
    toV.setLayoutParams(fromLP);
}

I've done a few hours of searching on here and combing through the TransitionManager code but can't see how to detect changes. I'd prefer to be able to detect the animation end within the CollageCanvasAperture but can't see any relevant listeners to apply.

I guess I could provide the view with the destination LayoutParams before the animation is executed and then the view can listen for size & location changes until they match...?

like image 773
dr_sulli Avatar asked Jun 05 '15 10:06

dr_sulli


People also ask

What is transition animation in Android?

Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.

How do I animate a view in Android?

You can use the view animation system to perform tweened animation on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.


1 Answers

So it turned out it's quite straightforward to add a listener to these 'automatic' transitions.

Instead of using: TransitionManager.beginDelayedTransition(mApertureGroup); you need to work out which automatic Transition is being using when you invoke beginDelayedTransition and call TransitionManager.go() instead.

My view transitions were using the ChangeBounds Transition. Once that was know all I had to do was:

    ChangeBounds mySwapTransition = new ChangeBounds();
    mySwapTransition.addListener(new Transition.TransitionListener() {
        @Override
        public void onTransitionStart(Transition transition) { }

        @Override
        public void onTransitionEnd(Transition transition) { }

        @Override
        public void onTransitionCancel(Transition transition) {}

        @Override
        public void onTransitionPause(Transition transition) { }

        @Override
        public void onTransitionResume(Transition transition) { }
    });

    TransitionManager.go(new Scene(mApertureGroup), mySwapTransition);
like image 190
dr_sulli Avatar answered Sep 21 '22 10:09

dr_sulli