Created
October 24, 2016 07:19
-
-
Save cattaka/f0d3188a7819e62208e3eb74c11a12cd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import android.animation.ArgbEvaluator; | |
import android.databinding.BindingAdapter; | |
import android.support.annotation.ColorInt; | |
import android.support.constraint.ConstraintLayout; | |
import android.view.View; | |
import android.view.ViewGroup; | |
/** | |
* Created by cattaka on 2016/10/24. | |
*/ | |
public class DataBindingFunctions { | |
private static ArgbEvaluator mArgbEvaluator; | |
@BindingAdapter({"setWidth"}) | |
public static void setWidth(View view, int v) { | |
ViewGroup.LayoutParams params = view.getLayoutParams(); | |
params.width = v; | |
view.setLayoutParams(params); | |
} | |
@BindingAdapter({"setHeight"}) | |
public static void setHeight(View view, int v) { | |
ViewGroup.LayoutParams params = view.getLayoutParams(); | |
params.height = v; | |
view.setLayoutParams(params); | |
} | |
@BindingAdapter({"setLayoutConstraintHorizontalWeight"}) | |
public static void setLayoutConstraintHorizontalWeight(View view, float v) { | |
if (v < 0.0001f) { | |
v = 0.0001f; // To avoid it goes match_parent. | |
} | |
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) view.getLayoutParams(); | |
params.horizontalWeight = v; | |
view.setLayoutParams(params); | |
} | |
@BindingAdapter({"setLayoutConstraintVerticalWeight"}) | |
public static void setLayoutConstraintVerticalWeight(View view, float v) { | |
if (v < 0.0001f) { | |
v = 0.0001f; // To avoid it goes match_parent. | |
} | |
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) view.getLayoutParams(); | |
params.verticalWeight = v; | |
view.setLayoutParams(params); | |
} | |
@BindingAdapter({"setLayoutMarginLeft"}) | |
public static void setLayoutMarginLeft(View view, int v) { | |
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams(); | |
params.leftMargin = v; | |
view.setLayoutParams(params); | |
} | |
@BindingAdapter({"setLayoutMarginTop"}) | |
public static void setLayoutMarginTop(View view, int v) { | |
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams(); | |
params.topMargin = v; | |
view.setLayoutParams(params); | |
} | |
@BindingAdapter({"setLayoutMarginRight"}) | |
public static void setLayoutMarginRight(View view, int v) { | |
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams(); | |
params.rightMargin = v; | |
view.setLayoutParams(params); | |
} | |
@BindingAdapter({"setLayoutMarginBottom"}) | |
public static void setLayoutMarginBottom(View view, int v) { | |
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams(); | |
params.bottomMargin = v; | |
view.setLayoutParams(params); | |
} | |
public static int evaluateColor(float fraction, @ColorInt int startValue, @ColorInt int endValue) { | |
if (mArgbEvaluator == null) { | |
mArgbEvaluator = new ArgbEvaluator(); | |
} | |
return (Integer) mArgbEvaluator.evaluate(fraction, startValue, endValue); | |
} | |
public static int evaluateColor(float fraction, @ColorInt int startValue, @ColorInt int centerValue, @ColorInt int endValue) { | |
if (mArgbEvaluator == null) { | |
mArgbEvaluator = new ArgbEvaluator(); | |
} | |
if (fraction <= 0.5) { | |
return (Integer) mArgbEvaluator.evaluate(fraction * 2, startValue, centerValue); | |
} else { | |
return (Integer) mArgbEvaluator.evaluate((fraction - 0.5f) * 2, centerValue, endValue); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment