- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在关注this制作彩色动画的问题。
我可以创建动画并且它运行良好,直到动画再次开始。有那么一刻感觉动画在那儿开始/停止了片刻。我希望动画运行流畅,这样它就不会在颜色上显示任何重启效果。有可能吗?
我正在使用以下代码:
<Storyboard x:Key="GradientAnimation"
RepeatBehavior="Forever"
Storyboard.TargetName="BackgroundBrush"
SpeedRatio="0.3">
<ColorAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)"
EnableDependentAnimation="True"
BeginTime="-0:0:0.5">
<LinearColorKeyFrame KeyTime="0:0:0" Value="Black"/>
<LinearColorKeyFrame KeyTime="0:0:1" Value="Red"/>
<LinearColorKeyFrame KeyTime="0:0:2" Value="Black"/>
<LinearColorKeyFrame KeyTime="0:0:3" Value="Red"/>
<LinearColorKeyFrame KeyTime="0:0:4" Value="Black"/>
<LinearColorKeyFrame KeyTime="0:0:5" Value="Red"/>
<LinearColorKeyFrame KeyTime="0:0:6" Value="Black"/>
<LinearColorKeyFrame KeyTime="0:0:7" Value="Red"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)"
EnableDependentAnimation="True">
<LinearColorKeyFrame KeyTime="0:0:0" Value="Red"/>
<LinearColorKeyFrame KeyTime="0:0:1" Value="Black"/>
<LinearColorKeyFrame KeyTime="0:0:2" Value="Red"/>
<LinearColorKeyFrame KeyTime="0:0:3" Value="Black"/>
<LinearColorKeyFrame KeyTime="0:0:4" Value="Red"/>
<LinearColorKeyFrame KeyTime="0:0:5" Value="Black"/>
<LinearColorKeyFrame KeyTime="0:0:6" Value="Red"/>
<LinearColorKeyFrame KeyTime="0:0:7" Value="Black"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
然后我在代码隐藏中开始这个动画:
((Storyboard)Resources["GradientAnimation"]).Begin();
也许有某种“缓动”方法混合了动画的开始和停止,因此它看起来像长时间运行的动画一样流畅。有什么建议吗?
最佳答案
您可以尝试 EasingColorKeyFrame
,但动画永远不会流畅,因为它在 UI 线程上运行(即 EnableDependentAnimation="True"
)。
这里有一些好消息和坏消息。好处是我们现在在 Composition 图层级别拥有新的渐变画笔 API,这意味着它完全可以设置动画并脱离 UI 线程运行。因此,在性能方面,它将比当前的 Storyboard
解决方案好得多。但它仅在 Windows 10 Insider Preview 16225 之后可用,这意味着它不适用于除内部人员之外的大多数当前用户。
但是,我仍将在此处发布这个新解决方案以供将来引用,因为目前还没有关于该主题的示例。
请注意,我为动画添加了一些额外的调味料以使其更有趣。如果您只想为颜色设置动画,请随意移动 EndPoint
和 RotationAngleInDegrees
动画(我使用 EndPoint
动画从实体动画在应用程序启动时将背景颜色设置为渐变,并使用 RotationAngleInDegrees
永久旋转渐变画笔)。
var compositor = Window.Current.Compositor;
// Initially, we set the end point to be (0,0) 'cause we want to animate it at start.
// If you don't want this behavior, simply set it to a different value within (1,1).
_gradientBrush = compositor.CreateLinearGradientBrush();
_gradientBrush.EndPoint = Vector2.Zero;
// Create gradient initial colors.
var gradientStop1 = compositor.CreateColorGradientStop();
gradientStop1.Offset = 0.0f;
gradientStop1.Color = GradientStop1StartColor;
var gradientStop2 = compositor.CreateColorGradientStop();
gradientStop2.Offset = 1.0f;
gradientStop2.Color = GradientStop2StartColor;
_gradientBrush.ColorStops.Add(gradientStop1);
_gradientBrush.ColorStops.Add(gradientStop2);
// Assign the gradient brush to the Root element's Visual.
_backgroundVisual = compositor.CreateSpriteVisual();
_backgroundVisual.Brush = _gradientBrush;
ElementCompositionPreview.SetElementChildVisual(Root, _backgroundVisual);
// There are 3 animations going on here.
// First, we kick off an EndPoint offset animation to create an special entrance scene.
// Once it's finished, we then kick off TWO other animations simultaneously.
// These TWO animations include a set of gradient stop color animations and
// a rotation animation that rotates the gradient brush.
var linearEase = compositor.CreateLinearEasingFunction();
var batch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
batch.Completed += (s, e) =>
{
StartGradientColorAnimations();
StartGradientRotationAnimation();
};
var endPointOffsetAnimation = compositor.CreateVector2KeyFrameAnimation();
endPointOffsetAnimation.Duration = TimeSpan.FromSeconds(3);
endPointOffsetAnimation.InsertKeyFrame(1.0f, Vector2.One);
_gradientBrush.StartAnimation(nameof(_gradientBrush.EndPoint), endPointOffsetAnimation);
batch.End();
void StartGradientColorAnimations()
{
var color1Animation = compositor.CreateColorKeyFrameAnimation();
color1Animation.Duration = TimeSpan.FromSeconds(10);
color1Animation.IterationBehavior = AnimationIterationBehavior.Forever;
color1Animation.Direction = AnimationDirection.Alternate;
color1Animation.InsertKeyFrame(0.0f, GradientStop1StartColor, linearEase);
color1Animation.InsertKeyFrame(0.5f, Color.FromArgb(255, 65, 88, 208), linearEase);
color1Animation.InsertKeyFrame(1.0f, Color.FromArgb(255, 43, 210, 255), linearEase);
gradientStop1.StartAnimation(nameof(gradientStop1.Color), color1Animation);
var color2Animation = compositor.CreateColorKeyFrameAnimation();
color2Animation.Duration = TimeSpan.FromSeconds(10);
color2Animation.IterationBehavior = AnimationIterationBehavior.Forever;
color2Animation.Direction = AnimationDirection.Alternate;
color2Animation.InsertKeyFrame(0.0f, GradientStop2StartColor, linearEase);
color1Animation.InsertKeyFrame(0.5f, Color.FromArgb(255, 200, 80, 192), linearEase);
color2Animation.InsertKeyFrame(1.0f, Color.FromArgb(255, 43, 255, 136), linearEase);
gradientStop2.StartAnimation(nameof(gradientStop2.Color), color2Animation);
}
void StartGradientRotationAnimation()
{
var rotationAnimation = compositor.CreateScalarKeyFrameAnimation();
rotationAnimation.Duration = TimeSpan.FromSeconds(15);
rotationAnimation.IterationBehavior = AnimationIterationBehavior.Forever;
rotationAnimation.InsertKeyFrame(1.0f, 360.0f, linearEase);
_gradientBrush.StartAnimation(nameof(_gradientBrush.RotationAngleInDegrees), rotationAnimation);
}
这是一个有效的 sample下面是它的样子。 :)
关于c# - 轻松上色动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45759192/
通过阅读文档,我确信这是明确声明的,但添加图标仍然是灰色的。 class _TaskState extends State { @override Widget b
通过阅读文档,我确信这是明确声明的,但添加图标仍然是灰色的。 class _TaskState extends State { @override Widget b
这个问题在这里已经有了答案: Get random elements from array in Swift (6 个答案) 关闭 7 年前。
你好,我有一个表,其中包含一些字段,例如 这里我想为表格整行设置颜色..意味着如果 ASR 值是 75 到 100 应该得到一种颜色,50 到 75 应该得到另一种颜色,低于 50 应该得到另一种颜色
如何为 fish 中的 ls 命令着色?我在 Mac 上使用带有适当字体等的 ITerm2。由于某种原因它停止在我的电脑上工作。当我做推荐的时候 function ls command ls
我尝试在 -webkit-transform:rotate(45deg) skew(5deg); 上添加颜色或背景色,但我没有尝试。请帮助我。 我有这个 Sencha Touch 的代码: .x-ch
我使用默认配色方案(稍作改动),没有着色插件。 不幸的是,现在我需要在无法使用 256 色模式的远程机器上工作(使用 Putty)。这看起来很纯净。 我尝试修复它,但我无法理解不同部分的突出显示名称是
是否可以给 scipy.spatial.Voronoi 上色?图表? I know it is. 但现在我的目标是根据色标为每个单元格着色以表示物理量。 如下图(PRL 107, 155704 (20
我正在使用 ggplot2 geom_bar 制作标准条形图。我试图用与最低值的条不同的颜色填充最高值的条。 例如, ggplot(mtcars, aes(x=as.factor(cyl) )) +
我正在尝试对 map 上的特定图 block 进行样式设置,但不知道如何设置。谷歌有一个我在这里指的图 block 的示例: https://developers.google.com/maps/do
我想通过添加一个名为“色调”的 Canvas 来为 Canvas 上的 png 着色,该 Canvas 会将颜色分配给主 Canvas 上的 png。但根本没有绘制任何东西: js fiddel co
我创建了一个模糊 View 并添加到主视图的 subview 中。这是我的代码: let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.ligh
我正在尝试实现与 this function in Matlab 相同的行为,其中每个箭头的颜色对应于它的大小和方向,本质上是从轮子中绘制它的颜色。我看到了this question ,但它似乎只适用
假设有一个fa图标: //这段代码不正确 我的目的是为奖杯本身着色,而不是为奖杯外的背景着色。现在我的奖杯看起来是黑色的,我怎样才能把奖杯的颜色改成黄色? 最佳答案 Font awesome 是
你好我正在做一个 Twitter 应用程序,我想给主题标签、用户名和 URL 上色这是我的手机上的课 class TweetTableViewCell: UITableViewCell { var t
我在下面尝试过,但它在解析颜色时抛出了一个语句错误。提前致谢。 hexColorStr = "0x0976b8" view.setBackgroundColor(Color.parseColor(St
我需要显示一个由圆形和多边形组成的 map ,每个圆形或多边形可能有不同的颜色。 我最初的计划是子类化 MKCircle 和 MKPolygon 并为我的 Region 类添加一个实例变量(其中包含将
我有以下用于生成箭头线的 ng 模板:
我正在使用 Git,使用 Windows 7、PowerShell 和 Posh-Git。我有以下别名设置: ls = log --pretty=tformat:"%C(yellow)%h\\ %C(
我是一名优秀的程序员,十分优秀!