Search code examples
javaandroidanimationviewz-order

Change Z order during the animation


I have 2 views with the same animation, during this animation these views should change your Z Orders for example The View A is over the View B during the animation I change this order, I'm getting this with viewA or viewB .bringToFront(); but the but the update is not immediate and I need this Z order changed at an exact point.

Thanks.


Solution

  • I solve this as follows:

    first in animation you set the custom interpolator, your class needs implements the ICustomInterpolator

    animation.setInterpolator(new CustomInterpolator(this));
    a.startAnimation(animation);
    b.startAnimation(animation);
    

    after

    public void currentProgress(float p, float time) {
        if(time == yourTime){
            a.bringToFront();
            a.invalidate();
            b.invalidate();
            a.requestLayout();
            b.requestLayout();
        }
    }
    

    the currentProgress is a Inteface method of a custom interpolator:

    public interface ICustomInterpolator {
        public void currentProgress(float p, float time);
    }
    

    in customInterpolator:

    public class CustomInterpolator extends
        AccelerateDecelerateInterpolator {
    
        private ICustomInterpolator delegate;
    
        public <T extends ICustomInterpolator> CustomInterpolator(T delegate) {
            super();
            this.delegate = delegate;
        }
    
        @Override
        public float getInterpolation(float input) {
            delegate.currentProgress(input);
            return super.getInterpolation(input);
        }
    }