Last active
August 29, 2015 14:03
-
-
Save ixiyang/9ebdeeb2560fb98903d4 to your computer and use it in GitHub Desktop.
from circleprogressbutton
This file contains 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
class MorphingAnimation { | |
private static final int ANIMATION_DURATION = 400; | |
private OnAnimationEndListener mListener; | |
private int mFromWidth; | |
private int mToWidth; | |
private int mFromColor; | |
private int mToColor; | |
private int mFromStrokeColor; | |
private int mToStrokeColor; | |
private float mFromCornerRadius; | |
private float mToCornerRadius; | |
private TextView mView; | |
private GradientDrawable mDrawable; | |
public MorphingAnimation(TextView viewGroup, GradientDrawable drawable) { | |
mView = viewGroup; | |
mDrawable = drawable; | |
} | |
public void setListener(OnAnimationEndListener listener) { | |
mListener = listener; | |
} | |
public void setFromWidth(int fromWidth) { | |
mFromWidth = fromWidth; | |
} | |
public void setToWidth(int toWidth) { | |
mToWidth = toWidth; | |
} | |
public void setFromColor(int fromColor) { | |
mFromColor = fromColor; | |
} | |
public void setToColor(int toColor) { | |
mToColor = toColor; | |
} | |
public void setFromStrokeColor(int fromStrokeColor) { | |
mFromStrokeColor = fromStrokeColor; | |
} | |
public void setToStrokeColor(int toStrokeColor) { | |
mToStrokeColor = toStrokeColor; | |
} | |
public void setFromCornerRadius(float fromCornerRadius) { | |
mFromCornerRadius = fromCornerRadius; | |
} | |
public void setToCornerRadius(float toCornerRadius) { | |
mToCornerRadius = toCornerRadius; | |
} | |
public void start() { | |
ValueAnimator widthAnimation = ValueAnimator.ofInt(mFromWidth, mToWidth); | |
widthAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | |
@Override | |
public void onAnimationUpdate(ValueAnimator animation) { | |
Integer value = (Integer) animation.getAnimatedValue(); | |
int leftOffset; | |
int rightOffset; | |
if(mFromWidth > mToWidth) { | |
leftOffset = (mFromWidth - value) / 2; | |
rightOffset = mFromWidth - leftOffset; | |
} else { | |
leftOffset = (mToWidth - value) / 2; | |
rightOffset = mToWidth - leftOffset; | |
} | |
mDrawable.setBounds(leftOffset, 0, rightOffset, mView.getHeight()); | |
} | |
}); | |
ValueAnimator bgColorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), mFromColor, mToColor); | |
bgColorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | |
@Override | |
public void onAnimationUpdate(final ValueAnimator animator) { | |
mDrawable.setColor((Integer) animator.getAnimatedValue()); | |
} | |
}); | |
final int strokeWidth = (int) mView.getContext().getResources().getDimension(R.dimen.stroke_width); | |
ValueAnimator strokeColorAnimation = | |
ValueAnimator.ofObject(new ArgbEvaluator(), mFromStrokeColor, mToStrokeColor); | |
strokeColorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | |
@Override | |
public void onAnimationUpdate(final ValueAnimator animator) { | |
mDrawable.setStroke(strokeWidth, (Integer) animator.getAnimatedValue()); | |
} | |
}); | |
ValueAnimator cornerAnimation = ValueAnimator.ofFloat(mFromCornerRadius, mToCornerRadius); | |
cornerAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | |
@Override | |
public void onAnimationUpdate(ValueAnimator animation) { | |
Float value = (Float) animation.getAnimatedValue(); | |
mDrawable.setCornerRadius(value); | |
} | |
}); | |
AnimatorSet animatorSet = new AnimatorSet(); | |
animatorSet.setDuration(ANIMATION_DURATION); | |
animatorSet.playTogether(widthAnimation, bgColorAnimation, strokeColorAnimation, cornerAnimation); | |
animatorSet.addListener(new Animator.AnimatorListener() { | |
@Override | |
public void onAnimationStart(Animator animation) { | |
} | |
@Override | |
public void onAnimationEnd(Animator animation) { | |
if (mListener != null) { | |
mListener.onAnimationEnd(); | |
} | |
} | |
@Override | |
public void onAnimationCancel(Animator animation) { | |
} | |
@Override | |
public void onAnimationRepeat(Animator animation) { | |
} | |
}); | |
animatorSet.start(); | |
} | |
} | |
//animationset |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment