上一篇文章主要介绍了doubleanimation的应用,本文将深入探讨pointanimation的使用和扩展。
- 使用PointAnimation PointAnimation可以使Shape变形,但这种用法并不常见,因为WPF开发的软件通常不需要如此复杂的效果。
1.1 在XAML中使用PointAnimation 在XAML中使用PointAnimation的代码如下:
...
![[UWP]用Shape做动画(2):使用与扩展PointAnimation](https://img.php.cn/upload/article/001/503/042/175634918248747.jpg)
在这个例子中,最棘手的是Property-path语法,如果不熟悉,最好依赖Blend来生成。
1.2 在代码中使用PointAnimation 如果需要处理大量的Point,例如图表,通常会在C#代码中使用PointAnimation:
_storyboard = new Storyboard();
Random random = new Random();
for (int i = 0; i < _points.Count; i++)
{
var pointAnimation = new PointAnimation
{
Duration = TimeSpan.FromSeconds(2),
To = new Point(random.NextDouble() * 100, random.NextDouble() * 100)
};
Storyboard.SetTarget(pointAnimation, _points[i]);
Storyboard.SetTargetProperty(pointAnimation, new PropertyPath("Point"));
_storyboard.Children.Add(pointAnimation);
}
_storyboard.Begin();![[UWP]用Shape做动画(2):使用与扩展PointAnimation](https://img.php.cn/upload/article/001/503/042/175634918243824.jpg)
因为可以直接使用
SetTarget,所以Property-path语法可以简化。
- 扩展PointAnimation 上述例子的动画相对简单,如果动画更复杂,XAML或C#代码都会变得非常复杂。我参考了一个网页,想要实现类似的动画,但发现需要编写大量的XAML,因此放弃了使用PointAnimation来实现。这个网页的动画核心是以下HTML代码:
只需一组Point集合就可以控制所有点的动画,这比PointAnimation高效得多。在WPF中,可以通过继承Timeline来实现一个PointCollectionAnimation,具体可以参考这个项目。然而,UWP的Timeline类虽然不封闭,但不知道如何继承和派生自定义的Animation。
因此,需要稍微改变思路。可以将DoubleAnimation理解为:Storyboard将TimeSpan传递给DoubleAnimation,DoubleAnimation通过这个TimeSpan(有时还需要结合EasingFunction)计算出目标属性的当前值,最后传递给目标属性,如下图所示:
![[UWP]用Shape做动画(2):使用与扩展PointAnimation](https://img.php.cn/upload/article/001/503/042/175634918289347.jpg)
既然这样,也可以接收到这个计算出来的Double,再通过Converter计算出目标的PointCollection值:
![[UWP]用Shape做动画(2):使用与扩展PointAnimation](https://img.php.cn/upload/article/001/503/042/175634918213054.jpg)
假设告诉这个Converter当传入的Double值(命名为Progress)为0时,PointCollection是{0,0 1,1 ...},Progress为100时PointCollection是{1,1 2,2 ...},当Progress处于其中任何值时的计算方法如下:
private PointCollection GetCurrentPoints(PointCollection fromPoints, PointCollection toPoints, double percentage)
{
var result = new PointCollection();
for (var i = 0; i < fromPoints.Count; i++)
{
var fromPoint = fromPoints[i];
var toPoint = toPoints[i];
var x = fromPoint.X + (toPoint.X - fromPoint.X) * percentage / 100;
var y = fromPoint.Y + (toPoint.Y - fromPoint.Y) * percentage / 100;
result.Add(new Point(x, y));
}
return result;
}这样就完成了从TimeSpan到PointCollection的转换过程。然后就是在XAML中定义使用方式。参考上面的PointCollectionAnimation,虽然多了一个Converter,但XAML应该足够简洁:
97.3,0 127.4,60.9 194.6,70.7 145.9,118.1 157.4,185.1 97.3,153.5 37.2,185.1 48.6,118.1 0,70.7 67.2,60.9 110,58.2 147.3,0 192.1,29 141.7,105.1 118.7,139.8 88.8,185.1 46.1,156.5 0,125 23.5,86.6 71.1,116.7 ...
最终,我选择将这个Converter命名为
ProgressToPointCollectionBridge。可以看出,Polygon将Points绑定到ProgressToPointCollectionBridge,DoubleAnimation改变ProgressToPointCollectionBridge.Progress,从而改变Points。XAML的简洁程度还算令人满意,如果需要操作多个点,相对于PointAnimation的优势就很大。
运行结果如下:
![[UWP]用Shape做动画(2):使用与扩展PointAnimation](https://img.php.cn/upload/article/001/503/042/175634918323869.jpg)
完整的XAML如下:
97.3,0 127.4,60.9 194.6,70.7 145.9,118.1 157.4,185.1 97.3,153.5 37.2,185.1 48.6,118.1 0,70.7 67.2,60.9 110,58.2 147.3,0 192.1,29 141.7,105.1 118.7,139.8 88.8,185.1 46.1,156.5 0,125 23.5,86.6 71.1,116.7
ProgressToPointCollectionBridge的实现如下:
[ContentProperty(Name = nameof(Children))]
public class ProgressToPointCollectionBridge : DependencyObject
{
public ProgressToPointCollectionBridge()
{
Children = new ObservableCollection();
}
///
/// 获取或设置Points的值
///
public PointCollection Points
{
get { return (PointCollection)GetValue(PointsProperty); }
set { SetValue(PointsProperty, value); }
}
///
/// 获取或设置Progress的值
///
public double Progress
{
get { return (double)GetValue(ProgressProperty); }
set { SetValue(ProgressProperty, value); }
}
///
/// 获取或设置Children的值
///
public Collection Children
{
get { return (Collection)GetValue(ChildrenProperty); }
set { SetValue(ChildrenProperty, value); }
}
protected virtual void OnProgressChanged(double oldValue, double newValue)
{
UpdatePoints();
}
protected virtual void OnChildrenChanged(Collection oldValue, Collection newValue)
{
var oldCollection = oldValue as INotifyCollectionChanged;
if (oldCollection != null)
oldCollection.CollectionChanged -= OnChildrenCollectionChanged;
var newCollection = newValue as INotifyCollectionChanged;
if (newCollection != null)
newCollection.CollectionChanged += OnChildrenCollectionChanged;
UpdatePoints();
}
private void OnChildrenCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdatePoints();
}
private void UpdatePoints()
{
if (Children == null || Children.Any() == false)
{
Points = null;
}
else if (Children.Count == 1)
{
Points = Children[0];
}
else if (Children.Count == 2)
{
var fromPoints = Children[0];
var toPoints = Children[1];
Points = GetCurrentPoints(fromPoints, toPoints, Progress);
}
}
private PointCollection GetCurrentPoints(PointCollection fromPoints, PointCollection toPoints, double percentage)
{
var result = new PointCollection();
for (var i = 0; i < fromPoints.Count; i++)
{
var fromPoint = fromPoints[i];
var toPoint = toPoints[i];
var x = fromPoint.X + (toPoint.X - fromPoint.X) * percentage / 100;
var y = fromPoint.Y + (toPoint.Y - fromPoint.Y) * percentage / 100;
result.Add(new Point(x, y));
}
return result;
}
public static readonly DependencyProperty PointsProperty =
DependencyProperty.Register("Points", typeof(PointCollection), typeof(ProgressToPointCollectionBridge), new PropertyMetadata(null));
public static readonly DependencyProperty ProgressProperty =
DependencyProperty.Register("Progress", typeof(double), typeof(ProgressToPointCollectionBridge), new PropertyMetadata(0.0, OnProgressChanged));
private static void OnProgressChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var bridge = (ProgressToPointCollectionBridge)d;
bridge.OnProgressChanged((double)e.OldValue, (double)e.NewValue);
}
public static readonly DependencyProperty ChildrenProperty =
DependencyProperty.Register("Children", typeof(Collection), typeof(ProgressToPointCollectionBridge), new PropertyMetadata(null, OnChildrenChanged));
private static void OnChildrenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var bridge = (ProgressToPointCollectionBridge)d;
bridge.OnChildrenChanged((Collection)e.OldValue, (Collection)e.NewValue);
}
}
- 结语
如果将DoubleAnimation描述为“对目标的Double属性进行动画”,那么PointAnimation可以被理解为“对目标的Point.X和Point.Y两个Double属性同时进行动画”,而ColorAnimation则是“对目标的Color.A、R、G、B四个Int属性同时进行动画”。从这个角度来看,PointAnimation和ColorAnimation只是DoubleAnimation的延伸。进一步说,通过DoubleAnimation应该可以扩展出所有类型属性的动画。不过,我并不清楚如何在UWP上自定义动画,只能通过本文的折衷方式进行扩展。虽然XAML需要编写得更复杂一些,但这种方法也有其优点:
- 不需要了解太多与Animation相关的类的知识,只需要掌握依赖属性和绑定等基础知识即可。
- 不会因为动画API的变化而需要更改代码,可以兼容WPF、Silverlight和UWP(虽然我没有在WPF上实际测试这些代码)。
- 代码足够简单,省去了计算TimeSpan和EasingFunction的步骤。
- 稍微修改后还可以做成泛型的
AnimationBridge
,提供PointCollection以外的数据类型支持。
结合上一篇文章再发散一下,总觉得将来遇到UWP没有提供的功能都可以通过变通的方法实现,Binding和DependencyProperty真是UWP开发者的好朋友。
- 参考文献
- How SVG Shape Morphing Works
- Gadal MetaSyllabus










