Created
December 12, 2014 15:31
-
-
Save NikolaDespotoski/c39f080d80d032a4fbe1 to your computer and use it in GitHub Desktop.
KitKat+ custom transition. It animates view coming from left or right depending if it is even or odd child in the ViewGroup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.animation.Animator; | |
import android.animation.ObjectAnimator; | |
import android.annotation.TargetApi; | |
import android.os.Build; | |
import android.transition.TransitionValues; | |
import android.transition.Visibility; | |
import android.view.View; | |
import android.view.ViewGroup; | |
/** | |
* Created by nikola on 12/12/14. | |
*/ | |
@TargetApi(Build.VERSION_CODES.KITKAT) | |
public class BrickTransition extends Visibility { | |
@Override | |
public Animator onDisappear(ViewGroup sceneRoot, View view, TransitionValues startValues, TransitionValues endValues) { | |
return getAnimator(sceneRoot,view,false); | |
} | |
@Override | |
public Animator onAppear(ViewGroup sceneRoot, View view, TransitionValues startValues, TransitionValues endValues) { | |
return getAnimator(sceneRoot,view,false); | |
} | |
private Animator getAnimator(ViewGroup sceneRoot, View view, boolean exit){ | |
int viewIndex = sceneRoot.indexOfChild(view); | |
return viewIndex % 2 !=0? getOddAnimator(view,viewIndex, exit) : | |
getEvenAnimator(view,viewIndex, exit); | |
} | |
private Animator getOddAnimator(View v, int viewIndex, boolean exit){ | |
int startLeft = 0; | |
int endRight = v.getLeft() + v.getWidth(); | |
if(exit){ | |
startLeft = endRight; | |
endRight = 0; | |
} | |
return buildAnimator(v, startLeft, endRight, viewIndex); | |
} | |
private Animator getEvenAnimator(View v, int viewIndex, boolean exit){ | |
int startLeft = v.getRight() - v.getLeft(); | |
int endRight = v.getRight(); | |
if(exit){ | |
int temp = startLeft; | |
startLeft = endRight; | |
endRight = temp; | |
} | |
return buildAnimator(v, startLeft, endRight, viewIndex); | |
} | |
private Animator buildAnimator(View v, int start, int end, int viewIndex) { | |
ObjectAnimator animator = viewIndex % 2 == 0 ? | |
ObjectAnimator.ofInt(v, "right", start, end) : | |
ObjectAnimator.ofInt(v, "left", start,end); | |
return animator; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment