WPF 星空动画效果 星星连线

该文章展示了一个使用WPF创建的星空动画效果,通过生成随机位置的圆形并将其连接成线,再结合动画和定时器计算线的长度,当线段超过一定长度时使其透明,从而模拟星星连线的动态景象。作者进行了性能测试,认为效果良好。
摘要由CSDN通过智能技术生成

WPF 星空动画效果 星星连线

测试WPF的性能,做了这样一个动画效果。实际测试感觉性能还可以,动画效果也还不错,分享一下
效果图在此:
请添加图片描述

  1. 先生成随机位置的圆形;
  2. 将圆形依次连接成线,将线的起始点和结束点绑定在圆的位置点;
  3. 生成随机的运动偏移点;
  4. 根据生成的随机偏移点进行计算,算出每个点运动到超出边界的目标位置;
  5. 开始圆形的动画,动画设置为自动回放模式;
  6. 设置一个定时器计算线的长度,超出一定长度时,使线段透明。

XAML:

<Window x:Class="AutoStar.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:AutoStar"
        mc:Ignorable="d"
        Title="MainWindow" Background="#1B1D26" WindowState="Normal" Height="450" Width="800" Loaded="Window_Loaded">
    <Window.Resources>
        <Style TargetType="Line">
            <Setter Property="Stroke" Value="#FFD8F4E9"/>
            <Setter Property="StrokeThickness" Value="1"/>
        </Style>
        <Style TargetType="Ellipse">
            <Setter Property="Stroke" Value="#53257DD9"/>
            <Setter Property="StrokeThickness" Value="1"/>
            <Setter Property="Fill" Value="#95FAE6"/>
            <Setter Property="Width" Value="8"/>
            <Setter Property="Height" Value="8"/>
        </Style>
    </Window.Resources>
    <Grid>
        <Canvas x:Name="canvas">
        </Canvas>
    </Grid>
</Window>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace AutoStar
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        Random random = new Random();
        Point range = new Point(800, 450);
        int starNum = 200;
        Dictionary<Ellipse, Point> ellipsePoints = new Dictionary<Ellipse, Point>();
        List<Line> lines = new List<Line>();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            range = new Point(ActualWidth, ActualHeight);
            Ellipse lastEllipse = default(Ellipse);
            for (int i = 0; i < starNum; i++)
            {
                Point point = new Point(random.Next((int)range.X), random.Next((int)range.Y));
                Ellipse ellipse = new Ellipse();

                Canvas.SetLeft(ellipse, point.X);
                Canvas.SetTop(ellipse, point.Y);
                canvas.Children.Add(ellipse);
                ellipsePoints[ellipse] = new Point(random.Next(-5, 5), random.Next(-5, 5));
                while (ellipsePoints[ellipse] == default)
                {
                    ellipsePoints[ellipse] = new Point(random.Next(-5, 5), random.Next(-5, 5));
                }

                // 连线计算
                if (lastEllipse != default)
                {
                    Line line = new Line();
                    lines.Add(line);
                    line.SetBinding(Line.X1Property, new Binding("(Canvas.Left)") { Source = lastEllipse, Mode = BindingMode.OneWay });
                    line.SetBinding(Line.Y1Property, new Binding("(Canvas.Top)") { Source = lastEllipse, Mode = BindingMode.OneWay });
                    line.SetBinding(Line.X2Property, new Binding("(Canvas.Left)") { Source = ellipse, Mode = BindingMode.OneWay });
                    line.SetBinding(Line.Y2Property, new Binding("(Canvas.Top)") { Source = ellipse, Mode = BindingMode.OneWay });
                    Canvas.SetLeft(line, 4);
                    Canvas.SetTop(line, 4);
                    canvas.Children.Add(line);
                }

                lastEllipse = ellipse;


                // 动画计算
                int countTime = 1;
                Point offset = ellipsePoints[ellipse];
                while (point.X + offset.X > 0 && point.X + offset.X < range.X
                    && point.Y + offset.Y > 0 && point.Y + offset.Y < range.Y)
                {
                    point = new Point(point.X + offset.X, point.Y + offset.Y);
                    countTime++;
                }

                point = new Point(point.X + offset.X, point.Y + offset.Y);
                Storyboard storyboard = new Storyboard();
                storyboard.AutoReverse = true;
                storyboard.RepeatBehavior = RepeatBehavior.Forever;
                DoubleAnimation doubleAnimation = new DoubleAnimation() { To = point.X, Duration = new Duration(TimeSpan.FromMilliseconds(1000 * countTime)) };
                DoubleAnimation doubleAnimationY = new DoubleAnimation() { To = point.Y, Duration = new Duration(TimeSpan.FromMilliseconds(1000 * countTime)) };

                storyboard.Children.Add(doubleAnimation);
                storyboard.Children.Add(doubleAnimationY);
                Storyboard.SetTarget(doubleAnimation, ellipse);
                Storyboard.SetTarget(doubleAnimationY, ellipse);

                Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Left)"));
                Storyboard.SetTargetProperty(doubleAnimationY, new PropertyPath("(Canvas.Top)"));
                storyboard.Begin(ellipse);

            }


            DispatcherTimer dispatcherTimerLine = new DispatcherTimer(TimeSpan.FromMilliseconds(50), DispatcherPriority.Normal, OnTimeTickLineOpacity, Dispatcher);
            dispatcherTimerLine.Start();
        }

        private void OnTimeTickLineOpacity(object sender, EventArgs e)
        {
            int index = 0;
            foreach (var item in ellipsePoints)
            {
                if (index > 0)
                {
                    var ellpses = ellipsePoints.Keys.ToList();
                    var firstPoint = new Point(Canvas.GetLeft(ellpses[index - 1]), Canvas.GetTop(ellpses[index - 1]));
                    var secPoint = new Point(Canvas.GetLeft(ellpses[index]), Canvas.GetTop(ellpses[index]));

                    double dist = Math.Sqrt(Math.Pow(firstPoint.X - secPoint.X, 2) + Math.Pow(firstPoint.Y - secPoint.Y, 2));
                    var currentLine = lines[index - 1];

                    if (dist > 200)
                    {
                        currentLine.Opacity = 0;
                    }
                    else
                    {
                        currentLine.Opacity = 1 - (dist / 200);
                    }
                }
                index++;
            }
        }
    }
}

