Skip to content

Commit

Permalink
fix for issue #45: workaround for a bug in IntProperty before 4.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
andkulikov committed Sep 12, 2016
1 parent 04591b4 commit 5aacdc6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ buildscript {
minSdkVersion = 8
targetSdkVersion = 23

versionCode = 16
versionName = "1.6.7"
versionCode = 17
versionName = "1.6.8"
}
}

Expand Down
16 changes: 9 additions & 7 deletions library/src/main/java/com/transitionseverywhere/Recolor.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Integer get(TextView object) {
return 0;
}

};
}.optimize();
COLORDRAWABLE_COLOR = new IntProperty<ColorDrawable>() {
@Override
public void setValue(ColorDrawable object, int value) {
Expand All @@ -75,7 +75,7 @@ public void setValue(ColorDrawable object, int value) {
public Integer get(ColorDrawable object) {
return object.getColor();
}
};
}.optimize();
} else {
TEXTVIEW_TEXT_COLOR = null;
COLORDRAWABLE_COLOR = null;
Expand Down Expand Up @@ -115,26 +115,28 @@ public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues
final View view = endValues.view;
Drawable startBackground = (Drawable) startValues.values.get(PROPNAME_BACKGROUND);
Drawable endBackground = (Drawable) endValues.values.get(PROPNAME_BACKGROUND);
ObjectAnimator bgAnimator = null;
if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) {
ColorDrawable startColor = (ColorDrawable) startBackground;
ColorDrawable endColor = (ColorDrawable) endBackground;
if (startColor.getColor() != endColor.getColor()) {
final int finalColor = endColor.getColor();
endColor.setColor(startColor.getColor());
return ObjectAnimator.ofObject(endColor, COLORDRAWABLE_COLOR,
new ArgbEvaluator(), startColor.getColor(), finalColor);
bgAnimator = ObjectAnimator.ofInt(endColor, COLORDRAWABLE_COLOR, startColor.getColor(), finalColor);
bgAnimator.setEvaluator(new ArgbEvaluator());
}
}
ObjectAnimator textColorAnimator = null;
if (view instanceof TextView) {
TextView textView = (TextView) view;
int start = (Integer) startValues.values.get(PROPNAME_TEXT_COLOR);
int end = (Integer) endValues.values.get(PROPNAME_TEXT_COLOR);
if (start != end) {
textView.setTextColor(end);
return ObjectAnimator.ofObject(textView, TEXTVIEW_TEXT_COLOR,
new ArgbEvaluator(), start, end);
textColorAnimator = ObjectAnimator.ofInt(textView, TEXTVIEW_TEXT_COLOR, start, end);
textColorAnimator.setEvaluator(new ArgbEvaluator());
}
}
return null;
return TransitionUtils.mergeAnimators(bgAnimator, textColorAnimator);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package com.transitionseverywhere.utils;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.util.Property;

/**
* It's great idea to extend it for any properties that works with int fields
* because ObjectAnimator have optimizations for IntProperty to avoid autoboxing.
*
* Helper class for properties with int values.
* Unfortunately we can't just extend android.util.IntProperty here,
* because of a bug that was fixed only in 4.2.2:
* https://android.googlesource.com/platform/frameworks/base/+/c5d43f76fd7c3ccb91f1b75618a9c9e8f202505b
* <p/>
* To apply internal optimizations to avoid autoboxing use object that will be
* returned by method {@link #optimize()}
* <p/>
* Created by Andrey Kulikov on 17/04/16.
*/
@SuppressLint("NewApi, Override")
public abstract class IntProperty<T> extends android.util.IntProperty<T> {
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public abstract class IntProperty<T> extends Property<T, Integer> {

public IntProperty() {
super(null);
// null instead of name here because it's used only for calling setter
// and getter via reflection. but we have our own overridden set and get.
super(Integer.class, null);
}

public abstract void setValue(T object, int value);

@Override
final public void set(T object, Integer value) {
setValue(object, value);
}

/**
Expand All @@ -25,4 +38,23 @@ public Integer get(T object) {
return null;
}

@SuppressLint("NewApi")
public Property<T, Integer> optimize() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
return new android.util.IntProperty<T>(null) {
@Override
public void setValue(T object, int value) {
IntProperty.this.setValue(object, value);
}

@Override
public Integer get(T object) {
return IntProperty.this.get(object);
}
};
} else {
return this;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void setValue(ProgressBar progressBar, int value) {
public Integer get(ProgressBar progressBar) {
return progressBar.getProgress();
}
};
}.optimize();

/**
* Internal name of property. Like a bundles for intent
Expand Down

0 comments on commit 5aacdc6

Please sign in to comment.