diff --git a/library/src/main/java/moe/feng/common/stepperview/VerticalStepperItemView.java b/library/src/main/java/moe/feng/common/stepperview/VerticalStepperItemView.java index f8a786b..304230d 100644 --- a/library/src/main/java/moe/feng/common/stepperview/VerticalStepperItemView.java +++ b/library/src/main/java/moe/feng/common/stepperview/VerticalStepperItemView.java @@ -175,18 +175,17 @@ private void updateMarginBottom() { * * @param state The state of this stepper item */ - @SuppressLint("NewApi") public synchronized void setState(@State int state) { // Change point background if (mPointColorAnimator != null) mPointColorAnimator.cancel(); if (state != STATE_NORMAL && mState == STATE_NORMAL) { - mPointColorAnimator = ObjectAnimator - .ofArgb(mPointBackground, "backgroundColor", mNormalColor, mActivatedColor); + mPointColorAnimator = ViewUtils.createArgbAnimator( + mPointBackground, "backgroundColor", mNormalColor, mActivatedColor); mPointColorAnimator.setDuration(mAnimationDuration); mPointColorAnimator.start(); } else if (state == STATE_NORMAL && mState != STATE_NORMAL) { - mPointColorAnimator = ObjectAnimator - .ofArgb(mPointBackground, "backgroundColor", mActivatedColor, mNormalColor); + mPointColorAnimator = ViewUtils.createArgbAnimator( + mPointBackground, "backgroundColor", mActivatedColor, mNormalColor); mPointColorAnimator.setDuration(mAnimationDuration); mPointColorAnimator.start(); } else { @@ -217,15 +216,13 @@ public synchronized void setState(@State int state) { // Update error state if (mErrorText != null) { - mTitleColorAnimator = ObjectAnimator - .ofArgb(mTitleText, "textColor", - lastTitleTextColor, mErrorColor); + mTitleColorAnimator = ViewUtils.createArgbAnimator( + mTitleText, "textColor", lastTitleTextColor, mErrorColor); mTitleColorAnimator.setDuration(mAnimationDuration); mTitleColorAnimator.start(); if (mSummaryColorAnimator != null) mSummaryColorAnimator.cancel(); - mSummaryColorAnimator = ObjectAnimator - .ofArgb(mSummaryText, "textColor", - mSummaryText.getCurrentTextColor(), mErrorColor); + mSummaryColorAnimator = ViewUtils.createArgbAnimator( + mSummaryText, "textColor", mSummaryText.getCurrentTextColor(), mErrorColor); mSummaryColorAnimator.setDuration(mAnimationDuration); mSummaryColorAnimator.start(); @@ -242,9 +239,8 @@ public synchronized void setState(@State int state) { } } else { if (mSummaryColorAnimator != null) mSummaryColorAnimator.cancel(); - mSummaryColorAnimator = ObjectAnimator - .ofArgb(mSummaryText, "textColor", - mSummaryText.getCurrentTextColor(), mLineColor); + mSummaryColorAnimator = ViewUtils.createArgbAnimator( + mSummaryText, "textColor", mSummaryText.getCurrentTextColor(), mLineColor); mSummaryColorAnimator.setDuration(mAnimationDuration); mSummaryColorAnimator.start(); diff --git a/library/src/main/java/moe/feng/common/stepperview/ViewUtils.java b/library/src/main/java/moe/feng/common/stepperview/ViewUtils.java index 1bc5f61..9f3c4de 100644 --- a/library/src/main/java/moe/feng/common/stepperview/ViewUtils.java +++ b/library/src/main/java/moe/feng/common/stepperview/ViewUtils.java @@ -1,10 +1,14 @@ package moe.feng.common.stepperview; +import android.animation.ObjectAnimator; +import android.animation.TypeEvaluator; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; +import android.os.Build; import android.support.annotation.AttrRes; import android.support.annotation.ColorInt; +import android.view.View; class ViewUtils { @@ -23,4 +27,32 @@ static int getColorFromAttr(Context context, @AttrRes int attr) { return color; } + static ObjectAnimator createArgbAnimator(View view, String propertyName, int startColor, int endColor) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + return ObjectAnimator.ofObject(view, propertyName, new TypeEvaluator() { + @Override + public Object evaluate(float fraction, Object startValue, Object endValue) { + int startInt = (Integer) startValue; + int startA = (startInt >> 24) & 0xff; + int startR = (startInt >> 16) & 0xff; + int startG = (startInt >> 8) & 0xff; + int startB = startInt & 0xff; + + int endInt = (Integer) endValue; + int endA = (endInt >> 24) & 0xff; + int endR = (endInt >> 16) & 0xff; + int endG = (endInt >> 8) & 0xff; + int endB = endInt & 0xff; + + return (startA + (int)(fraction * (endA - startA))) << 24 | + (startR + (int)(fraction * (endR - startR))) << 16 | + (startG + (int)(fraction * (endG - startG))) << 8 | + (startB + (int)(fraction * (endB - startB))); + } + }, startColor, endColor); + } else { + return ObjectAnimator.ofArgb(view, propertyName, startColor, endColor); + } + } + }