### 回答1: WPF动画效果 demo是一个展示WPF动画效果的演示程序。WPF是Windows Presentation Foundation的缩写,是微软开发的一种基于向量图形的桌面应用程序开发平台。WPF具有强大的图形渲染能力和动画效果的支持,可以帮助开发人员快速开发出富有交互性的应用程序。 WPF动画效果demo展示了WPF桌面应用程序开发中最常用的动画效果:平移、旋转、缩放和淡入淡出。这些动画效果可以通过WPF内置的动画类或自定义动画类实现。在demo中,每种动画效果都配有一个按钮,点击按钮即可观看相应的动画效果。 平移动画效果是通过设置元素的位置属性来实现的。demo中演示了一个按钮在平移时如何改变位置属性,并使用WPF提供的动画类来实现流畅的运动效果。 旋转动画效果是通过设置元素的旋转角度属性来实现的。demo中演示了如何通过使用动画类来控制元素的旋转角度,并使元素在旋转时保持流畅性。 缩放动画效果是通过设置元素的缩放属性来实现的。demo中演示了如何通过使用动画类来控制元素的缩放属性,实现元素在缩放时的平滑过渡效果。 淡入淡出动画效果是通过设置元素的不透明度属性来实现的。demo中演示了如何使用动画类来控制元素的不透明度属性,实现元素在淡入淡出时的平滑过渡效果。 总体来说,WPF动画效果demo是一个非常有用的资源,可以帮助开发人员更好地理解和掌握WPF动画效果的实现方法,并为开发交互性强的桌面应用程序提供有力支持。 ### 回答2: WPF动画效果demo是一种展示WPF技术所提供的动画效果的实例应用。WPF动画效果demo的目的是为了展示WPF技术中所包含的丰富的动画效果,以让开发者在实际应用中可以使用这些动画效果来制作更加生动、优美的应用界面。 WPF动画效果demo可以运用各种形式的动画效果,如平移、旋转、缩放、透明度等,通过动画效果的实时反馈来吸引用户的注意力,为用户提供更加丰富的视觉体验。在动画效果的展示中,可根据实际需求,自由控制动画的速率、节奏、方向等参数,以达到不同的效果表现。 此外,WPF动画效果demo还可以通过应用多种动画效果的组合来实现更为复杂的视觉效果,增强应用的互动性和吸引力。 总之,WPF动画效果demo是WPF技术中的一个重要应用示例,通过动态展示各种动画效果,让开发者们可以利用WPF技术来实现更为生动、灵活的应用界面,提高应用的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知名君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值