CompositionMaskBrush 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用应用了不透明蒙板的 CompositionBrush 绘制 SpriteVisual。 不透明蒙板的源可以是 CompositionColorBrush、CompositionLinearGradientBrush、CompositionSurfaceBrush、CompositionEffectBrush 或 CompositionNineGridBrush 类型的任意 CompositionBrush。 不透明度蒙板必须指定为 CompositionSurfaceBrush。
public ref class CompositionMaskBrush sealed : CompositionBrush
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.Foundation.LiftedContract, 65536)]
/// [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.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.Foundation.WindowsAppSDKContract, 65536)]
class CompositionMaskBrush final : CompositionBrush
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.Foundation.LiftedContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class CompositionMaskBrush : CompositionBrush
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.Foundation.WindowsAppSDKContract), 65536)]
public sealed class CompositionMaskBrush : CompositionBrush
Public NotInheritable Class CompositionMaskBrush
Inherits CompositionBrush
- 继承
- 属性
示例
将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) |
DispatcherQueue |
获取 CompositionObject 的 DispatcherQueue。 (继承自 CompositionObject) |
ImplicitAnimations |
附加到此 对象的隐式动画的集合。 (继承自 CompositionObject) |
Mask |
一个画笔,它包含源画笔的内容将用于屏蔽的不透明蒙板。 可以是 CompositionSurfaceBrush 或 CompositionNineGridBrush 类型。 |
Properties |
与 CompositionObject 关联的属性的集合。 (继承自 CompositionObject) |
Source |
一个画笔,其内容将由不透明蒙板屏蔽。 可以是 CompositionSurfaceBrush、CompositionColorBrush 或 CompositionNineGridBrush 类型。 |
方法
Close() |
关闭 CompositionObject 并释放系统资源。 (继承自 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) |