在 Android 中查看 onScroll 的颜色过渡
View color transition onScroll in Android
我试图在向上滚动时将我的一些视图颜色从 'color1' 过渡到 'color2',反之亦然。
视图在 CoordinateLayout 中,Scroll Listener 实现在自定义 Behaviour.
中
当前实现 UpScroll:
int balanceLabelTextColor = Color.rgb(
(int) (Color.red(color1) * magicFactor),
(int) (Color.green(color1) * magicFactor),
(int) (Color.blue(color1) * magicFactor)
);
balanceLabel.setTextColor(balanceLabelTextColor);
当前实现 DownScroll:
int balanceLabelTextColor = Color.rgb(
(int) (Color.red(color2) * magicFactor),
(int) (Color.green(color2) * magicFactor),
(int) (Color.blue(color2) * magicFactor)
);
balanceLabel.setTextColor(balanceLabelTextColor);
'magicFactor' 根据滚动位置从 (0,1) 取值。
当您仅向上或向下滚动时,此方法工作正常。当您在中途将滚动方向从向上更改为向下时,转换触发器会翻转。
正在寻找一种同时采用 'color1' 和 'color2' 并为平滑颜色过渡生成中间值的实现。
谢谢。
听起来您正在寻找类似 ColorUtils.blendARGB
.
的内容
Blend between two ARGB colors using the given ratio.
A blend ratio of 0.0 will result in color1, 0.5 will give an even
blend, 1.0 will result in color2.
@ColorInt
public static int blendARGB(@ColorInt int color1, @ColorInt int color2,
@FloatRange(from = 0.0, to = 1.0) float ratio) {
final float inverseRatio = 1 - ratio;
float a = Color.alpha(color1) * inverseRatio + Color.alpha(color2) * ratio;
float r = Color.red(color1) * inverseRatio + Color.red(color2) * ratio;
float g = Color.green(color1) * inverseRatio + Color.green(color2) * ratio;
float b = Color.blue(color1) * inverseRatio + Color.blue(color2) * ratio;
return Color.argb((int) a, (int) r, (int) g, (int) b);
}
我试图在向上滚动时将我的一些视图颜色从 'color1' 过渡到 'color2',反之亦然。
视图在 CoordinateLayout 中,Scroll Listener 实现在自定义 Behaviour.
中当前实现 UpScroll:
int balanceLabelTextColor = Color.rgb(
(int) (Color.red(color1) * magicFactor),
(int) (Color.green(color1) * magicFactor),
(int) (Color.blue(color1) * magicFactor)
);
balanceLabel.setTextColor(balanceLabelTextColor);
当前实现 DownScroll:
int balanceLabelTextColor = Color.rgb(
(int) (Color.red(color2) * magicFactor),
(int) (Color.green(color2) * magicFactor),
(int) (Color.blue(color2) * magicFactor)
);
balanceLabel.setTextColor(balanceLabelTextColor);
'magicFactor' 根据滚动位置从 (0,1) 取值。
当您仅向上或向下滚动时,此方法工作正常。当您在中途将滚动方向从向上更改为向下时,转换触发器会翻转。
正在寻找一种同时采用 'color1' 和 'color2' 并为平滑颜色过渡生成中间值的实现。
谢谢。
听起来您正在寻找类似 ColorUtils.blendARGB
.
Blend between two ARGB colors using the given ratio.
A blend ratio of 0.0 will result in color1, 0.5 will give an even blend, 1.0 will result in color2.
@ColorInt
public static int blendARGB(@ColorInt int color1, @ColorInt int color2,
@FloatRange(from = 0.0, to = 1.0) float ratio) {
final float inverseRatio = 1 - ratio;
float a = Color.alpha(color1) * inverseRatio + Color.alpha(color2) * ratio;
float r = Color.red(color1) * inverseRatio + Color.red(color2) * ratio;
float g = Color.green(color1) * inverseRatio + Color.green(color2) * ratio;
float b = Color.blue(color1) * inverseRatio + Color.blue(color2) * ratio;
return Color.argb((int) a, (int) r, (int) g, (int) b);
}