下面列出了怎么用android.animation.TimeInterpolator的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void preparePartialTransition_setsPartialInterpolator() {
//given
final StaggeredAnimationGroup spiedGroup = prepareSpiedGroup();
final TimeInterpolator testInterpolator = new LinearOutSlowInInterpolator();
final Transition spiedTransition = spy(new AutoTransition());
final StaggeredAnimationGroup.PartialTransitionFactory factory =
new StaggeredAnimationGroup.PartialTransitionFactory() {
@Override
public Transition createPartialTransition(boolean show, int viewId, int indexInTransition) {
return spiedTransition;
}
};
spiedGroup.setPartialTransitionFactory(factory);
spiedGroup.setPartialInterpolator(testInterpolator);
//when
spiedGroup.preparePartialTransition(true, 0, 0);
//then
verify(spiedTransition, times(1)).setInterpolator(testInterpolator);
}
/**
* Moves to bottom(VERTICAL) or right(HORIZONTAL) of child view
* Sets 0 to duration if you want to move immediately.
*
* @param index index child view index
* @param duration
* @param interpolator use the default interpolator if the argument is null.
*/
public void moveChild(int index, long duration, @Nullable TimeInterpolator interpolator) {
if (isAnimating) return;
final int destination = getChildPosition(index) +
(isVertical() ? getPaddingBottom() : getPaddingRight());
if (duration <= 0) {
isExpanded = destination > closePosition;
setLayoutSize(destination);
requestLayout();
notifyListeners();
return;
}
createExpandAnimator(getCurrentPosition(), destination,
duration, interpolator == null ? this.interpolator : interpolator).start();
}
private TimeInterpolator getTimeInterpolater() {
switch (mAnimationStyle) {
case ViewBaseCommon.ANIMATION_LINEAR:
return new LinearInterpolator();
case ViewBaseCommon.ANIMATION_DECELERATE:
return new DecelerateInterpolator();
case ViewBaseCommon.ANIMATION_ACCELERATE:
return new AccelerateInterpolator();
case ViewBaseCommon.ANIMATION_ACCELERATEDECELERATE:
return new AccelerateDecelerateInterpolator();
case ViewBaseCommon.ANIMATION_SPRING:
return new SpringInterpolator();
default:
return new LinearInterpolator();
}
}
@Override
public void prepareAnimators(DepthRelativeLayout target, int index, int animationDelay) {
final TimeInterpolator interpolator = new ExpoIn();
final float finalTranslationY = exitConfiguration.getFinalYPercent() * target.getResources().getDisplayMetrics().heightPixels;
final float finalTranslationX = exitConfiguration.getFinalXPercent() * target.getResources().getDisplayMetrics().widthPixels;
final long totalDuration = exitConfiguration.getDuration();
final ObjectAnimator translationY2 = ObjectAnimator.ofFloat(target, View.TRANSLATION_Y, finalTranslationY);
translationY2.setDuration(totalDuration);
//translationY2.setInterpolator(new AccelerateInterpolator());
translationY2.setInterpolator(interpolator);
translationY2.setStartDelay(animationDelay);
attachListener(translationY2);
add(translationY2);
final ObjectAnimator translationX2 = ObjectAnimator.ofFloat(target, View.TRANSLATION_X, finalTranslationX);
translationX2.setDuration(totalDuration);
translationX2.setInterpolator(interpolator);
translationX2.setStartDelay(animationDelay);
add(translationX2);
}
public void animateIndeterminate(int durationOneCircle,
TimeInterpolator interpolator) {
animator = ObjectAnimator.ofFloat(this, "startAngle", getStartAngle(), getStartAngle() + 360);
if (interpolator != null) animator.setInterpolator(interpolator);
animator.setDuration(durationOneCircle);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.start();
}
public static MoveData startAnimation(Context context, final View toView, Bundle transitionBundle, Bundle savedInstanceState, final int duration, final TimeInterpolator interpolator) {
final TransitionData transitionData = new TransitionData(context, transitionBundle);
if (transitionData.imageFilePath != null) {
setImageToView(toView, transitionData.imageFilePath);
}
final MoveData moveData = new MoveData();
moveData.toView = toView;
moveData.duration = duration;
if (savedInstanceState == null) {
ViewTreeObserver observer = toView.getViewTreeObserver();
observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
toView.getViewTreeObserver().removeOnPreDrawListener(this);
int[] screenLocation = new int[2];
toView.getLocationOnScreen(screenLocation);
moveData.leftDelta = transitionData.thumbnailLeft - screenLocation[0];
moveData.topDelta = transitionData.thumbnailTop - screenLocation[1];
moveData.widthScale = (float) transitionData.thumbnailWidth / toView.getWidth();
moveData.heightScale = (float) transitionData.thumbnailHeight / toView.getHeight();
runEnterAnimation(moveData, interpolator);
return true;
}
});
}
return moveData;
}
private static void runEnterAnimation(MoveData moveData, TimeInterpolator interpolator) {
final View toView = moveData.toView;
toView.setPivotX(0);
toView.setPivotY(0);
toView.setScaleX(moveData.widthScale);
toView.setScaleY(moveData.heightScale);
toView.setTranslationX(moveData.leftDelta);
toView.setTranslationY(moveData.topDelta);
toView.animate().setDuration(moveData.duration).
scaleX(1).scaleY(1).
translationX(0).translationY(0).
setInterpolator(interpolator);
}
private void doStartAnimation(ViewPropertyAnimator parent) {
int size = parent.mPendingAnimations.size();
long startDelay = parent.getStartDelay();
long duration = parent.getDuration();
TimeInterpolator interpolator = parent.getInterpolator();
if (interpolator == null) {
// Documented to be LinearInterpolator in ValueAnimator.setInterpolator
interpolator = sLinearInterpolator;
}
if (!RenderNodeAnimator.isNativeInterpolator(interpolator)) {
interpolator = new FallbackLUTInterpolator(interpolator, duration);
}
for (int i = 0; i < size; i++) {
NameValuesHolder holder = parent.mPendingAnimations.get(i);
int property = RenderNodeAnimator.mapViewPropertyToRenderProperty(holder.mNameConstant);
final float finalValue = holder.mFromValue + holder.mDeltaValue;
RenderNodeAnimator animator = new RenderNodeAnimator(property, finalValue);
animator.setStartDelay(startDelay);
animator.setDuration(duration);
animator.setInterpolator(interpolator);
animator.setTarget(mView);
animator.start();
mAnimators[property] = animator;
}
parent.mPendingAnimations.clear();
}
/**
* Creates an animator that can be used for x and/or y translations. When interrupted,
* it sets a tag to keep track of the position so that it may be continued from position.
*
* @param view The view being moved. This may be in the overlay for onDisappear.
* @param values The values containing the view in the view hierarchy.
* @param viewPosX The x screen coordinate of view
* @param viewPosY The y screen coordinate of view
* @param startX The start translation x of view
* @param startY The start translation y of view
* @param endX The end translation x of view
* @param endY The end translation y of view
* @param interpolator The interpolator to use with this animator.
* @return An animator that moves from (startX, startY) to (endX, endY) unless there was
* a previous interruption, in which case it moves from the current position to (endX, endY).
*/
static Animator createAnimation(View view, TransitionValues values, int viewPosX, int viewPosY,
float startX, float startY, float endX, float endY, TimeInterpolator interpolator,
Transition transition) {
float terminalX = view.getTranslationX();
float terminalY = view.getTranslationY();
int[] startPosition = (int[]) values.view.getTag(R.id.transitionPosition);
if (startPosition != null) {
startX = startPosition[0] - viewPosX + terminalX;
startY = startPosition[1] - viewPosY + terminalY;
}
// Initial position is at translation startX, startY, so position is offset by that amount
int startPosX = viewPosX + Math.round(startX - terminalX);
int startPosY = viewPosY + Math.round(startY - terminalY);
view.setTranslationX(startX);
view.setTranslationY(startY);
if (startX == endX && startY == endY) {
return null;
}
Path path = new Path();
path.moveTo(startX, startY);
path.lineTo(endX, endY);
ObjectAnimator anim = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, View.TRANSLATION_Y,
path);
TransitionPositionListener listener = new TransitionPositionListener(view, values.view,
startPosX, startPosY, terminalX, terminalY);
transition.addListener(listener);
anim.addListener(listener);
anim.addPauseListener(listener);
anim.setInterpolator(interpolator);
return anim;
}
private void expandButtons() {
int numberOfButtons = buttons.size();
Animator[] animations = new Animator[numberOfButtons];
for (int i = 0; i < numberOfButtons; i++) {
View button = buttons.get(i);
TimeInterpolator interpolator = getExpandAnimatorInterpolation();
float toY = calculateExpandedYPosition(i);
animations[i] = createAnimatorForButton(interpolator, button, toY);
}
playAnimatorsTogether(animations);
}
@Override
public TimeInterpolator getInterpolator() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
return mAnimator.getInterpolator();
}
if (mAnimator instanceof ObjectAnimator) {
return ((ObjectAnimator) mAnimator).getInterpolator();
}
return null;
}
public void setInterpolator(TimeInterpolator interpolator) {
if (interpolator == null) {
mInterpolator = SCROLL;
} else {
mInterpolator = interpolator;
}
}
/**
* {@inheritDoc}
*/
@Override
public void collapse(final long duration, final @Nullable TimeInterpolator interpolator) {
if (isAnimating) return;
if (duration <= 0) {
move(closePosition, duration, interpolator);
return;
}
createExpandAnimator(getCurrentPosition(), closePosition, duration, interpolator).start();
}
public EyebrowsScaleAnimator(int minDuration, int maxDuration, TimeInterpolator interpolator, float startSize, float endSize) {
this.minDuration = minDuration;
this.maxDuration = maxDuration;
this.mInterpolator = interpolator;
this.startSize = startSize;
this.endSize = endSize;
}
/**
* Utility method to handle creating and running the Animator.
*/
private Animator createAnimation(View view, float[] startTranslation, float[] endTranslation,
Animator.AnimatorListener listener, TimeInterpolator interpolator) {
if (startTranslation == null || endTranslation == null ||
startTranslation.length != endTranslation.length || equals(startTranslation, endTranslation)) {
// run listener if we're noop'ing the animation, to get the end-state results now
if (listener != null) {
listener.onAnimationEnd(null);
}
return null;
}
final AnimatorSet anim = new AnimatorSet();
ObjectAnimator animX = null, animY = null;
if ((mTranslationDirection & X) != 0) {
animX = ObjectAnimator.ofFloat(view, View.X,
startTranslation[0], endTranslation[0]);
}
if ((mTranslationDirection & Y) != 0) {
animY = ObjectAnimator.ofFloat(view, View.Y,
startTranslation[1], endTranslation[1]);
}
if (null != animX && null == animY) anim.play(animX);
if (null == animX && null != animY) anim.play(animY);
if (null != animX && null != animY) anim.playTogether(animX, animY);
if (null == animX && null == animY) return null;
if (listener != null) {
anim.addListener(listener);
}
anim.setInterpolator(interpolator);
return anim;
}
private void runAnimation(final ValueAnimator animator, final float startValue,
final float targetValue, final long duration_ms,
final TimeInterpolator interpolator)
{
if (startValue == targetValue)
{
return;
}
animator.setInterpolator(interpolator);
animator.setDuration(duration_ms);
animator.setFloatValues(startValue, targetValue);
animator.start();
}
private static void addAnimation(Collection<Animator> animators, View view, TimeInterpolator interpolator, int duration, int delay) {
Animator scaleX = ObjectAnimator.ofFloat(view, View.SCALE_X, 0f, 1f);
scaleX.setInterpolator(interpolator);
scaleX.setDuration(duration);
scaleX.setStartDelay(delay);
animators.add(scaleX);
Animator scaleY = ObjectAnimator.ofFloat(view, View.SCALE_Y, 0f, 1f);
scaleY.setInterpolator(interpolator);
scaleY.setDuration(duration);
scaleY.setStartDelay(delay);
animators.add(scaleY);
}
/**
* {@inheritDoc}
*/
@Override
public void collapse(final long duration, final @Nullable TimeInterpolator interpolator) {
if (isAnimating) return;
if (duration <= 0) {
move(closePosition, duration, interpolator);
return;
}
createExpandAnimator(getCurrentPosition(), closePosition, duration, interpolator).start();
}
private static void runEnterAnimation(MoveData moveData, TimeInterpolator interpolator, Animator.AnimatorListener listener) {
final View toView = moveData.toView;
toView.setPivotX(0);
toView.setPivotY(0);
toView.setScaleX(moveData.widthScale);
toView.setScaleY(moveData.heightScale);
toView.setTranslationX(moveData.leftDelta);
toView.setTranslationY(moveData.topDelta);
toView.animate().setDuration(moveData.duration).scaleX(1).scaleY(1).translationX(0).translationY(0).setListener(listener).setInterpolator(interpolator);
}
protected void snapToPage(int whichPage, int duration, boolean immediate,
TimeInterpolator interpolator) {
whichPage = validateNewPage(whichPage);
int newX = getScrollForPage(whichPage);
final int delta = newX - getUnboundedScrollX();
snapToPage(whichPage, delta, duration, immediate, interpolator);
}
public KeyframeAssert hasInterpolator(TimeInterpolator interpolator) {
isNotNull();
TimeInterpolator actualInterpolator = actual.getInterpolator();
assertThat(actualInterpolator) //
.overridingErrorMessage("Expected interpolator <%s> but was <%s>.", interpolator,
actualInterpolator) //
.isSameAs(interpolator);
return this;
}
private static void addAnimation(Collection<Animator> animators, View view, TimeInterpolator interpolator, int duration, int delay) {
Animator scaleX = ObjectAnimator.ofFloat(view, View.SCALE_X, 0f, 1f);
scaleX.setInterpolator(interpolator);
scaleX.setDuration(duration);
scaleX.setStartDelay(delay);
animators.add(scaleX);
Animator scaleY = ObjectAnimator.ofFloat(view, View.SCALE_Y, 0f, 1f);
scaleY.setInterpolator(interpolator);
scaleY.setDuration(duration);
scaleY.setStartDelay(delay);
animators.add(scaleY);
}
private static float lerp(
float startValue, float endValue, float fraction, @Nullable TimeInterpolator interpolator) {
if (interpolator != null) {
fraction = interpolator.getInterpolation(fraction);
}
return AnimationUtils.lerp(startValue, endValue, fraction);
}
@Override public void setInterpolator(@NonNull TimeInterpolator interpolator) {
iconAnimator.setInterpolator(interpolator);
}
public void setPositionInterpolator(TimeInterpolator interpolator) {
positionInterpolator = interpolator;
recalculate();
}
@Override
public void setInterpolator(TimeInterpolator timeInterpolator) {
mAnimator.setInterpolator(timeInterpolator);
}
/**
* @param interpolator
* The interpolator of the entire animation to set.
*/
public ExplodeAnimation setInterpolator(TimeInterpolator interpolator) {
this.interpolator = interpolator;
return this;
}
/**
* @return The interpolator of the entire animation.
*/
public TimeInterpolator getInterpolator() {
return interpolator;
}
static boolean isNativeInterpolator(TimeInterpolator interpolator) {
return interpolator.getClass().isAnnotationPresent(HasNativeInterpolator.class);
}
/**
* Sets a {@link TimeInterpolator} for expanding.
* @param expandInterpolator the interpolator
*/
public void setExpandInterpolator(final TimeInterpolator expandInterpolator)
{
this.expandInterpolator = expandInterpolator;
}