1. 什么是BlendEffect

上一篇文章介绍了CompositionLinearGradientBrush的基本用法, 这篇文章再结合BlendEffec介绍一些更复杂的玩法。

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

其中最简单的是Screen模式,它的计算公式如下

看起来有点复杂, 我的理解是它相当于色轮中Background和Foreground之间拉直线,在直线的中间点的颜色,如下面这张图,红色和蓝色组合成为紫色:

2. 组合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);

它们的效果分别如下面两张图片所示:

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

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

使用BlendEffect创建Brush,并用SetSourceParameter设置它的Foreground和Background。

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);

最终运行效果如下:

3. 创建动画

和上一篇文章一样,我也把这篇文章用到的技术用在了一个番茄钟应用里,,简单地使用ColorKeyFrameAnimationScalarKeyFrameAnimation制作动画:

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);
}

完整代码在这里,具体运行效果如下:

4. 结语

上面的动画可以安装我的番茄钟应用试玩一下,安装地址:

一个番茄钟

这篇文章的动画和代码都参考了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,双倍的快乐相关推荐

  1. 操作系统课程设计X2(双倍的”快乐“)

                                   3种页面置换算法 和3种内存动态分区分配方式 TIPS:键盘按下(CTRL+C,CTRL+V)开启白嫖模式                 ...

  2. python结合json,双倍的快乐

    参考的json [{"age": 18,"isMan": true,"like": ["听歌", "吃饭&qu ...

  3. 六一双倍的快乐:ggplot2绘制双y轴图

    生信交流与合作请关注公众号@生信探索 双y轴图的目的,是想要在同一坐标系中画两组数据,但是他们值范围差很多,比如一组数据是1-10,另一组是10-100,那么可以对第一组数据做数据变化,比如第一组数据 ...

  4. 云之幻哔哩哔哩uwp_[UWP]推荐一款很Fluent Design的bilibili UWP客户端 : 哔哩

    UWP已经有好几个Bilibili的客户端,最近又多了一个: 作者云之幻是一位很擅长设计的UWP开发者,我也从他那里学到了很多设计方面的技巧.它还是一位Bilibili的Up主,主打PowerPoin ...

  5. win10 uwp 让焦点在点击在页面空白处时回到textbox中

    原文:win10 uwp 让焦点在点击在页面空白处时回到textbox中 在网上 有一个大神问我这样的问题:在做UWP的项目,怎么能让焦点在点击在页面空白处时回到textbox中? 虽然我的小伙伴认为 ...

  6. 机器人无限火力无限e符文_LOL:无限火力开黑指南 三大玩法让你快乐加倍

    无限火力上线以来受到大家的热烈追捧,今天给大家介绍几个主流的系列,让你在峡谷能够感受到双倍的快乐! 超强控制流 这一类英雄都是带有强力控制的,无限火力的80%减CD,能够让女坦等英雄可以打出长达几秒的 ...

  7. 图片太大,导致页面加载过慢的处理方法

    整合博客:https://blog.csdn.net/wsyzxss/article/details/73480436  与 http://yujiangshui.com/three-html5-fe ...

  8. Unity3D for iOS初级教程:Part 2/3

    Unity3D for iOS初级教程:Part 2/3 这篇教材是来自教程团队成员 Christine Abernathy, 他是Facebook的开发支持团队的工程师. 欢迎来到Unity3D f ...

  9. 我不是码神!Serverless真的可以为所欲为?

    "你做什么工作的?" "程序员." "那正好,你帮我修一下电脑吧!" 在很多外行人的眼里,程序员就是神一样的存在.他们全年996,节假日无休 ...

最新文章

  1. git如何利用分支进行多人开发
  2. 趣味SQL:用SQL计算瓷砖费用
  3. 2021牛客多校2 - WeChat Walk(分块)
  4. 主键约束 mysql
  5. 《[深入浅出 C#] (第3版)》 - 学习笔记
  6. groovy 访问java,Groovy如何能够访问Java类的私有方法?
  7. TouchVG 支持 CocoaPods 了!
  8. 关关php采集插件,推荐使用:关关采集器(杰奇全版本通用编码版)v3.5.00
  9. iphone个系列尺寸_Iphone各个型号机型的详细参数,尺寸和dpr以及像素
  10. 记一次扩容U盘修复(主控: ChipsBank(芯邦))
  11. 打开我的计算机我的文档不见,我电脑桌面上的我的文档不见了,是什么原因造成我的文档不见了呢?是? 爱问知识人...
  12. 大学生破译周鸿祎手机号 李开复放“橄榄枝”
  13. HP EliteDesk 880 G2 TWR无法从U盘启用
  14. 用LSTM做文本情感分类(以英文为例)附github代码
  15. Dart(5)-内置类型
  16. 想要选好群控系统! 还要先了解的群控发展史
  17. connection_reset -101
  18. Jupyter notebook无法执行代码
  19. MATLAB远程桌面不可启动——解决方法
  20. 土豆视频显示服务器走丢了怎么办,江湖风云录玩家常见问题解答 玩家常遇到的四十个问题_3DM手游...

热门文章

  1. 【Python开发】ComicDownloader(漫画批量下载)
  2. CC00185.CloudKubernetes——|KuberNetes配置管理.V16|——|configmap.secret热更新.v02|configmap编辑方式|
  3. trick3-关于目标检测算法好坏的一些衡量指标
  4. 计算机未来职业人生规划目标与行动措施,计算机专业职业生涯规划方案书.doc...
  5. Electron+React高仿bilibili客户端
  6. 适用于任意模糊内核的深度即插即用超分辨率(DPSR论文笔记-2019CVPR)
  7. Symbian OSE Client-Server Summary
  8. 如何制作魔方机器人-00绪论
  9. Apache 是什么
  10. 临港新片区专精特新中小企业认定政策解读