package com.futuremind.recyclerviewfastscroll;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.util.Log;
import android.view.View;

/**
 * Created by Michal on 05/08/16.
 */
public class VisibilityAnimationManager {

    protected final View view;

    protected AnimatorSet hideAnimator;
    protected AnimatorSet showAnimator;

    protected VisibilityAnimationManager(final View view, int showAnimator, int hideAnimator, float pivotXRelative, float pivotYRelative, int hideDelay){
        this.view = view;
        view.setPivotX(pivotXRelative*view.getMeasuredWidth());
        view.setPivotY(pivotYRelative*view.getMeasuredHeight());
        this.hideAnimator = (AnimatorSet) AnimatorInflater.loadAnimator(view.getContext(), hideAnimator);
        this.hideAnimator.setStartDelay(hideDelay);
        this.hideAnimator.setTarget(view);
        this.showAnimator = (AnimatorSet) AnimatorInflater.loadAnimator(view.getContext(), showAnimator);
        this.showAnimator.setTarget(view);
        this.hideAnimator.addListener(new AnimatorListenerAdapter() {

            //because onAnimationEnd() goes off even for canceled animations
            boolean wasCanceled;

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if(!wasCanceled) view.setVisibility(View.INVISIBLE);
                wasCanceled = false;
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
                wasCanceled = true;
            }
        });
    }

    public void show(){
        hideAnimator.cancel();
        if (view.getVisibility() == View.INVISIBLE) {
            view.setVisibility(View.VISIBLE);
            showAnimator.start();
        }
    }

    public void hide(){
        hideAnimator.start();
    }

    public static abstract class AbsBuilder<T extends VisibilityAnimationManager> {
        protected final View view;
        protected int showAnimatorResource = R.animator.fastscroll__default_show;
        protected int hideAnimatorResource = R.animator.fastscroll__default_hide;
        protected int hideDelay = 1000;
        protected float pivotX = 0.5f;
        protected float pivotY = 0.5f;

        public AbsBuilder(View view) {
            this.view = view;
        }

        public AbsBuilder<T> withShowAnimator(int showAnimatorResource){
            this.showAnimatorResource = showAnimatorResource;
            return this;
        }

        public AbsBuilder<T> withHideAnimator(int hideAnimatorResource){
            this.hideAnimatorResource = hideAnimatorResource;
            return this;
        }

        public AbsBuilder<T> withHideDelay(int hideDelay){
            this.hideDelay = hideDelay;
            return this;
        }

        public AbsBuilder<T> withPivotX(float pivotX){
            this.pivotX = pivotX;
            return this;
        }

        public AbsBuilder<T> withPivotY(float pivotY){
            this.pivotY = pivotY;
            return this;
        }

        public abstract T build();
    }

    public static class Builder extends AbsBuilder<VisibilityAnimationManager> {

        public Builder(View view) {
            super(view);
        }

        public VisibilityAnimationManager build(){
            return new VisibilityAnimationManager(view, showAnimatorResource, hideAnimatorResource, pivotX, pivotY, hideDelay);
        }

    }

}

@@ -211,33 +211,8 @@
 et;%0A
-import android.util.Log;%0A
 impo
@@ -233,16 +233,16 @@
 w.View;%0A
+
 %0A/**%0A * 
@@ -432,16 +432,85 @@
 mator;%0A%0A
+    private float pivotXRelative;%0A    private float pivotYRelative;%0A%0A
     prot
@@ -693,127 +693,90 @@
     
-view.setPivotX(pivotXRelative*view.getMeasuredWidth());%0A        view.setPivotY(pivotYRelative*view.getMeasuredHeight())
+this.pivotXRelative = pivotXRelative;%0A        this.pivotYRelative = pivotYRelative
 ;%0A  
@@ -1757,16 +1757,40 @@
      %7D);
+%0A%0A        updatePivot();
 %0A    %7D%0A%0A
@@ -1936,32 +1936,59 @@
 (View.VISIBLE);%0A
+            updatePivot();%0A
             show
@@ -2042,24 +2042,47 @@
 oid hide()%7B%0A
+        updatePivot();%0A
         hide
@@ -2098,32 +2098,203 @@
 start();%0A    %7D%0A%0A
+    protected void updatePivot() %7B%0A        view.setPivotX(pivotXRelative*view.getMeasuredWidth());%0A        view.setPivotY(pivotYRelative*view.getMeasuredHeight());%0A    %7D%0A%0A
     public stati

Update pivot point before animating.