wpf实现动画效果

首先引入动画命名空间

using System.Windows.Media.Animation;

创建button控件,并且设置属性,并添加name

伸长动画

            //实例化一个动画
            DoubleAnimation shen = new DoubleAnimation();
            //设置动画的开始值
            shen.From = button.Width;
            //设置动画的结束值
            shen.To = 300;
            //设置动画时间
            shen.Duration = new Duration(TimeSpan.FromSeconds(1));
            //开始动画
            button.BeginAnimation(Button.WidthProperty, shen);

缩短动画

和伸长动画一样,只需改变结束的属性就行

            //实例化一个动画
            DoubleAnimation suo = new DoubleAnimation();
            //设置动画的开始值
            suo.From = button.Width;
            //设置动画的结束值
            suo.To = 100;
            //设置动画时间
            suo.Duration = new Duration(TimeSpan.FromSeconds(1));
            //开始动画
            but.BeginAnimation(Button.WidthProperty, suo);

同时缩放XY

            DoubleAnimation dawidth = new DoubleAnimation(button.Width, 150, new Duration(TimeSpan.FromSeconds(1)));
            DoubleAnimation daheight = new DoubleAnimation(button.Height, 150, new Duration(TimeSpan.FromSeconds(1)));
            //开始动画
            anniu.BeginAnimation(Button.WidthProperty, dawidth);
            anniu.BeginAnimation(Button.HeightProperty, daheight);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章