【发布时间】:2017-09-16 18:51:15
【问题描述】:
我尝试在 UWP 应用中编写 Android 效果(波纹)。所以我在网格内(在我的用户控件中)创建了一个 EllipseGeometry 但是当我的 ellipseGeometry 的 RadiusX 和 RadiusY 播放动画时,我的 EllipseGeometry 增长出我的网格...... 我试图用可见区域限制路径并将其剪辑到路径,但没有成功。
这是我的 XAML 代码:
<UserControl
x:Class="UIComponents.POIButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UIComponents"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="100"
d:DesignWidth="650" Background="White">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Gray">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<!--Animation Ellipse-->
<Grid x:Name="ellipseContainer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" Grid.ColumnSpan="3">
<Path x:Name="path" Fill="Red" Stroke="Black" StrokeThickness="1" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5">
<Path.Data>
<EllipseGeometry x:Name="circleGeometry" Center="0,0" RadiusX="5" RadiusY="5" />
</Path.Data>
</Path>
</Grid>
<Rectangle x:Name="clickableRect" Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" PointerPressed="clickableRect_PointerPressed" Fill="Transparent" Tapped="clickableRect_Tapped"/>
</Grid>
<UserControl.Resources>
<Storyboard x:Name="RipplePath">
<DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="RadiusX" Storyboard.TargetName="circleGeometry">
<EasingDoubleKeyFrame KeyTime="0" Value="5"/>
<EasingDoubleKeyFrame KeyTime="0:0:10" Value="200"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="RadiusY" Storyboard.TargetName="circleGeometry">
<EasingDoubleKeyFrame KeyTime="0" Value="5"/>
<EasingDoubleKeyFrame KeyTime="0:0:10" Value="200"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
这是我的 Cs 代码:
private void clickableRect_Tapped(object sender, TappedRoutedEventArgs e)
{
Point touchPosition = e.GetPosition(ellipseContainer);
//RectangleGeometry visibleArea = new RectangleGeometry();
//visibleArea.Rect = new Rect(0, 0, 650, 100);
//path.Clip = visibleArea;
Storyboard animation = this.FindName("RipplePath") as Storyboard;
animation.Begin();
}
这里,结果:
非常感谢您的帮助:)
============== 试试 Justin XL 的解决方案:结果:
感谢贾斯汀的帮助,矩形几何似乎也被动画化了:
但网格上没有任何东西。
Xaml 代码是:
<UserControl
x:Class="UIComponents.POIButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UIComponents"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="100"
d:DesignWidth="650" Background="White">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Gray">
<Grid.Clip>
<RectangleGeometry Rect="0,0,650,100" />
</Grid.Clip> ...
CS 代码是:
public POIButton()
{
this.InitializeComponent();
var visual = ElementCompositionPreview.GetElementVisual(this);
visual.Clip = visual.Compositor.CreateInsetClip();
}
================ XAML 结果:
这个 XAML 代码没有产生正确的效果:
<UserControl
x:Class="UIComponents.POIButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UIComponents"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Height="100"
Width="650" Background="White">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Gray">
<Grid.Clip>
<RectangleGeometry Rect="0,0,650,100" />
</Grid.Clip>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<!--Animation Ellipse-->
<Grid x:Name="ellipseContainer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" Grid.ColumnSpan="3">
<Path x:Name="path" Fill="Red" Stroke="Black" StrokeThickness="1" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5">
<Path.Data>
<EllipseGeometry x:Name="circleGeometry" Center="0,0" RadiusX="5" RadiusY="5" />
</Path.Data>
</Path>
</Grid>...
但 CS 代码工作正常(在 XAML 代码中注释 RectangleGeometry 行):
this.InitializeComponent();
var visual = ElementCompositionPreview.GetElementVisual(this);
visual.Clip = visual.Compositor.CreateInsetClip();
制作这个:
感谢@Justin XL 的帮助 :)
【问题讨论】:
-
不是您问题的确切答案,但您应该在Github 上查看此示例中的 Color Bloom 动画。可能有点用处。
-
@JustinXL 又一个问题相关的用户界面
-
顺便说一句,XAML 方法也应该可以工作(我用你的确切代码试过,它工作得很好),你的代码中一定有其他东西导致了这个问题。
标签: c# android xaml animation uwp