CompositionMaskBrush 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用 CompositionBrush 绘制 SpriteVisual,并应用了不透明度掩码。 不透明度掩码的源可以是 CompositionColorBrush、CompositionLinearGradientBrush、CompositionSurfaceBrush、CompositionEffectBrush 或 CompositionNineGridBrush 类型的任意 CompositionBrush。 不透明度蒙板必须指定为 CompositionSurfaceBrush。
public ref class CompositionMaskBrush sealed : CompositionBrush
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 196608)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class CompositionMaskBrush final : CompositionBrush
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 196608)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class CompositionMaskBrush : CompositionBrush
Public NotInheritable Class CompositionMaskBrush
Inherits CompositionBrush
- 继承
- 属性
Windows 要求
设备系列 |
Windows 10 Anniversary Edition (在 10.0.14393.0 中引入)
|
API contract |
Windows.Foundation.UniversalApiContract (在 v3.0 中引入)
|
示例
将star形状的蒙板应用于图像。
private SpriteVisual CreateCircleImage(ICompositionSurface myImageSurface, ICompositionSurface myStarMaskSurface)
{
// Create SurfaceBrush with image surface and specify stretch and alignment
CompositionSurfaceBrush imageBrush = _compositor.CreateSurfaceBrush(myImageSurface);
imageBrush.HorizontalAlignmentRatio = 0.5f;
imageBrush.VerticalAlignmentRatio = 0.5f;
imageBrush.Stretch = CompositionStretch.UniformToFill;
// Create surface brush with opacity mask surface and specify stretch and alignment
CompositionSurfaceBrush starMaskBrush = _compositor.CreateSurfaceBrush(myStarMaskSurface);
starMaskBrush.Stretch = CompositionStretch.Uniform;
// SpriteVisual to be painted with masked content
SpriteVisual imageVisual = _compositor.CreateSpriteVisual();
imageVisual.Size = new Vector2(200, 200);
// Create MaskBrush
CompositionMaskBrush maskBrush = _compositor.CreateMaskBrush();
maskBrush.Source = imageBrush; // Set source to content that is to be masked
maskBrush.Mask = starMaskBrush; // Set mask to content that is the opacity mask
imageVisual.Brush = maskBrush; // Paint SpriteVisual with MaskBrush
return imageVisual;
}
将不透明蒙板应用于纯色填充以创建形状;对不透明蒙板 CompositionSurfaceBrush 上的转换属性进行动画处理,以创建颜色开花动画
private void CreateCircleMaskSprite(ICompositionSurface myCircleMaskSurface, ICompositionSurface myImageSurface)
{
// SpriteVisual to be painted by MaskBrush
_circleVisual = _compositor.CreateSpriteVisual();
_circleVisual.Size = MyContainerGrid.RenderSize.ToVector2();
// SurfaceBrush w/ image as MaskBrush.Source
CompositionSurfaceBrush imageBrush = _compositor.CreateSurfaceBrush(myImageSurface);
imageBrush.Stretch = CompositionStretch.UniformToFill;
// SurfaceBrush w/ opacity mask surface as MaskBrush.Mask
CompositionSurfaceBrush circleBrush = _compositor.CreateSurfaceBrush(myCircleMaskSurface);
// Specify stretch and transform properties to scale and position brush for initial state
circleBrush.Stretch = CompositionStretch.Uniform;
circleBrush.CenterPoint = _circleVisual.Size / 2;
circleBrush.Scale = new Vector2(0.5f);
// Create MaskBrush
CompositionMaskBrush maskBrush = _compositor.CreateMaskBrush();
maskBrush.Source = imageBrush;
maskBrush.Mask = circleBrush;
_circleVisual.Brush = maskBrush;
// Set SpriteVisual as child of XAML Grid
ElementCompositionPreview.SetElementChildVisual(MyContainerGrid, _circleVisual);
}
private void BloomAnimation()
{
// Create KeyFrame Animation to target Scale transform on SurfaceBrush
Vector2KeyFrameAnimation scaleAnimation = _compositor.CreateVector2KeyFrameAnimation();
scaleAnimation.InsertKeyFrame(1.0f, new Vector2(2));
scaleAnimation.Duration = TimeSpan.FromMilliseconds(500);
var maskBrush = (CompositionMaskBrush)_circleVisual.Brush;
var surfaceBrush = (CompositionSurfaceBrush) maskBrush.Mask;
// Animate the Scale on the circle opacity mask
surfaceBrush.StartAnimation("Scale", scaleAnimation);
}
应用圆角矩形蒙板创建纯色圆角矩形,其角半径在调整大小后不会增加 (使用 CompositionNineGridBrush 作为 CompositionMaskBrush ) 的输入。
private SpriteVisual CreateMaskedRoundedRectVisual(ICompositionSurface myRoundedRectMaskSurface)
{
// ColorBrush to be set as MaskBrush.Source
CompositionColorBrush colorBrush = _compositor.CreateColorBrush(Colors.Blue);
// SurfaceBrush w/ opacity mask surface
CompositionSurfaceBrush roundedRectBrush = _compositor.CreateSurfaceBrush(myRoundedRectMaskSurface);
roundedRectBrush.Stretch = CompositionStretch.Fill; // stretch for center of nine-grid
// NineGridBrush w/ insets on opacity mask surface
CompositionNineGridBrush ninegridBrush = _compositor.CreateNineGridBrush();
ninegridBrush.Source = roundedRectBrush;
ninegridBrush.SetInsets(_cornerRadius); // the radius, in pixels, of the corner as specified on my opacity mask surface
// Create MaskBrush
CompositionMaskBrush maskBrush = _compositor.CreateMaskBrush();
maskBrush.Source = colorBrush;
maskBrush.Mask = ninegridBrush;
// Paint SpriteVisual with MaskBrush
SpriteVisual sprite = _compositor.CreateSpriteVisual();
sprite.Size = new Vector2(300, 200);
sprite.Brush = maskBrush;
return sprite;
}
注解
CompositionMaskBrush 不能设置为 CompositionEffectBrush 的源参数。 如果要将 IGraphicsEffect 应用于掩码内容,请改用 CompositionEffectBrush 和 复合效果 。
属性
Comment |
要与 CompositionObject 关联的字符串。 (继承自 CompositionObject) |
Compositor |
用于创建此 CompositionObject 的 Compositor。 (继承自 CompositionObject) |
Dispatcher |
CompositionObject 的调度程序。 (继承自 CompositionObject) |
DispatcherQueue |
获取 CompostionObject 的 DispatcherQueue 。 (继承自 CompositionObject) |
ImplicitAnimations |
附加到此对象的隐式动画集合。 (继承自 CompositionObject) |
Mask |
一个画笔,该画笔包含源画笔内容的不透明度掩码。 可以是 CompositionSurfaceBrush 或 CompositionNineGridBrush 类型。 |
Properties |
与 CompositionObject 关联的属性的集合。 (继承自 CompositionObject) |
Source |
一个画笔,其内容将由不透明度掩码屏蔽。 可以是 CompositionSurfaceBrush、CompositionColorBrush 或 CompositionNineGridBrush 类型。 |
方法
Close() |
关闭 CompositionObject 并释放系统资源。 (继承自 CompositionObject) |
ConnectAnimation(String, CompositionAnimation) |
连接和动画。 (继承自 CompositionObject) |
DisconnectAnimation(String) |
断开动画的连接。 (继承自 CompositionObject) |
Dispose() |
执行与释放或重置非托管资源关联的应用程序定义的任务。 (继承自 CompositionObject) |
PopulatePropertyInfo(String, AnimationPropertyInfo) |
定义可进行动画处理的属性。 (继承自 CompositionObject) |
StartAnimation(String, CompositionAnimation, AnimationController) |
使用 对象的指定属性连接动画并启动动画。 (继承自 CompositionObject) |
StartAnimation(String, CompositionAnimation) |
使用 对象的指定属性连接动画并启动动画。 (继承自 CompositionObject) |
StartAnimationGroup(ICompositionAnimationBase) |
启动动画组。 CompositionObject 上的 StartAnimationGroup 方法可用于启动 CompositionAnimationGroup。 组中的所有动画将同时在 对象上启动。 (继承自 CompositionObject) |
StopAnimation(String) |
断开动画与指定属性的连接并停止动画。 (继承自 CompositionObject) |
StopAnimationGroup(ICompositionAnimationBase) |
停止动画组。 (继承自 CompositionObject) |
TryGetAnimationController(String) |
返回指定属性上运行的动画的 AnimationController。 (继承自 CompositionObject) |