C#WPF收缩对象动画

ScaleTransform 类

命名空间: System.Windows.Media

程序集: PresentationCore.dll

使用 ScaleTransform 可以水平或垂直拉伸或收缩对象。 ScaleX属性指定沿 x 轴拉伸或收缩对象的程度,而 ScaleY 属性指定沿 y 轴拉伸或收缩对象的量量。 缩放操作以和属性指定的点为中心 CenterX CenterY 。

		public void ScanAnimaction(object sender, MouseButtonEventArgs e)
        {
            Dispatcher.Invoke(() => {
                TextBlock textBlock = (TextBlock)sender;
                TransformGroup transformGroup = new TransformGroup();
                ScaleTransform scaleTransform = new ScaleTransform();
                transformGroup.Children.Add(scaleTransform);
                DoubleAnimation animation = new DoubleAnimation
                {
                    From = 1,
                    To = 0.9,
                    Duration = TimeSpan.FromSeconds(0.30),
                    FillBehavior = FillBehavior.Stop
                };
                textBlock.RenderTransform = transformGroup;
                scaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty, animation);
                scaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty, animation);
            });
        }

可用于实现点击时的动画效果

完整代码如下:

		private void Border_Click(object sender, MouseButtonEventArgs e)
        {
            //点击按键
            Border border = sender as Border;
            Type type = border.Child.GetType();
            if (type.Name.Equals("TextBlock"))
            {
                TextBlock textBlock = border.Child as TextBlock;
                if (textBlock.Text.Equals("清 空"))
                {
                    PassWord = Chongze.Const.HotelConst.EMPTY;
                    txtPassword.Text = Chongze.Const.HotelConst.EMPTY;
                }
                else if (txtPassword.Text.Length < 6)
                {
                    new Thread(() => { ScanAnimaction(textBlock, e); }).Start();
                    PassWord += textBlock.Text;
                    if (UserName != Chongze.Const.HotelConst.EMPTY)
                    {
                        txtPassword.Text += "*";
                    }
                    else
                    {
                        txtPassword.Text += textBlock.Text;
                    }
                    txtPassword.SelectionStart = txtPassword.Text.Length;
                }
            }
            else
            {
                if (txtPassword.Text.Length > 0)
                {
                    PassWord = PassWord.Remove(txtPassword.Text.Length - 1, 1);
                    txtPassword.Text = txtPassword.Text.Remove(txtPassword.Text.Length - 1, 1);
                }
                txtPassword.SelectionStart = txtPassword.Text.Length;
            }

        }
        //输入动画
        public void ScanAnimaction(object sender, MouseButtonEventArgs e)
        {
            Dispatcher.Invoke(() => {
                TextBlock textBlock = (TextBlock)sender;
                TransformGroup transformGroup = new TransformGroup();
                ScaleTransform scaleTransform = new ScaleTransform();
                transformGroup.Children.Add(scaleTransform);
                DoubleAnimation animation = new DoubleAnimation
                {
                    From = 1,
                    To = 0.9,
                    Duration = TimeSpan.FromSeconds(0.30),
                    FillBehavior = FillBehavior.Stop
                };
                textBlock.RenderTransform = transformGroup;
                scaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty, animation);
                scaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty, animation);
            });
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值