[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐

星夢妙者
发布: 2025-09-23 08:55:30
原创
512人浏览过
  1. 什么是BlendEffect

在上一篇文章中,我们已经了解了compositionlineargradientbrush的基本用法,这篇文章将进一步结合blendeffect介绍一些更为复杂的应用。

Microsoft.Graphics.Canvas.Effects
登录后复制
命名空间下的BlendEffect用于组合两张图片(分别作为输入源的Background和Foreground),它包含多种模式,如下图所示:

[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐

其中最简单的模式是

Screen
登录后复制
模式,它的计算公式如下:

[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐

虽然看起来有点复杂,我的理解是它相当于在色轮中拉出一条直线连接Background和Foreground,然后取直线中间点的颜色。例如,红色和蓝色组合会变成紫色,如下图所示:

[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐

  1. 组合CompositionBrush并使用BlendEffect

许多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,双倍的快乐[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐

接下来创建BlendEffect,并将Foreground和Background设置为CompositionEffectSourceParameter:

var blendEffect = new BlendEffect(){
    Mode = BlendEffectMode.Screen,
    Foreground = new CompositionEffectSourceParameter("Main"),
    Background = new CompositionEffectSourceParameter("Tint"),
};
登录后复制

使用BlendEffect创建Brush,并用

SetSourceParameter
登录后复制
设置它的Foreground和Background:

知我AI
知我AI

一款多端AI知识助理,通过一键生成播客/视频/文档/网页文章摘要、思维导图,提高个人知识获取效率;自动存储知识,通过与知识库聊天,提高知识利用效率。

知我AI 101
查看详情 知我AI
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,双倍的快乐

  1. 创建动画

与上一篇文章一样,我也将这篇文章中使用的技术应用到了

一个番茄钟
登录后复制
应用中。通过
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,双倍的快乐

  1. 结语

上述动画可以在我的番茄钟应用中试玩,安装地址:

一个番茄钟

这篇文章的动画和代码参考了JustinLiu的代码,感谢他的分享。

使用XAML画笔难以实现这种多向渐变的效果,这都得益于UWP提供了BlendEffect这一有趣的功能。BlendEffect还有很多其他有趣的模式,大家有空可以多多尝试。

参考资料:

  • 合成画笔 - Windows UWP applications | Microsoft Docs
  • BlendEffect Class
  • BlendEffectMode Enumeration
  • CompositionEffectBrush.SetSourceParameter(String, CompositionBrush) Method (Windows.UI.Composition) - Windows UWP applications | Microsoft Docs
  • CompositionEffectSourceParameter Class (Windows.UI.Composition) - Windows UWP applications | Microsoft Docs
  • 源码OnePomodoro_GradientsWithBlend.xaml.cs at master

以上就是[UWP]CompositionLinearGradientBrush加BlendEffect,双倍的快乐的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号