这个问题仍然是有效的,几乎在2017年中旬,我想为我的自定义视图颜色变化的平稳过渡。我使用了LinearGradients着色器。
我的目的是让betweeen我的观点的anabled和禁用状态的平稳过渡。
我的情况就更加复杂了,由于观点的建设和目的。这是一个滑块。所以我的视图由几乎2个相同的图层组成,使用画布在第二个图层上绘制一个图层。第三个元素是一个指针。
两个层使用LinearGradients,指针使用单一颜色。
总结所有我在同一时间做5种颜色之间的过渡:
- 开始和底层端颜色 - 酮的LinearGradient,
- 开始和结束颜色顶层 - 第二的LinearGradient ,
- 指针颜色 - 正常颜色;
解决方案是为具有5个独立的ValueAnimators。最重要的是使用ValueAnimator.ofArgb():
private ValueAnimator frontStartColorAnimator;
private ValueAnimator frontEndColorAnimator;
private ValueAnimator bottomStartColorAnimator;
private ValueAnimator bottomEndColorAnimator;
private ValueAnimator pointerColorAnimator;
private AnimatorSet colorsAnimatorSet;
...
frontStartColorAnimator = ValueAnimator.ofArgb(frontLayerStartColor, frontLayerDisabledStartColor);
frontStartColorAnimator.setDuration(animationDuration);
frontStartColorAnimator.setInterpolator(new LinearInterpolator());
frontStartColorAnimator.addUpdateListener(animation
-> shaderFrontLayerStartColor = (int) animation.getAnimatedValue());
frontEndColorAnimator = ValueAnimator.ofArgb(frontLayerEndColor, frontLayerDisabledEndColor);
frontEndColorAnimator.setDuration(animationDuration);
frontEndColorAnimator.setInterpolator(new LinearInterpolator());
frontEndColorAnimator.addUpdateListener(animation -> {
shaderFrontLayerEndColor = (int) animation.getAnimatedValue();
frontLayerPaint.setShader(new LinearGradient(0, 0, getWidth(), 0, shaderFrontLayerStartColor,
shaderFrontLayerEndColor, Shader.TileMode.CLAMP));
});
bottomStartColorAnimator = ValueAnimator.ofArgb(bottomLayerStartColor, bottomLayerDisabledStartColor);
bottomStartColorAnimator.setDuration(animationDuration);
bottomStartColorAnimator.setInterpolator(new LinearInterpolator());
bottomStartColorAnimator.addUpdateListener(animation ->
shaderBottomLayerStartColor = (int) animation.getAnimatedValue());
bottomEndColorAnimator = ValueAnimator.ofArgb(bottomLayerEndColor, bottomLayerDisabledEndColor);
bottomEndColorAnimator.setDuration(animationDuration);
bottomEndColorAnimator.setInterpolator(new LinearInterpolator());
bottomEndColorAnimator.addUpdateListener(animation -> {
shaderBottomLayerEndColor = (int) animation.getAnimatedValue();
backLayerPaint.setShader(new LinearGradient(0, 0, 0, getHeight(), shaderBottomLayerStartColor,
shaderBottomLayerEndColor, Shader.TileMode.CLAMP));
});
pointerColorAnimator = ValueAnimator.ofArgb(pointerEnabledColor, pointerDisabledColor);
pointerColorAnimator.setDuration(animationDuration);
pointerColorAnimator.setInterpolator(new LinearInterpolator());
pointerColorAnimator.addUpdateListener(animation -> {
pointerColor = (int) animation.getAnimatedValue();
pointerPaint.setColor(pointerColor);
});
colorsAnimatorSet = new AnimatorSet();
colorsAnimatorSet.playTogether(frontStartColorAnimator, frontEndColorAnimator,
bottomStartColorAnimator, bottomEndColorAnimator, pointerColorAnimator);
...
colorsAnimatorSet.start();
我也是在开始关注建立在每个动画更新一个新的LinearGradient但后来我推出GPU分析设备上以“在屏幕上吧” 。一切都很好,并在安全范围内运行。
感谢罗曼,你能分享一下你对我下面campnic的评论吗?我想知道什么最有效的.draw ...()方法将绘制创建此效果所需的两个图层,或者如果LayerDrawable正是我所需要的。 – Greg 2011-01-19 01:33:13
@Romain Guy我一直在试图使用SweepGradient来实现圆锥渐变效果。但它似乎只适用于两种颜色。如果我使用四色数组,那么** SweepGradient **似乎不像预期的那样工作。我尝试过**位置**值的各种组合,并尝试设置位置为** NULL **,但大多数我只能获得两种颜色或不是圆锥形的渐变。任何帮助将非常感激。 – 2016-07-05 06:49:01