我正在编写一个代码,其中需要修改查看背景从一种颜色到另一种颜色的阿尔法动画。
我试过
ObjectAnimator backgroundColorAnimator = ObjectAnimator.ofObject(stickyLayout,
"backgroundColor",
new ArgbEvaluator(),
0xFFFFFFFF,
0xff78c5f9);编辑:,因为我们中的大多数人都不清楚,假设我们需要用上面提到的搜索栏上的alpha更新值来改变这个颜色,但是不像alpha...any建议那样工作吗?
发布于 2015-03-20 11:32:50
到目前为止,这是我找到的工作代码:
/**
* 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);
}并设定抵消你的愿望。
/**
* 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);
}发布于 2015-03-25 07:57:16
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();其中myView是要应用动画的视图
发布于 2015-03-20 21:04:13
您并不真正需要这些方法:initHeaderColor & translateHeaderView。
将ArgbEvaluator定义为类成员:
ArgbEvaluator mArgbEvaluator;
// define start & end colors
int mStartColor, mEndColor;
// initialize start & end colors使用参数evaluate调用ArgbEvaluator的(slideOffset, startColor, endColor)方法,将返回值强制转换为Integer,并使用它设置fmActionBar的背景色
void updateActionBarbgColor(float slideOffset) {
if (mArgbEvaluator == null)
mArgbEvaluator = new ArgbEvaluator();
int bgColor = (Integer) mArgbEvaluator.evaluate(slideOffset, mStartColor, mEndColor);
fmActionBar.setBackgroundColor(bgColor);
}供参考,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))));
}https://stackoverflow.com/questions/27780973
复制相似问题