UWP 구성 API : 둥근 모서리?
Nov 19 2020
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;
}
답변
1 SeanO'Neil Nov 19 2020 at 13:24
해결책을 찾았습니다. CompositionVisualSurface를 만들고, ShapeVisual을 추가하고, 여기에서 Mask 소스로 사용할 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;
}
편집 : 마스크는 셰이프에서도 얻을 수 있지만 이미 시각적 트리에있는 경우에만 가능합니다.
Windows.UI.Xaml.Shapes.Shape rect = new Rectangle();
CompositionBrush mask = rect.GetAlphaMask();
NicoZhu-MSFT Nov 19 2020 at 12:45
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);
}