好玩的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


时间: 2024-08-08 14:55:29

好玩的WPF第三弹:颤抖吧,地球!消失吧,地球!的相关文章

好玩的WPF第四弹:用Viewport2DVisual3D实现3D旋转效果

效果呢就是这么个效果,但是大家要发挥想象力,比如做成一个可以旋转的按钮等等. 定义一个这样的资源就好. <Window.Resources> <DiffuseMaterial x:Key="DiffuseMaterialStyle" Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="White"/> </Window.Resources> 关键是在Gri

好玩的WPF第二弹:电子表字体显示时间+多彩呼吸灯特效button

我们先来看看Quartz MS字体动态显示系统时间的效果,难度相较于上一篇也要简单很多. 首先是定义一个TextBlock例如以下. <Grid> <TextBlock Name="tBlockTime" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="68" Foreground="Green"/>

好玩的WPF第二弹:电子表字体+显示系统时间

效果呢就是这么个效果,难度相较于上一篇也要简单许多. 首先是定义一个TextBlock如下. <Grid> <TextBlock Name="tBlockTime" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="68" Foreground="Green"/> </Grid> 后台代码如

日均百万PV架构第三弹(分布内容为王)

接续接上篇 缓存时代来临 为蓝本,继续改造我们的百万级站点架构,这次我们 拿之前存储静态内容的 nfs 开刀,众所周知 nfs 的多台集群节点下可能由于多重 原因(磁盘io , 网络带宽, 并发场景),不适合做文件共享系统的基础结构. 互联网站点中,存在大量图片或其他静态内容,并且这些内容一般在1M之内,对于 海量小文件,我们将采用mogilefs分布式文件系统来完成.其中概念自行google. # mogilefs分布式文件系统工作流程 架构已经愈发复杂,我们需要从新梳理一下.从下表中应该很容

《我与希乐仑》第三弹

此案编号:黄劳人仲 (2014) 办字 第518号 开庭前,发生了一个戏剧性的场景,对方来了三个人,一位是外服的李盼,一位是徐敏,但还有一位叫王霞的,尽然说是Selerant的HR,扯淡嘛!此人来审判庭的目的非常值得怀疑,首先我有充分的证据证明她不属于Selerant,很简单!我们可以去调阅她的人事档案,至少在2014年3月26日当天,她肯定不属于Selerant,因为我清楚地记得她是徐敏的朋友,所以罗,纯粹是打酱油的,也或许是来拉关系的.你说一个不相干的人,庭上基本没说话,那她来干什么?这又不

codechef营养题 第三弹

第三弾が始まる! codechef problems 第三弹 一.Motorbike Racing 题面 It's time for the annual exciting Motorbike Race in Byteland. There are N motorcyclists taking part in the competition. Johnny is watching the race. At the present moment (time 0), Johnny has taken

深究angularJS系列 - 第三弹

深究angularJS系列 - 初识 深究angularJS系列 - 第二弹 深究angularJS系列 - 第三弹,我们一起深入探究angular的服务和自定义指令O(∩_∩)O~~ Angular服务 $http: $http是angular中的一个核心服务; $http利用浏览器的xmlhttprequest或JSONP与远程HTTP服务器进行交互; $http的支持多种method的请求,get.post.put.delete.jsonp等. 下面通过JSONP方法进行$http服务的使

Android Window PhoneWindow Activity学习心得--第三弹

Android Window  PhoneWindow Activity学习心得--第三弹 前面 我们完成了从Activity到PhoneWindow的整体跨度 正如我们所知道的与Activity组件关联的一个应用程序窗口视图对象关联一个ViewRoot对象,而将 一个Activity组件的应用程序窗口视图对象与一个ViewRoot对象关联是通过该Activity组件所使用的 窗口管理器(WindowManager)来执行的. 在我们初始化DecorView完成之后,我们需要关联应用程序窗口视图

[爱上Swift]第三弹:使用Swift建立App基本基石

搭架子 首先这次我们会主要使用IOS自带的导航Controller为一个APP建立一个简单的基石, 新建一个空的Application并创建3个swift文件,分别命名为:FirstViewController,SecondViewController,ThirdViewController; 同时在三个Swift的Controller中重写继承类的viewDidLoad()方法: override func viewDidLoad(){ super.viewDidLoad(); } 在整个程序