Skip to content
This repository has been archived by the owner on Aug 22, 2020. It is now read-only.

Commit

Permalink
VerticalStepperItemView: Fix SDK < 21 crashes by unsupported method O…
Browse files Browse the repository at this point in the history
…bjectAnimator.ofArgb

Signed-off-by: Fung <[email protected]>
  • Loading branch information
fython committed Aug 17, 2017
1 parent fc494f2 commit f21bd30
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down
32 changes: 32 additions & 0 deletions library/src/main/java/moe/feng/common/stepperview/ViewUtils.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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);
}
}

}

0 comments on commit f21bd30

Please sign in to comment.