0

0

[UWP]用Shape做动画(2):使用与扩展PointAnimation

爱谁谁

爱谁谁

发布时间:2025-08-28 10:46:14

|

1015人浏览过

|

来源于php中文网

原创

上一篇文章主要介绍了doubleanimation的应用,本文将深入探讨pointanimation的使用和扩展。

  1. 使用PointAnimation PointAnimation可以使Shape变形,但这种用法并不常见,因为WPF开发的软件通常不需要如此复杂的效果。

1.1 在XAML中使用PointAnimation 在XAML中使用PointAnimation的代码如下:


    
    
    

...

    
        
            
                
                
                
                
                
            
        
    

[UWP]用Shape做动画(2):使用与扩展PointAnimation

在这个例子中,最棘手的是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

因为可以直接使用

SetTarget
,所以Property-path语法可以简化。

  1. 扩展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

既然这样,也可以接收到这个计算出来的Double,再通过Converter计算出目标的PointCollection值:

[UWP]用Shape做动画(2):使用与扩展PointAnimation

假设告诉这个Converter当传入的Double值(命名为Progress)为0时,PointCollection是{0,0 1,1 ...},Progress为100时PointCollection是{1,1 2,2 ...},当Progress处于其中任何值时的计算方法如下:

ima.copilot
ima.copilot

腾讯大混元模型推出的智能工作台产品,提供知识库管理、AI问答、智能写作等功能

下载
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

完整的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);
}

}

  1. 结语 如果将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开发者的好朋友。

  1. 参考文献
  • How SVG Shape Morphing Works
  • Gadal MetaSyllabus

相关专题

更多
html版权符号
html版权符号

html版权符号是“©”,可以在html源文件中直接输入或者从word中复制粘贴过来,php中文网还为大家带来html的相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

591

2023.06.14

html在线编辑器
html在线编辑器

html在线编辑器是用于在线编辑的工具,编辑的内容是基于HTML的文档。它经常被应用于留言板留言、论坛发贴、Blog编写日志或等需要用户输入普通HTML的地方,是Web应用的常用模块之一。php中文网为大家带来了html在线编辑器的相关教程、以及相关文章等内容,供大家免费下载使用。

638

2023.06.21

html网页制作
html网页制作

html网页制作是指使用超文本标记语言来设计和创建网页的过程,html是一种标记语言,它使用标记来描述文档结构和语义,并定义了网页中的各种元素和内容的呈现方式。本专题为大家提供html网页制作的相关的文章、下载、课程内容,供大家免费下载体验。

458

2023.07.31

html空格
html空格

html空格是一种用于在网页中添加间隔和对齐文本的特殊字符,被用于在网页中插入额外的空间,以改变元素之间的排列和对齐方式。本专题为大家提供html空格的相关的文章、下载、课程内容,供大家免费下载体验。

240

2023.08.01

html是什么
html是什么

HTML是一种标准标记语言,用于创建和呈现网页的结构和内容,是互联网发展的基石,为网页开发提供了丰富的功能和灵活性。本专题为大家提供html相关的各种文章、以及下载和课程。

2852

2023.08.11

html字体大小怎么设置
html字体大小怎么设置

在网页设计中,字体大小的选择是至关重要的。合理的字体大小不仅可以提升网页的可读性,还能够影响用户对网页整体布局的感知。php中文网将介绍一些常用的方法和技巧,帮助您在HTML中设置合适的字体大小。

500

2023.08.11

html转txt
html转txt

html转txt的方法有使用文本编辑器、使用在线转换工具和使用Python编程。本专题为大家提供html转txt相关的文章、下载、课程内容,供大家免费下载体验。

306

2023.08.31

html文本框代码怎么写
html文本框代码怎么写

html文本框代码:1、单行文本框【<input type="text" style="height:..;width:..;" />】;2、多行文本框【textarea style=";height:;"></textare】。

418

2023.09.01

桌面文件位置介绍
桌面文件位置介绍

本专题整合了桌面文件相关教程,阅读专题下面的文章了解更多内容。

0

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 6.3万人学习

Excel 教程
Excel 教程

共162课时 | 10万人学习

PHP基础入门课程
PHP基础入门课程

共33课时 | 1.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号