首页 > 其他分享 >wfp一个简单的动画

wfp一个简单的动画

时间:2023-01-05 12:33:36浏览次数:47  
标签:动画 int System private ellipses 简单 using wfp ellipse

https://www.cnblogs.com/Peter-Luo/p/12431549.html

<Window x:Class="WpfApp1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="396" Width="463.2">
    <Grid Margin="3">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal">
            <Button Margin="3" Padding="3" Click="cmdStart_Clicked">Start</Button>
            <Button Margin="3" Padding="3" Click="cmdStop_Clicked">Stop</Button>
        </StackPanel>
        <Canvas Name="canvas" Grid.Row="1" Margin="3"></Canvas>
    </Grid>
</Window>
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.Imaging;
using System.Windows.Shapes;

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



        private bool rendering = false;
        private void cmdStart_Clicked(object sender, RoutedEventArgs e)
        {
            if (!rendering)
            {
                ellipses.Clear();
                canvas.Children.Clear();

                CompositionTarget.Rendering += RenderFrame;
                rendering = true;
            }
        }
        private void cmdStop_Clicked(object sender, RoutedEventArgs e)
        {
            StopRendering();
        }

        private void StopRendering()
        {
            CompositionTarget.Rendering -= RenderFrame;
            rendering = false;
        }

        private List<EllipseInfo> ellipses = new List<EllipseInfo>();

        private double accelerationY = 0.1;
        private int minStartingSpeed = 1;
        private int maxStartingSpeed = 50;
        private double speedRatio = 0.1;
        private int minEllipses = 20;
        private int maxEllipses = 100;
        private int ellipseRadius = 10;

        private void RenderFrame(object sender, EventArgs e)
        {
            if (ellipses.Count == 0)
            {
                // Animation just started. Create the ellipses.
                int halfCanvasWidth = (int)canvas.ActualWidth / 2;

                Random rand = new Random();
                int ellipseCount = rand.Next(minEllipses, maxEllipses + 1);
                for (int i = 0; i < ellipseCount; i++)
                {
                    Ellipse ellipse = new Ellipse();
                    ellipse.Fill = Brushes.LimeGreen;
                    ellipse.Width = ellipseRadius;
                    ellipse.Height = ellipseRadius;
                    Canvas.SetLeft(ellipse, halfCanvasWidth + rand.Next(-halfCanvasWidth, halfCanvasWidth));
                    Canvas.SetTop(ellipse, 0);
                    canvas.Children.Add(ellipse);

                    EllipseInfo info = new EllipseInfo(ellipse, speedRatio * rand.Next(minStartingSpeed, maxStartingSpeed));
                    ellipses.Add(info);
                }
            }
            else
            {
                for (int i = ellipses.Count - 1; i >= 0; i--)
                {
                    EllipseInfo info = ellipses[i];
                    double top = Canvas.GetTop(info.Ellipse);
                    Canvas.SetTop(info.Ellipse, top + 1 * info.VelocityY);

                    if (top >= (canvas.ActualHeight - ellipseRadius * 2 - 10))
                    {
                        // This circle has reached the bottom.
                        // Stop animating it.
                        ellipses.Remove(info);
                    }
                    else
                    {
                        // Increase the velocity.
                        info.VelocityY += accelerationY;
                    }

                    if (ellipses.Count == 0)
                    {
                        // End the animation.
                        // There's no reason to keep calling this method
                        // if it has no work to do.
                        StopRendering();
                    }
                }
            }
        }
    }
    public class EllipseInfo
    {
        public Ellipse Ellipse
        {
            get;
            set;
        }

        public double VelocityY
        {
            get;
            set;
        }

        public EllipseInfo(Ellipse ellipse, double velocityY)
        {
            VelocityY = velocityY;
            Ellipse = ellipse;
        }
    }
}

 

标签:动画,int,System,private,ellipses,简单,using,wfp,ellipse
From: https://www.cnblogs.com/gisoracle/p/17027207.html

相关文章

  • 技术汇总:第十八章:枚举的简单使用
    结合上一章阅读:https://blog.csdn.net/java_wxid/article/details/99168098枚举代码:packagecom.javaliao.backstage;importlombok.Getter;publicenumMyData{......
  • 简单又好用的财务分析工具有哪些?
    什么样的财务分析工具才能算是简单又好用?是能够快速完成组合多变的财务指标运算分析;能够充分发挥企业经营健康晴雨表作用,反映企业财务健康状态;还是能够支持多维度动态分析、......
  • Ansible安装及简单使用
    一、配置免密互通参考地址:https://www.cnblogs.com/qq1035807396/p/16998602.html二、开始安装Ansible1、添加repoyuminstall-yhttps://dl.fedoraproject.org/......
  • Exchanger的简单使用
    Exchanger适用于超过一个线程之间数据交换  代码案例packagecom.java.test.exchanger;importorg.junit.Test;importjava.util.Random;importjava.util.con......
  • 09-简单数据类型和复杂数据类型
    简单数据类型简单数据类型null,返回的是一个空的对象object如果有个变量以后打算存储为对象,暂时没想好放啥,这个时候可以放null简单数据类型是存放在栈里面,里面......
  • 设计模式简单介绍
    注:所有知识来源于《设计模式:可复用软件面向对象的基础》1什么是设计模式ChristopherAlexander说过:“每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解......
  • Android-创建简单登陆界面
    文章目录​​1、问题描述​​​​2、结果展示​​​​3、具体实现​​​​4、代码实现​​1、问题描述使用androidstudio或者其他开发案桌工具,创建一个简易的登陆界面。2......
  • 诊断网络问题的7个简单步骤
    在您的计算机可能存在的所有类型的问题中,网络问题可能是最糟糕的问题之一。更糟糕的是,当您无法上网时,您无法轻松研究修复问题。让我们逐步完成您应该执行的过程来诊断网络......
  • 深入浅出简单JS加密解密案例
    加密是指使用密钥将明文(未加密的信息)转换为密文(无法被读取的信息)的过程。解密则是指使用相同的密钥将密文转换回明文的过程。JavaScript提供了一种内置的加密方法,称为​​......
  • 简单封装Poly
    非常优雅的马蜂#include<bits/stdc++.h>typedeflonglongll;typedefunsignedlonglongull;typedefdoubledb;typedeflongdoubleldb;#definefre(x)freopen(......