好玩的WPF第三弹:颤抖吧,地球!消失吧,地球!
我承认这一篇比较标题党,不过下面这个GIF貌似也和适合这个标题嘛。
(画质比较烂是因为CSDN的博客图片限制在2M,所以我设置的是20帧,时间也很短,大家可以自己把项目拷回去慢慢看)
这个最终设计出来的样式:
中间的小圆点是一个Button,外面是一个经过切割的Grid,Grid里面还有一个Image。
其中在加上Image(地球图片)之前,Button还是很大的,所以给他设计了渐变色。
<Button Padding="20" Foreground="White" BorderBrush="#FFD8A00A"
FontSize="16" Click="OnClick" Margin="100" Width="20" Height="20"
RenderTransformOrigin="0.54,-0.058">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#FF45ADB7" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Ellipse x:Name="bg" Fill="{TemplateBinding Background}"
Stroke="{TemplateBinding BorderBrush}" StrokeThickness="2" />
<Ellipse x:Name="fr" Opacity="0" >
<Ellipse.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#CCFFFFFF" Offset="0"/>
<GradientStop Offset="1"/>
<GradientStop Color="#7FFFFFFF" Offset="0.392"/>
</LinearGradientBrush>
</Ellipse.Fill>
</Ellipse>
<ContentPresenter x:Name="ContentPresenter" Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True" >
<Setter TargetName="fr" Property="Opacity" Value="1"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<Image Source="Earth.jpg" />
上面这两个控件都放到Grid内部。
<Grid x:Name="layoutroot">
<Grid.Resources>
<Storyboard x:Key="std">
<DoubleAnimation From="1" To="0" Duration="0:0:6"
Storyboard.TargetName="layoutroot"
Storyboard.TargetProperty="(UIElement.OpacityMask).(GradientBrush.GradientStops)[1].Offset"/>
<DoubleAnimation Duration="0:0:4.5" BeginTime="0:0:1.5" From="1" To="0"
Storyboard.TargetName="layoutroot"
Storyboard.TargetProperty="(UIElement.OpacityMask).(GradientBrush.GradientStops)[2].Offset"/>
<ColorAnimation Duration="0" To="#00000000" Storyboard.TargetName="layoutroot"
Storyboard.TargetProperty="(UIElement.OpacityMask).(GradientBrush.GradientStops)[2].Color"/>
</Storyboard>
</Grid.Resources>
<Grid.OpacityMask>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000" Offset="0"/>
<GradientStop Color="#FF000000" Offset="1"/>
<GradientStop Color="#FF000000" Offset="1"/>
</LinearGradientBrush>
</Grid.OpacityMask>
<Grid.Clip>
<EllipseGeometry Center="150 150" RadiusX="150" RadiusY="150"/>
</Grid.Clip>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFCFCFC" Offset="0.003"/>
<GradientStop Color="#FF76253C" Offset="1"/>
<GradientStop Color="#FF29769D" Offset="0.318"/>
<GradientStop Color="#FFA94444" Offset="0.84"/>
<GradientStop Color="#FFB2B62F" Offset="0.488"/>
<GradientStop Color="#FF9B2BD3" Offset="0.666"/>
<GradientStop Color="#FF5CC569" Offset="0.151"/>
</LinearGradientBrush>
</Grid.Background>
</Grid>
上面这些都是给Grid设置的渐变色,有了Image也没太大用了。
真正有用的是Resources。
后台文件中的抖动效果如下(在上一篇详细介绍了抖动过程):
// 全局变量
private double left = 0;
private double top = 0;
private Storyboard storyboard = new Storyboard();
// 初始化
left = mainWindow.Left;
top = mainWindow.Top;
private void DoubleAnimation()
{
// 窗口抖动效果
DoubleAnimation doubleAnimationL1 = new DoubleAnimation();
doubleAnimationL1.BeginTime = TimeSpan.FromSeconds(0.01);
doubleAnimationL1.Duration = TimeSpan.FromSeconds(0.01);
doubleAnimationL1.From = mainWindow.Left;
doubleAnimationL1.To = mainWindow.Left - 6;
doubleAnimationL1.EasingFunction = new BounceEase() { Bounces = 12, EasingMode = EasingMode.EaseInOut };
Storyboard.SetTarget(doubleAnimationL1, mainWindow);
Storyboard.SetTargetProperty(doubleAnimationL1, new PropertyPath("(Left)"));
DoubleAnimation doubleAnimationL2 = new DoubleAnimation();
doubleAnimationL2.BeginTime = TimeSpan.FromSeconds(0.001);
doubleAnimationL2.Duration = TimeSpan.FromSeconds(0.01);
doubleAnimationL2.From = mainWindow.Left;
doubleAnimationL2.To = mainWindow.Left + 6;
doubleAnimationL2.EasingFunction = new BounceEase() { Bounces = 12, EasingMode = EasingMode.EaseInOut };
Storyboard.SetTarget(doubleAnimationL2, mainWindow);
Storyboard.SetTargetProperty(doubleAnimationL2, new PropertyPath("(Left)"));
DoubleAnimation doubleAnimationT1 = new DoubleAnimation();
doubleAnimationT1.BeginTime = TimeSpan.FromSeconds(0.01);
doubleAnimationT1.Duration = TimeSpan.FromSeconds(0.01);
doubleAnimationT1.From = mainWindow.Top;
doubleAnimationT1.To = mainWindow.Top + 6; ;
doubleAnimationT1.EasingFunction = new BounceEase() { Bounces = 12, EasingMode = EasingMode.EaseInOut };
Storyboard.SetTarget(doubleAnimationT1, mainWindow);
Storyboard.SetTargetProperty(doubleAnimationT1, new PropertyPath("(Top)"));
DoubleAnimation doubleAnimationT2 = new DoubleAnimation();
doubleAnimationT2.BeginTime = TimeSpan.FromSeconds(0.01);
doubleAnimationT2.Duration = TimeSpan.FromSeconds(0.01);
doubleAnimationT2.From = mainWindow.Top;
doubleAnimationT2.To = mainWindow.Top - 6;
doubleAnimationT2.EasingFunction = new BounceEase() { Bounces = 12, EasingMode = EasingMode.EaseInOut };
Storyboard.SetTarget(doubleAnimationT2, mainWindow);
Storyboard.SetTargetProperty(doubleAnimationT2, new PropertyPath("(Top)"));
storyboard.Children.Add(doubleAnimationL1);
storyboard.Children.Add(doubleAnimationL2);
storyboard.Children.Add(doubleAnimationT1);
storyboard.Children.Add(doubleAnimationT2);
storyboard.RepeatBehavior = RepeatBehavior.Forever;
storyboard.Completed += new EventHandler(storyboard_Completed);
storyboard.Begin(this, true);
}
private void storyboard_Completed(object sender, EventArgs e)
{
// 解除绑定
storyboard.Remove(this);
// 解除TextWindow窗口
storyboard.Children.Clear();
//grid.Children.Clear();
// 恢复窗口初始位置
mainWindow.Left = left;
mainWindow.Top = top;
}
后台文件中的消失效果如下:
// 全局变量
Storyboard storyboard2 = null;
// 初始化
storyboard2 = (System.Windows.Media.Animation.Storyboard)layoutroot.Resources["std"];
storyboard2.Completed += (t, r) => this.Close();
this.layoutroot.Loaded += (aa, bb) =>
{
EllipseGeometry ellipsegeometry = (EllipseGeometry)this.layoutroot.Clip;
double dx = layoutroot.ActualWidth / 2d;
double dy = layoutroot.ActualHeight / 2d;
ellipsegeometry.Center = new Point(dx, dy);
ellipsegeometry.RadiusX = dx;
ellipsegeometry.RadiusY = dy;
};
然后是Button的OnClick事件:
private void OnClick(object sender, RoutedEventArgs e)
{
if (storyboard2 != null)
{
storyboard2.Begin();
}
DoubleAnimation();
}
源码的话,上面也都有了。GIF我就不再上传咯,毕竟2M的限制太无趣了。
感谢您的访问,希望对您有所帮助。 欢迎大家关注、收藏以及评论。
为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp
好玩的WPF第三弹:颤抖吧,地球!消失吧,地球!的更多相关文章
- 好玩的WPF第四弹:用Viewport2DVisual3D实现3D旋转效果
原文:好玩的WPF第四弹:用Viewport2DVisual3D实现3D旋转效果 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https:// ...
- 好玩的WPF第二弹:电子表字体显示时间+多彩呼吸灯特效button
我们先来看看Quartz MS字体动态显示系统时间的效果,难度相较于上一篇也要简单很多. 首先是定义一个TextBlock例如以下. <Grid> <TextBlock Name=& ...
- 好玩的WPF第一弹:窗口抖动+边框阴影效果+倒计时显示文字
原文:好玩的WPF第一弹:窗口抖动+边框阴影效果+倒计时显示文字 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csd ...
- 前端学习 第三弹: JavaScript语言的特性与发展
前端学习 第三弹: JavaScript语言的特性与发展 javascript的缺点 1.没有命名空间,没有多文件的规范,同名函数相互覆盖 导致js的模块化很差 2.标准库很小 3.null和unde ...
- WPF WebBrowser屏蔽弹出alert ,confirm ,prompt ,showModalDialog() ,window.open()
WPF WebBrowser屏蔽弹出alert ,confirm ,prompt ,showModalDialog() ,window.open()添加Microsoft.mshtml.dll,然后写 ...
- WPF案例 (三) 模拟QQ“快速换装"界面
原文:WPF案例 (三) 模拟QQ"快速换装"界面 这个小程序使用Wpf模拟QQ快速换装页面的动画特效,通过使用组合快捷键Ctrl+Left或Ctrl+Right,可实现Image ...
- 『PyTorch』第三弹重置_Variable对象
『PyTorch』第三弹_自动求导 torch.autograd.Variable是Autograd的核心类,它封装了Tensor,并整合了反向传播的相关实现 Varibale包含三个属性: data ...
- codechef营养题 第三弹
第三弾が始まる! codechef problems 第三弹 一.Motorbike Racing 题面 It's time for the annual exciting Motorbike Rac ...
- WPF入门(三)->几何图形之不规则图形(PathGeometry) (2)
原文:WPF入门(三)->几何图形之不规则图形(PathGeometry) (2) 上一节我们介绍了PathGeometry中LineSegment是点与点之间绘制的一条直线,那么我们这一节来看 ...
随机推荐
- Hbase常见异常 分类: B7_HBASE 2015-02-02 16:16 412人阅读 评论(0) 收藏
1. HBase is able to connect to ZooKeeper but the connection closes immediately hbase(main):001:0> ...
- js如何将字符串作为函数名调用函数
js将如何字符串作为函数名调用函数 一.总结 一句话总结:用eval来实现.eval可以执行参数字符串. 二.js将字符串作为函数名调用函数 比如我现在有一个字符串str = "func_a ...
- 使用truss、strace或ltrace诊断软件的"疑难杂症"
原文链接 简介 进程无法启动,软件运行速度突然变慢,程序的"Segment Fault"等等都是让每个Unix系统用户头痛的问题,本文通过三个实际案例演示如何使用truss.str ...
- Android自定义组件系列【7】——进阶实践(4)
上一篇<Android自定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识,这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpan ...
- 独立博客网站FansUnion.cn运营2年的经验和教训以及未来规划
今天,我把运营了2年的独立博客网站FansUnion给"归零"了. 2012年6月,我成功搭建了自己的博客网站FansUnion.cn,这是由于自己的不懈努力和时代发展成就的 ...
- Java 开源博客——B3log Solo 0.6.6 正式版发布了!
Java 开源博客 -- B3log Solo 0.6.6 正式版发布了!欢迎大家下载. 该版本引入了数据库连接池:Druid. 另外,欢迎观摩 B3log 团队的新项目:Noty,也非常欢迎大家参与 ...
- 【社交分享SDK】ShareSDK for Android 2.5.9已经公布
ShareSDK for Android 2.5.9已经公布 版本号:V2.5.9 2015-3-19 1.升级Dropbox对API接口的调用.包含授权.获取用户信息,分享三个接口 2.升级Kak ...
- HDU 5387 Clock(分数类+模拟)
题意: 给你一个格式为hh:mm:ss的时间,问:该时间时针与分针.时针与秒针.分针与秒针之间夹角的度数是多少. 若夹角度数不是整数,则输出最简分数形式A/B,即A与B互质. 解析: 先计算出总的秒数 ...
- SVN入门图解教程(超详细)
SVN入门图解教程(超详细) 一.总结 一句话总结: 二.SVN入门教程 1. 什么是SVN SVN全名Subversion,即版本控制系统.SVN与CVS一样,是一个跨平台的软件,支持大多数常见的操 ...
- 最新国内外可用SVN托管仓库有哪些
最新国内外可用SVN托管仓库哪些 一.总结 一句话总结:用SVNBucket和SourceForge 二.最新国内外可用SVN托管仓库推荐 这几年很多SVN托管平台都基本不维护或者直接关闭了,我翻遍了 ...