WPF 实现倒计时转场动画~

元旦元旦团团圆圆、WPF开发者在此真诚的祝愿开发者们在新的一年里心想事成、万事如意!33de38993d2be92283074c48a024c250.png1eb5881be4f051df683ca3c04f0166c6.pngb7ace42864c75aff05890fe069f30c90.png

WPF开发者QQ群: 340500857

       由于微信群人数太多入群请添加小编微信号

 yanjinhuawechat 或 W_Feng_aiQ 入群

 需备注WPF开发者 

  PS:有更好的方式欢迎推荐。

01

代码如下

一、创建 CountdownTimer.xaml 继承ContentControl代码如下。

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;namespace WPFDevelopers.Controls
{public enum CountdownTimerEffect{Default,MultiColor}public class CountdownTimer : ContentControl{private Storyboard storyboard;private const double seconds = 800;private double currentSeconds = seconds;private Grid myGrid;public int Number{get { return (int)GetValue(NumberProperty); }set { SetValue(NumberProperty, value); }}public static readonly DependencyProperty NumberProperty =DependencyProperty.Register("Number", typeof(int), typeof(CountdownTimer), new PropertyMetadata(3));/// <summary>/// 完成后回到开始/// </summary>public bool IsFinishStart{get { return (bool)GetValue(IsFinishStartProperty); }set { SetValue(IsFinishStartProperty, value); }}public static readonly DependencyProperty IsFinishStartProperty =DependencyProperty.Register("IsFinishStart", typeof(bool), typeof(CountdownTimer), new PropertyMetadata(false));public CountdownTimerEffect CountdownTimerEffect{get { return (CountdownTimerEffect)GetValue(CountdownTimerEffectProperty); }set { SetValue(CountdownTimerEffectProperty, value); }}public static readonly DependencyProperty CountdownTimerEffectProperty =DependencyProperty.Register("ExhibitionEnum", typeof(CountdownTimerEffect), typeof(CountdownTimer), new PropertyMetadata(CountdownTimerEffect.Default));public override void OnApplyTemplate(){base.OnApplyTemplate();NameScope.SetNameScope(this, new NameScope());if (FontSize == SystemFonts.CaptionFontSize)FontSize = 200;FontFamily = DrawingContextHelper.FontFamily;storyboard = new Storyboard();myGrid = new Grid();myGrid.Name = "myGrid";myGrid.ToolTip = "MouseDown";myGrid.Background = new SolidColorBrush(Colors.White);var linearGradient = new LinearGradientBrush{GradientStops = new GradientStopCollection{new GradientStop{ Color = Colors.Red, Offset = 1 },new GradientStop{ Color = Colors.White, Offset = 1 },new GradientStop{ Color = Colors.White, Offset = .5 },new GradientStop{ Color = Colors.Red, Offset = .5 },new GradientStop{ Color = Colors.Red, Offset = 0 },new GradientStop{ Color = Colors.White, Offset = 0 },},StartPoint = new Point(0.5, 0),EndPoint = new Point(10, 10),SpreadMethod = GradientSpreadMethod.Reflect,MappingMode = BrushMappingMode.Absolute};SolidColorBrush solidColor;this.RegisterName(myGrid.Name, myGrid);var num = 0;for (int i = Number; i >= num; i--){var textBlock = new TextBlock();switch (CountdownTimerEffect){case CountdownTimerEffect.Default:if (i % 2 == 0)solidColor = Brushes.White;elsesolidColor = Brushes.Black;textBlock.Foreground = solidColor;break;case CountdownTimerEffect.MultiColor:textBlock.Foreground = linearGradient;break;}textBlock.Text = i.ToString();textBlock.Name = $"textBlock{i}";textBlock.FontSize = FontSize;textBlock.FontWeight = FontWeights.ExtraBold;textBlock.VerticalAlignment = VerticalAlignment.Center;textBlock.HorizontalAlignment = HorizontalAlignment.Center;textBlock.RenderTransformOrigin = new Point(.5, .5);textBlock.Effect = new DropShadowEffect{ShadowDepth = 2,RenderingBias = RenderingBias.Performance,Color = Colors.Red};if (!i.Equals(Number))textBlock.Opacity = 0;textBlock.RenderTransform = new ScaleTransform{ScaleX = 2,ScaleY = 2,};this.RegisterName(textBlock.Name, textBlock);TimeSpan beginTime = TimeSpan.Zero;if (storyboard.Children.Count > 0){beginTime = TimeSpan.FromMilliseconds(currentSeconds);currentSeconds += seconds;}var cubicEase = new CubicEase{EasingMode = EasingMode.EaseIn,};DoubleAnimation doubleAnimationScaleX = new DoubleAnimation();doubleAnimationScaleX.From = 2;doubleAnimationScaleX.To = 0;doubleAnimationScaleX.EasingFunction = cubicEase;Storyboard.SetTargetName(doubleAnimationScaleX, textBlock.Name);Storyboard.SetTargetProperty(doubleAnimationScaleX, new PropertyPath("(TextBlock.RenderTransform).(ScaleTransform.ScaleX)"));var doubleAnimationScaleY = new DoubleAnimation{From = 2,To = 0,EasingFunction = cubicEase};Storyboard.SetTargetName(doubleAnimationScaleY, textBlock.Name);Storyboard.SetTargetProperty(doubleAnimationScaleY, new PropertyPath("(TextBlock.RenderTransform).(ScaleTransform.ScaleY)"));doubleAnimationScaleX.BeginTime = beginTime;doubleAnimationScaleY.BeginTime = beginTime;doubleAnimationScaleX.Duration = TimeSpan.FromMilliseconds(seconds);doubleAnimationScaleY.Duration = TimeSpan.FromMilliseconds(seconds);if (!i.Equals(Number)){var doubleAnimationOpacity = new DoubleAnimation{Duration = TimeSpan.FromMilliseconds(0),BeginTime = beginTime,From = 0,To = 1};Storyboard.SetTargetName(doubleAnimationOpacity, textBlock.Name);Storyboard.SetTargetProperty(doubleAnimationOpacity, new PropertyPath(TextBlock.OpacityProperty));storyboard.Children.Add(doubleAnimationOpacity);}if (i % 2 == 0){var colorAnimation = new ColorAnimation{Duration = TimeSpan.FromMilliseconds(0),From = Colors.White,BeginTime = beginTime,To = Colors.Black};Storyboard.SetTargetName(colorAnimation, myGrid.Name);Storyboard.SetTargetProperty(colorAnimation, new PropertyPath("(Panel.Background).(SolidColorBrush.Color)"));storyboard.Children.Add(colorAnimation);}else{if (!i.Equals(Number)){var colorAnimation = new ColorAnimation{Duration = TimeSpan.FromMilliseconds(0),BeginTime = beginTime,From = Colors.Black,To = Colors.White};Storyboard.SetTargetName(colorAnimation, myGrid.Name);Storyboard.SetTargetProperty(colorAnimation, new PropertyPath("(Panel.Background).(SolidColorBrush.Color)"));storyboard.Children.Add(colorAnimation);}}storyboard.Children.Add(doubleAnimationScaleX);storyboard.Children.Add(doubleAnimationScaleY);myGrid.Children.Add(textBlock);}this.Content = myGrid;}protected override void OnMouseDown(MouseButtonEventArgs e){base.OnMouseDown(e);if (storyboard != null && storyboard.Children.Count > 0){storyboard.Completed += (s, y) =>{myGrid.Background = new SolidColorBrush(Colors.White);if (IsFinishStart){var scaleTransform = new ScaleTransform{ScaleX = 2,ScaleY = 2};var tb = myGrid.Children.Cast<TextBlock>().First();tb.RenderTransform = scaleTransform;}};storyboard.Begin(this);}}}
}

二、CountdownTimerExample.xaml 代码如下

<UserControl x:Class="WPFDevelopers.Samples.ExampleViews.CountdownTimerExample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"xmlns:wpfdev="https://github.com/yanjinhuagood/WPFDevelopers"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"><Grid Margin="10" Grid.Row="1"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><Border Margin="0,0,0,0" Background="{StaticResource WhiteSolidColorBrush}" CornerRadius="4,4,0,0" Effect="{StaticResource NormalShadowDepth}"><wpfdev:NavigateMenu TabStripPlacement="Top" SelectionChanged="NavigateMenu_SelectionChanged"><ListBoxItem Content="Default"/><ListBoxItem Content="MultiColor"/></wpfdev:NavigateMenu></Border><Border Grid.Row="1" Background="{StaticResource WhiteSolidColorBrush}" CornerRadius="0,0,4,4"Effect="{StaticResource NormalShadowDepth}"><Grid Margin="10"><wpfdev:CountdownTimer Number="3" x:Name="CountdownTimer1"/><UniformGrid Columns="4" Visibility="Collapsed" x:Name="CountdownTimerGroup"><wpfdev:CountdownTimer Number="9" CountdownTimerEffect="MultiColor" FontSize="150" IsFinishStart="True"/><wpfdev:CountdownTimer Number="5" CountdownTimerEffect="MultiColor" FontSize="150" IsFinishStart="True"/><wpfdev:CountdownTimer Number="2" CountdownTimerEffect="MultiColor" FontSize="150" IsFinishStart="True"/><wpfdev:CountdownTimer Number="7" CountdownTimerEffect="MultiColor" FontSize="150" IsFinishStart="True"/></UniformGrid></Grid></Border></Grid>
</UserControl>

三、CountdownTimerExample.xaml.cs 代码如下

using System.Windows;
using System.Windows.Controls;namespace WPFDevelopers.Samples.ExampleViews
{/// <summary>/// CountdownTimerExample.xaml 的交互逻辑/// </summary>public partial class CountdownTimerExample : UserControl{public CountdownTimerExample(){InitializeComponent();}private void NavigateMenu_SelectionChanged(object sender, SelectionChangedEventArgs e){var item = e.AddedItems[0] as ListBoxItem;if (item == null) return;switch (item.Content.ToString()){case "Default":if(CountdownTimer1.Visibility != Visibility.Visible){CountdownTimer1.Visibility = Visibility.Visible;CountdownTimerGroup.Visibility = Visibility.Collapsed;}break;case "MultiColor":if (CountdownTimerGroup.Visibility != Visibility.Visible){CountdownTimerGroup.Visibility = Visibility.Visible;CountdownTimer1.Visibility = Visibility.Collapsed;}break;}}}
}

02


效果预览

鸣谢素材提供者 - 贺龙

源码地址如下

Github:https://github.com/WPFDevelopersOrg

Gitee:https://gitee.com/WPFDevelopersOrg

WPF开发者QQ群: 340500857 

Github:https://github.com/WPFDevelopersOrg

出处:https://www.cnblogs.com/yanjinhua

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

转载请著名作者 出处 https://github.com/WPFDevelopersOrg

d01f794b6f7af35d4004af33f5a3736f.png

扫一扫关注我们,

3d85fc7e18798c126758fabecf4981f0.gif

更多知识早知道!

020820ddf6df901c47edf98e78f95689.gif

点击阅读原文可跳转至源代码

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/293267.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

su命令的详细用法

1.命令作用 su的作用是变更为其它使用者的身份&#xff0c;超级用户除外&#xff0c;需要键入该使用者的密码。 2.使用方式 su [-fmp] [-c command] [-s shell] [--help] [--version] [-] [USER [ARG]] 3.参数说明 -f &#xff0c; –fast&#xff1a;不必读启动文件&#xff0…

Partitioning Strategies

001、三种基本分区方式&#xff1a;Range、Hash、List。 002、Single-Level Partitioning 表以三种分区方式之一进行分区&#xff0c;使用一列或多列作为分区键。 Range Partitioning 范围分区将数据按照分区键的范围值分配到各个分区。这是最常见的分区方式&#…

Java类集-SortedSet接口

TreeSet实现了SortedSet接口 package iotest; import java.util.SortedSet; import java.util.TreeSet; public class sset { public static void main(String args[]){ SortedSet<String> allsnew TreeSet<String>(); alls.add("A"); alls.add("M&…

chmod 777 修改权限

在Unix和Linux的各种操作系统下&#xff0c;每个文件&#xff08;文件夹也被看作是文件&#xff09;都按读、写、运行设定权限。例如我用ls -l命令列文件表时&#xff0c;得到如下输出:-rw-r--r-- 1 apple users 2254 2006-05-20 13:47 tt.htm从第二个字符起rw-是说用户apple有…

WPF架构分析

<?xml version"1.0" encoding"UTF-8"?> 1.DisptcherObject提供了线程和并发模型&#xff0c;实现了消息系统。 2.DependencyObject提供了更改通知&#xff0c;实现了绑定&#xff0c;样式。3.Visual是托管API和非托管API&#xff08;milcore&#…

数字化架构

看过《EA企业架构》、《应用架构》和《IT与业务之间的鸿沟》文章的好多朋友给我发信息&#xff0c;能不能再写篇文章&#xff0c;各行各业的朋友都能看懂的、容易接受的&#xff0c;下面我将尽量尝试朝着这个方向努力写一篇数字化架构的文章&#xff0c;希望各界的朋友们都能有…

安卓手机运行python程序的软件-安卓手机定时运行python脚本

话说现在智能手机的计算能力严重过剩啊&#xff0c;不玩游戏、不打电话、不刷微信时&#xff0c;要充分利用手机的计算潜力为我所用。完全可以把一些台式电脑上的计算任务移到手机上来运行。python就是一个很好的跨平台的解决方案。今天谈谈安卓手机上python脚本的定时运行问题…

readonly和const的区别

readonly与const的区别1、const常量在声明的同时必须赋值&#xff0c;readonly在声明时可以不赋值2、readonly只能在声明时或在构造方法中赋值&#xff08;readonly的成员变量可以根据调用不同的构造方法赋不同的值&#xff09;3、const前面不能添加static&#xff08;因为Cons…

shell查找命令大全

1.whereis 文件名 特点:快速,但是是模糊查找,例如 找 #whereis mysql 它会把mysql,mysql.ini,mysql.*所在的目录都找出来.一般的查找都用这条命令.2.find / -name 文件名特点:准确,但速度慢,消耗资源大,例如我想找到php.ini的准确位置,就需要用#find / -name php.ini3.locate 文…

busybox怎么安装

BusyBox就好比是一个满满的工具箱&#xff0c;它包压缩了 Linux 的许多工具和命令&#xff0c;还包含了 Android 系统自带的shell。 第一步&#xff1a; 首先要把手机给Root了&#xff0c;有很多root手机的软件&#xff0c;下了之后按提示就可以把手机root了。 第二部&#…

快速理解ASP.NET Core的认证与授权

ASP.NET Core的认证与授权已经不是什么新鲜事了&#xff0c;微软官方的文档对于如何在ASP.NET Core中实现认证与授权有着非常详细深入的介绍。但有时候在开发过程中&#xff0c;我们也往往会感觉无从下手&#xff0c;或者由于一开始没有进行认证授权机制的设计与规划&#xff0…

有一个开始

初级入门&#xff1a;独上高楼&#xff0c;望尽天涯路。---------------------------------------我是分割线-----------------------------------------每天看3个以上的网页设计或者网页模版&#xff0c;说说为什么好或者不好&#xff1b;实现3个以上的特效&#xff0c;想想其…

字符用_连接的是什么加密_防水连接器外壳与铜针表面涂层有什么用?

防水连接器外壳与铜针表面涂层会关系到产品的质量&#xff0c;毕竟材料选择方面&#xff0c;以及手工劳动方面都是需要把好关的&#xff0c;这样我们才能确保做出来了的产品送至用户身上是最好的。(凌科BD系列防水连接器铜针镀金效果)1、无氰偏碱亮铜&#xff1a;在铜合金材料防…

【27前端】base标签带有href属性会让chrome里的svg元素url失效

一个chrome的问题&#xff0c;但具体原因不明。 触发条件&#xff1a;chrome浏览器base标签里href属性有值的时候 触发问题&#xff1a;svg里面的元素如果有用url的滤镜和模糊&#xff0c;则会失效&#xff0c;在firefox里和IE10没有发现这个问题。 正常状态&#xff1a; 有bas…

新成立的Scala中心将重点关注教育和Scala社区

在2016年6月的Scala Days柏林大会上&#xff0c;研究员Heather Miller在主题演讲中详细介绍了新成立的的Scala中心。这是一个非盈利性的组织&#xff0c;将重点关注教育和开源社区。\\InfoQ在最近发表的一篇文章中介绍过Scala中心&#xff0c;这里我们将介绍更多的细节。\\为什…

Android之root手机之后用busybox找到我们需要删除的文件

第一步&#xff1a;root手机 给手机root,我们可以使用kingroot 第二步&#xff1a;下载busybox 把下好的busybox放在ubuntu Desktop文件夹第三步&#xff1a;把busybox移到手机sdcard 打开Desktop终端adb push busybox-armv6l /sdcard/ 第四步&#xff1a;执行su命令 在ubun…

强大的矩阵奇异值分解(SVD)及其应用

本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用&#xff0c;但请注明出处&#xff0c;如果有问题&#xff0c;请联系wheeleastgmail.com 前言&#xff1a; 上一次写了关于PCA与LDA的文章&#xff0c;PCA的实现一般有两种&#xff0…

已婚男人看见美女都这个眼神?

1 答应我&#xff1a;穿汉服晚上就别骑车了&#xff01;&#xff08;素材来源网络&#xff0c;侵删&#xff09;▼2 真香定理从来不迟到▼3 这万圣节大餐吃得下去吗&#xff1f;&#xff08;素材来源网络&#xff0c;侵删&#xff09;▼4 这...也灵活了吧&#xff1f;▼5 谁…