在上一篇文章中,我们已经了解了compositionlineargradientbrush的基本用法,这篇文章将进一步结合blendeffect介绍一些更为复杂的应用。
Microsoft.Graphics.Canvas.Effects
![[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐](https://img.php.cn/upload/article/001/503/042/175858893370136.jpg)
其中最简单的模式是
Screen
![[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐](https://img.php.cn/upload/article/001/503/042/175858893466468.jpg)
虽然看起来有点复杂,我的理解是它相当于在色轮中拉出一条直线连接Background和Foreground,然后取直线中间点的颜色。例如,红色和蓝色组合会变成紫色,如下图所示:
![[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐](https://img.php.cn/upload/article/001/503/042/175858893414786.jpg)
许多CompositionBrushes可以使用其他CompositionBrushes作为输入。例如,使用SetSourceParameter方法可以将其他CompositionBrush设为CompositionEffectBrush的输入。这是CompositionBrush最有趣的地方之一。以下是如何使用BlendEffect创建CompositionBrush的示例。
首先创建两个CompositionLinearGradientBrush:
var foregroundBrush = compositor.CreateLinearGradientBrush(); foregroundBrush.StartPoint = Vector2.Zero; foregroundBrush.EndPoint = new Vector2(1.0f); var redGradientStop = compositor.CreateColorGradientStop(); redGradientStop.Offset = 0f; redGradientStop.Color = Color.FromArgb(255, 255, 0, 0); var yellowGradientStop = compositor.CreateColorGradientStop(); yellowGradientStop.Offset = 1f; yellowGradientStop.Color = Color.FromArgb(255, 0, 178, 255); foregroundBrush.ColorStops.Add(redGradientStop); foregroundBrush.ColorStops.Add(yellowGradientStop); var backgroundBrush = compositor.CreateLinearGradientBrush(); backgroundBrush.StartPoint = new Vector2(0, 1f); backgroundBrush.EndPoint = new Vector2(1f, 0); var blueGradientStop = compositor.CreateColorGradientStop(); blueGradientStop.Offset = 0f; blueGradientStop.Color = Color.FromArgb(255, 0, 0, 255); var greenGradientStop = compositor.CreateColorGradientStop(); greenGradientStop.Offset = 1f; greenGradientStop.Color = Color.FromArgb(255, 0, 255, 0); backgroundBrush.ColorStops.Add(blueGradientStop); backgroundBrush.ColorStops.Add(greenGradientStop);
它们的效果分别如下面两张图片所示:
![[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐](https://img.php.cn/upload/article/001/503/042/175858894039698.jpg)
![[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐](https://img.php.cn/upload/article/001/503/042/175858894040596.jpg)
接下来创建BlendEffect,并将Foreground和Background设置为CompositionEffectSourceParameter:
var blendEffect = new BlendEffect(){
Mode = BlendEffectMode.Screen,
Foreground = new CompositionEffectSourceParameter("Main"),
Background = new CompositionEffectSourceParameter("Tint"),
};使用BlendEffect创建Brush,并用
SetSourceParameter
var effectFactory = compositor.CreateEffectFactory(blendEffect);
var blendEffectBrush = effectFactory.CreateBrush();
blendEffectBrush.SetSourceParameter("Main", foregroundBrush);
blendEffectBrush.SetSourceParameter("Tint", backgroundBrush);最后,常规使用这个blendEffectBrush的代码如下:
//创建SpriteVisual并设置Brush var spriteVisual = compositor.CreateSpriteVisual(); spriteVisual.Brush = blendEffectBrush; //将自定义 SpriteVisual 设置为元素的可视化树的最后一个子元素。 ElementCompositionPreview.SetElementChildVisual(Gradient, spriteVisual);
最终运行效果如下:
![[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐](https://img.php.cn/upload/article/001/503/042/175858894088388.jpg)
与上一篇文章一样,我也将这篇文章中使用的技术应用到了
一个番茄钟
ColorKeyFrameAnimation
ScalarKeyFrameAnimation
private void StartOffsetAnimation(CompositionColorGradientStop gradientOffset, float offset){
var offsetAnimation = _compositor.CreateScalarKeyFrameAnimation();
offsetAnimation.Duration = TimeSpan.FromSeconds(1);
offsetAnimation.InsertKeyFrame(1.0f, offset);
gradientOffset.StartAnimation(nameof(CompositionColorGradientStop.Offset), offsetAnimation);
}
private void StartColorAnimation(CompositionColorGradientStop gradientOffset, Color color){
var colorAnimation = _compositor.CreateColorKeyFrameAnimation();
colorAnimation.Duration = TimeSpan.FromSeconds(2);
colorAnimation.Direction = Windows.UI.Composition.AnimationDirection.Alternate;
colorAnimation.InsertKeyFrame(1.0f, color);
gradientOffset.StartAnimation(nameof(CompositionColorGradientStop.Color), colorAnimation);
}完整代码在这里,具体运行效果如下:
![[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐](https://img.php.cn/upload/article/001/503/042/175858894115198.jpg)
上述动画可以在我的番茄钟应用中试玩,安装地址:
一个番茄钟
这篇文章的动画和代码参考了JustinLiu的代码,感谢他的分享。
使用XAML画笔难以实现这种多向渐变的效果,这都得益于UWP提供了BlendEffect这一有趣的功能。BlendEffect还有很多其他有趣的模式,大家有空可以多多尝试。
参考资料:
以上就是[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号