【问题标题】:UWP Composition API: Rounded Corners?UWP 组合 API:圆角?
【发布时间】:2020-11-19 00:01:55
【问题描述】:

我正在努力想办法使用 Composition API 创建内容的圆角。这是我所在的地方,任何帮助将不胜感激:

void CreateRoundedCorners(Vector2 cornerRadius, CompositionSurfaceBrush imageSourceBrush, SpriteVisual targetVisual)
{
    CompositionRoundedRectangleGeometry roundedRectangle = _compositor.CreateRoundedRectangleGeometry();
    roundedRectangle.Size = new Vector2(;
    roundedRectangle.CornerRadius = cornerRadius;

    CompositionSpriteShape spriteShape = _compositor.CreateSpriteShape(roundedRectangle);
    spriteShape.FillBrush = _compositor.CreateColorBrush(Colors.Black);
    spriteShape.CenterPoint = new Vector2(_imageSize.X / 2, _imageSize.Y / 2);

    ShapeVisual spriteShapeVisual = _compositor.CreateShapeVisual();
    spriteShapeVisual.Size = _imageSize;
    spriteShapeVisual.Shapes.Add(spriteShape);

    CompositionMaskBrush maskBrush = _compositor.CreateMaskBrush();
    maskBrush.Source = imageSourceBrush;
    maskBrush.Mask = null; // How do I get the rectangle shape in here?

    targetVisual.Brush = maskBrush;
}

【问题讨论】:

    标签: uwp windows-runtime windows-composition-api


    【解决方案1】:

    我已经找到了解决方案。创建一个 CompositionVisualSurface,将 ShapeVisual 添加到其中,并从中创建一个 CompositionSurfaceBrush 以用作蒙版源。

    void CreateRoundedCorners(Vector2 cornerRadius, CompositionBrush imageSourceBrush, SpriteVisual targetVisual)
    {
        CompositionRoundedRectangleGeometry roundedRectangle =_compositor.CreateRoundedRectangleGeometry();
        roundedRectangle.Size = _imageSize;
        roundedRectangle.CornerRadius = cornerRadius;
    
        CompositionSpriteShape spriteShape = _compositor.CreateSpriteShape(roundedRectangle);
        spriteShape.FillBrush = _compositor.CreateColorBrush(Colors.Black);
        spriteShape.CenterPoint = new Vector2(_imageSize.X / 2, _imageSize.Y / 2);
    
        ShapeVisual spriteShapeVisual = _compositor.CreateShapeVisual();
        spriteShapeVisual.BorderMode = CompositionBorderMode.Soft;
        spriteShapeVisual.Size = _imageSize;
        spriteShapeVisual.Shapes.Add(spriteShape);
    
        CompositionVisualSurface surface = _compositor.CreateVisualSurface();
        surface.SourceSize = _imageSize;
        surface.SourceVisual = spriteShapeVisual;
    
        CompositionMaskBrush maskBrush = _compositor.CreateMaskBrush();
        maskBrush.Source = imageSourceBrush;
        maskBrush.Mask = _compositor.CreateSurfaceBrush(surface);
    
        targetVisual.Brush = maskBrush;
    }
    

    编辑: 蒙版也可以从 Shape 中获得,但前提是它已经在可视化树中:

    Windows.UI.Xaml.Shapes.Shape rect = new Rectangle();
    CompositionBrush mask = rect.GetAlphaMask();
    

    【讨论】:

    • 如果您的问题已经解决,请标记自己。
    【解决方案2】:

    UWP 合成 API:圆角?

    根据您的要求,您可以使用SetElementChildVisual 方法将CompositionRoundedRectangleGeometry 添加到当前页面。

    例如:

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        var visual = ElementCompositionPreview.GetElementVisual(this);
        var compositor = visual.Compositor;
      
        CompositionRoundedRectangleGeometry roundedRectangle = compositor.CreateRoundedRectangleGeometry();
        roundedRectangle.Size = new Vector2(200, 200);
        roundedRectangle.CornerRadius = new Vector2(30,30);
    
        CompositionSpriteShape spriteShape = compositor.CreateSpriteShape(roundedRectangle);
        spriteShape.FillBrush = compositor.CreateColorBrush(Colors.Blue);
        spriteShape.CenterPoint = new Vector2(100, 100);
    
        ShapeVisual spriteShapeVisual = compositor.CreateShapeVisual();
        spriteShapeVisual.Size = new Vector2(200, 200); 
        
        spriteShapeVisual.Shapes.Clear();
        spriteShapeVisual.Shapes.Add(spriteShape);
      
        ElementCompositionPreview.SetElementChildVisual(this, spriteShapeVisual);
    }
    

    【讨论】:

    • 感谢您的回复,但这只是绘制了一个带圆角的蓝色矩形。我正在尝试在任何类型的 CompositionBrush 上获得圆角,包括加载的图像或视频。这个想法是使用矩形形状作为不透明蒙版。
    • 为什么不用CornerRadius直接设置控制角半径呢?
    • 是的,如果首先加载 Grid 似乎确实有效,但这对我来说在性能方面会出现问题,而且我很确定阴影不会有圆角这样做。
    • 好的,我发现你已经发布了自己的答案,如果你的解决方案有效,你可以自己标记它
    • 解决了。将 ShapeVisual.BorderMode 更改为“Soft”,现在它看起来就像 XAML CornerRadius。感谢您的帮助,否则我不会注意到这一点。
    猜你喜欢
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    相关资源
    最近更新 更多