0

0

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

星夢妙者

星夢妙者

发布时间:2025-09-23 08:55:30

|

524人浏览过

|

来源于php中文网

原创

  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大模型,精准解析招标文件,智能生成投标内容。

下载
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

相关专题

更多
string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

311

2023.08.02

class在c语言中的意思
class在c语言中的意思

在C语言中,"class" 是一个关键字,用于定义一个类。想了解更多class的相关内容,可以阅读本专题下面的文章。

454

2024.01.03

python中class的含义
python中class的含义

本专题整合了python中class的相关内容,阅读专题下面的文章了解更多详细内容。

6

2025.12.06

html5动画制作有哪些制作方法
html5动画制作有哪些制作方法

html5动画制作方法有使用CSS3动画、使用JavaScript动画库、使用HTML5 Canvas等。想了解更多html5动画制作方法相关内容,可以阅读本专题下面的文章。

495

2023.10.23

windows查看端口占用情况
windows查看端口占用情况

Windows端口可以认为是计算机与外界通讯交流的出入口。逻辑意义上的端口一般是指TCP/IP协议中的端口,端口号的范围从0到65535,比如用于浏览网页服务的80端口,用于FTP服务的21端口等等。怎么查看windows端口占用情况呢?php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

489

2023.07.26

查看端口占用情况windows
查看端口占用情况windows

端口占用是指与端口关联的软件占用端口而使得其他应用程序无法使用这些端口,端口占用问题是计算机系统编程领域的一个常见问题,端口占用的根本原因可能是操作系统的一些错误,服务器也可能会出现端口占用问题。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

1027

2023.07.27

windows照片无法显示
windows照片无法显示

当我们尝试打开一张图片时,可能会出现一个错误提示,提示说"Windows照片查看器无法显示此图片,因为计算机上的可用内存不足",本专题为大家提供windows照片无法显示相关的文章,帮助大家解决该问题。

727

2023.08.01

windows查看端口被占用的情况
windows查看端口被占用的情况

windows查看端口被占用的情况的方法:1、使用Windows自带的资源监视器;2、使用命令提示符查看端口信息;3、使用任务管理器查看占用端口的进程。本专题为大家提供windows查看端口被占用的情况的相关的文章、下载、课程内容,供大家免费下载体验。

397

2023.08.02

笔记本电脑卡反应很慢处理方法汇总
笔记本电脑卡反应很慢处理方法汇总

本专题整合了笔记本电脑卡反应慢解决方法,阅读专题下面的文章了解更多详细内容。

1

2025.12.25

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 6万人学习

Excel 教程
Excel 教程

共162课时 | 9.5万人学习

PHP基础入门课程
PHP基础入门课程

共33课时 | 1.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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