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

Commit f21bd30

Browse files
committed
VerticalStepperItemView: Fix SDK < 21 crashes by unsupported method ObjectAnimator.ofArgb
Signed-off-by: Fung <[email protected]>
1 parent fc494f2 commit f21bd30

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

library/src/main/java/moe/feng/common/stepperview/VerticalStepperItemView.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,17 @@ private void updateMarginBottom() {
175175
*
176176
* @param state The state of this stepper item
177177
*/
178-
@SuppressLint("NewApi")
179178
public synchronized void setState(@State int state) {
180179
// Change point background
181180
if (mPointColorAnimator != null) mPointColorAnimator.cancel();
182181
if (state != STATE_NORMAL && mState == STATE_NORMAL) {
183-
mPointColorAnimator = ObjectAnimator
184-
.ofArgb(mPointBackground, "backgroundColor", mNormalColor, mActivatedColor);
182+
mPointColorAnimator = ViewUtils.createArgbAnimator(
183+
mPointBackground, "backgroundColor", mNormalColor, mActivatedColor);
185184
mPointColorAnimator.setDuration(mAnimationDuration);
186185
mPointColorAnimator.start();
187186
} else if (state == STATE_NORMAL && mState != STATE_NORMAL) {
188-
mPointColorAnimator = ObjectAnimator
189-
.ofArgb(mPointBackground, "backgroundColor", mActivatedColor, mNormalColor);
187+
mPointColorAnimator = ViewUtils.createArgbAnimator(
188+
mPointBackground, "backgroundColor", mActivatedColor, mNormalColor);
190189
mPointColorAnimator.setDuration(mAnimationDuration);
191190
mPointColorAnimator.start();
192191
} else {
@@ -217,15 +216,13 @@ public synchronized void setState(@State int state) {
217216

218217
// Update error state
219218
if (mErrorText != null) {
220-
mTitleColorAnimator = ObjectAnimator
221-
.ofArgb(mTitleText, "textColor",
222-
lastTitleTextColor, mErrorColor);
219+
mTitleColorAnimator = ViewUtils.createArgbAnimator(
220+
mTitleText, "textColor", lastTitleTextColor, mErrorColor);
223221
mTitleColorAnimator.setDuration(mAnimationDuration);
224222
mTitleColorAnimator.start();
225223
if (mSummaryColorAnimator != null) mSummaryColorAnimator.cancel();
226-
mSummaryColorAnimator = ObjectAnimator
227-
.ofArgb(mSummaryText, "textColor",
228-
mSummaryText.getCurrentTextColor(), mErrorColor);
224+
mSummaryColorAnimator = ViewUtils.createArgbAnimator(
225+
mSummaryText, "textColor", mSummaryText.getCurrentTextColor(), mErrorColor);
229226
mSummaryColorAnimator.setDuration(mAnimationDuration);
230227
mSummaryColorAnimator.start();
231228

@@ -242,9 +239,8 @@ public synchronized void setState(@State int state) {
242239
}
243240
} else {
244241
if (mSummaryColorAnimator != null) mSummaryColorAnimator.cancel();
245-
mSummaryColorAnimator = ObjectAnimator
246-
.ofArgb(mSummaryText, "textColor",
247-
mSummaryText.getCurrentTextColor(), mLineColor);
242+
mSummaryColorAnimator = ViewUtils.createArgbAnimator(
243+
mSummaryText, "textColor", mSummaryText.getCurrentTextColor(), mLineColor);
248244
mSummaryColorAnimator.setDuration(mAnimationDuration);
249245
mSummaryColorAnimator.start();
250246

library/src/main/java/moe/feng/common/stepperview/ViewUtils.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package moe.feng.common.stepperview;
22

3+
import android.animation.ObjectAnimator;
4+
import android.animation.TypeEvaluator;
35
import android.content.Context;
46
import android.content.res.TypedArray;
57
import android.graphics.Color;
8+
import android.os.Build;
69
import android.support.annotation.AttrRes;
710
import android.support.annotation.ColorInt;
11+
import android.view.View;
812

913
class ViewUtils {
1014

@@ -23,4 +27,32 @@ static int getColorFromAttr(Context context, @AttrRes int attr) {
2327
return color;
2428
}
2529

30+
static ObjectAnimator createArgbAnimator(View view, String propertyName, int startColor, int endColor) {
31+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
32+
return ObjectAnimator.ofObject(view, propertyName, new TypeEvaluator() {
33+
@Override
34+
public Object evaluate(float fraction, Object startValue, Object endValue) {
35+
int startInt = (Integer) startValue;
36+
int startA = (startInt >> 24) & 0xff;
37+
int startR = (startInt >> 16) & 0xff;
38+
int startG = (startInt >> 8) & 0xff;
39+
int startB = startInt & 0xff;
40+
41+
int endInt = (Integer) endValue;
42+
int endA = (endInt >> 24) & 0xff;
43+
int endR = (endInt >> 16) & 0xff;
44+
int endG = (endInt >> 8) & 0xff;
45+
int endB = endInt & 0xff;
46+
47+
return (startA + (int)(fraction * (endA - startA))) << 24 |
48+
(startR + (int)(fraction * (endR - startR))) << 16 |
49+
(startG + (int)(fraction * (endG - startG))) << 8 |
50+
(startB + (int)(fraction * (endB - startB)));
51+
}
52+
}, startColor, endColor);
53+
} else {
54+
return ObjectAnimator.ofArgb(view, propertyName, startColor, endColor);
55+
}
56+
}
57+
2658
}

0 commit comments

Comments
 (0)