I am writing an code where need to modified view background from one color to other color along alpha animation.
I tried with
ObjectAnimator backgroundColorAnimator = ObjectAnimator.ofObject(stickyLayout,
"backgroundColor",
new ArgbEvaluator(),
0xFFFFFFFF,
0xff78c5f9);
EDIT: Since most of us not clear, Assume we need to change this color with alpha on seekbar updated values above mentioned solution work somewhat but not as expected with alpha...any suggestion?
ValueAnimator colorAnim = ObjectAnimator.ofInt(**myView**, "backgroundColor", Color.RED, Color.BLUE);
colorAnim.setDuration(3000);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
Where myView is the view on which you want to apply Animation
You don't really need these methods: initHeaderColor
& translateHeaderView
.
Define an ArgbEvaluator
as a class member:
ArgbEvaluator mArgbEvaluator;
// define start & end colors
int mStartColor, mEndColor;
// initialize start & end colors
Call ArgbEvaluator's evaluate
method with parameters (slideOffset, startColor, endColor)
, cast the return value to Integer
, and use it to set the background color of fmActionBar
:
void updateActionBarbgColor(float slideOffset) {
if (mArgbEvaluator == null)
mArgbEvaluator = new ArgbEvaluator();
int bgColor = (Integer) mArgbEvaluator.evaluate(slideOffset, mStartColor, mEndColor);
fmActionBar.setBackgroundColor(bgColor);
}
For reference, ArgbEvaluator#evaluate(...)
:
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 (int)((startA + (int)(fraction * (endA - startA))) << 24) |
(int)((startR + (int)(fraction * (endR - startR))) << 16) |
(int)((startG + (int)(fraction * (endG - startG))) << 8) |
(int)((startB + (int)(fraction * (endB - startB))));
}
You can use different evaluator or animate alpha independently with AnimatorSet.
This is working code I found so far:
/**
* Initialise header color while translate animation
*/
private void initHeaderColor(int startColor, int endColor) {
initR = Color.red(startColor);
initG = Color.green(startColor);
initB = Color.blue(startColor);
initAlpha = Color.alpha(startColor);
endR = Color.red(endColor);
endG = Color.green(endColor);
endB = Color.blue(endColor);
endAlpha = Color.alpha(endColor);
}
and set offset of your wish.
/**
* Translate header view component
* @param slideOffset
*/
private void translateHeaderView(float slideOffset) {
finalR = (int) (initR + (endR - initR) * slideOffset);
finalG = (int) (initG + (endG - initG) * slideOffset);
finalB = (int) (initB + (endB - initB) * slideOffset);
finalAlpha = (int) (initAlpha + (endAlpha - initAlpha) * slideOffset);
int color = Color.argb(finalAlpha, finalR, finalG, finalB);
fmActionBar.setBackgroundColor(color);
}
I use an static helper Method to interpolate two colors.
public static int interpolateColor(int colorA, int colorB, float proportion) {
float[] hsva = new float[3];
float[] hsvb = new float[3];
Color.colorToHSV(colorA, hsva);
Color.colorToHSV(colorB, hsvb);
for (int i = 0; i < 3; i++) {
hsvb[i] = interpolate(hsva[i], hsvb[i], proportion);
}
int alpha1 = Color.alpha(colorA);
int alpha2 = Color.alpha(colorB);
int newAlpha = interpolate(alpha1, alpha2, proportion);
return Color.HSVToColor(newAlpha, hsvb); }
Also the interpolate Method:
private static float interpolate(float a, float b, float proportion) {
return (a + ((b - a) * proportion)); }
This method gives the correct color for a proportion value betwenn 0-1 where 0 is colorA is fully visible and 1 means colorB is fully visible.
You then can use a ValueAnimator add an onUpdateListener and get the animationPercentage and pass it as proportion to the interpolate Method.
As you can see i added the alpha values into the method aswell.
Use Color.parseColor
instead of directly 0x
value. Code example in Kotlin though
val objectAnimator = ObjectAnimator.ofObject(my_2_text, "backgroundColor", ArgbEvaluator(),
Color.parseColor("#FFFFFFFF"), Color.parseColor("#FF78c5f9"))
User contributions licensed under CC BY-SA 3.0