首页 > 其他分享 >WPF实现跳动的字符效果

WPF实现跳动的字符效果

时间:2023-08-09 19:44:23浏览次数:34  
标签:字符 storyboard 动画 AssociatedObject private 跳动 new WPF null

本文将介绍一个好玩但实际作用可能不太大的动画效果:跳动的字符。为了提高动画效果的可重用性以及调用的灵活性,通过Behavior实现跳动的字符动画。先看下效果:

image

技术要点与实现

通过TextEffectPositionStartPositionCount属性控制应用动画效果的子字符串的起始位置以及长度,同时使用TranslateTransform设置字符纵坐标的移动变换,以实现跳动的效果。主要步骤如下:

  • 在OnAttached方法中,注册Loaded事件,在Load事件中为TextBlock添加TextEffect效果,其中PositionCount设置为1,每次只跳动一个字符。
  • 添加启动动画效果的BeginEffect方法,并创建控制子字符纵向移动变换的线性动画。然后根据字符串(剔除空字符)的长度n,创建n个关键帧,每个关键帧中把PositionStart设置为要跳动的字符在字符串中的索引
  • 在开启动画属性IsEnabled=trueTextBlock内容变化时,启动动画效果

在创建关键帧设置跳动字符位置时剔除了空字符,是为了是动画效果显得连贯

public class DanceCharEffectBehavior : Behavior<TextBlock>
    {
        private TextEffect _textEffect;
        private string _textEffectName;
        private TranslateTransform _translateTransform = null;
        private string _translateTransformName;
        private Storyboard _storyboard;

        protected override void OnAttached()
        {
            base.OnAttached();

            this.AssociatedObject.Loaded += AssociatedObject_Loaded;
            this.AssociatedObject.Unloaded += AssociatedObject_Unloaded;
            this.AssociatedObject.IsVisibleChanged += AssociatedObject_IsVisibleChanged;
            BindingOperations.SetBinding(this, DanceCharEffectBehavior.InternalTextProperty, new Binding("Text") { Source = this.AssociatedObject });
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            this.AssociatedObject.Loaded -= AssociatedObject_Loaded;
            this.AssociatedObject.Unloaded -= AssociatedObject_Unloaded;
            this.AssociatedObject.IsVisibleChanged -= AssociatedObject_IsVisibleChanged;
            this.ClearValue(DanceCharEffectBehavior.InternalTextProperty);

            if (_storyboard != null)
            {
                _storyboard.Remove(this.AssociatedObject);
                _storyboard.Children.Clear();
            }
            if (_textEffect != null)
                this.AssociatedObject.TextEffects.Remove(_textEffect);
        }

        private void AssociatedObject_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue == false)
            {
                if (_storyboard != null)
                    _storyboard.Stop(this.AssociatedObject);
            }
            else
            {
                BeginEffect(this.AssociatedObject.Text);
            }
        }

        private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
        {
            if (_textEffect == null)
            {
                this.AssociatedObject.TextEffects.Add(_textEffect = new TextEffect()
                {
                    PositionCount = 1,
                    Transform = _translateTransform = new TranslateTransform(),
                });
                NameScope.SetNameScope(this.AssociatedObject, new NameScope());
                this.AssociatedObject.RegisterName(_textEffectName = "n" + Guid.NewGuid().ToString("N"), _textEffect);
                this.AssociatedObject.RegisterName(_translateTransformName = "n" + Guid.NewGuid().ToString("N"), _translateTransform);
                if (IsEnabled)
                    BeginEffect(this.AssociatedObject.Text);
            }
        }

        private void AssociatedObject_Unloaded(object sender, RoutedEventArgs e)
        {
            StopEffect();
        }


        private void SetEffect(string text)
        {
            if (string.IsNullOrEmpty(text) || this.AssociatedObject.IsLoaded == false)
            {
                StopEffect();
                return;
            }

            BeginEffect(text);

        }

        private void StopEffect()
        {
            if (_storyboard != null)
            {
                _storyboard.Stop(this.AssociatedObject);
            }
        }

        private void BeginEffect(string text)
        {
            StopEffect();

            int textLength = text.Length;
            if (textLength < 1 || _translateTransformName == null || IsEnabled == false) return;

            if (_storyboard == null)
                _storyboard = new Storyboard();
            double duration = 0.5d;
            DoubleAnimation da = new DoubleAnimation();

            Storyboard.SetTargetName(da, _translateTransformName);
            Storyboard.SetTargetProperty(da, new PropertyPath(TranslateTransform.YProperty));
            da.From = 0d;
            da.To = 10d;
            da.Duration = TimeSpan.FromSeconds(duration / 2d);
            da.RepeatBehavior = RepeatBehavior.Forever;
            da.AutoReverse = true;

            char emptyChar = ' ';
            List<int> lsb = new List<int>();
            for (int i = 0; i < textLength; ++i)
            {
                if (text[i] != emptyChar)
                {
                    lsb.Add(i);
                }
            }

            Int32AnimationUsingKeyFrames frames = new Int32AnimationUsingKeyFrames();
            Storyboard.SetTargetName(frames, _textEffectName);
            Storyboard.SetTargetProperty(frames, new PropertyPath(TextEffect.PositionStartProperty));
            frames.Duration = TimeSpan.FromSeconds((lsb.Count) * duration);
            frames.RepeatBehavior = RepeatBehavior.Forever;
            frames.AutoReverse = true;

            int ii = 0;
            foreach (int index in lsb)
            {
                frames.KeyFrames.Add(new DiscreteInt32KeyFrame()
                {
                    Value = index,
                    KeyTime = TimeSpan.FromSeconds(ii * duration),
                });
                ++ii;
            }

            _storyboard.Children.Add(da);
            _storyboard.Children.Add(frames);
            _storyboard.Begin(this.AssociatedObject, true);
        }

        private string InternalText
        {
            get { return (string)GetValue(InternalTextProperty); }
            set { SetValue(InternalTextProperty, value); }
        }

        private static readonly DependencyProperty InternalTextProperty =
        DependencyProperty.Register("InternalText", typeof(string), typeof(DanceCharEffectBehavior),
        new PropertyMetadata(OnInternalTextChanged));

        private static void OnInternalTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var source = d as DanceCharEffectBehavior;
            if (source._storyboard != null)
            {
                source._storyboard.Stop(source.AssociatedObject);
                source._storyboard.Children.Clear();
            }
            source.SetEffect(e.NewValue == null ? string.Empty : e.NewValue.ToString());
        }


        public bool IsEnabled
        {
            get { return (bool)GetValue(IsEnabledProperty); }
            set { SetValue(IsEnabledProperty, value); }
        }

        public static readonly DependencyProperty IsEnabledProperty =
            DependencyProperty.Register("IsEnabled", typeof(bool), typeof(DanceCharEffectBehavior), new PropertyMetadata(true, (d, e) =>
            {
                bool b = (bool)e.NewValue;
                var source = d as DanceCharEffectBehavior;
                source.SetEffect(source.InternalText);
            }));

    }

调用的时候只需要在TextBlock添加Behavior即可,代码如下

<TextBlock FontSize="20" Text="Hello">
    <i:Interaction.Behaviors>
        <local:DanceCharEffectBehavior x:Name="titleEffect" IsEnabled="True" />
    </i:Interaction.Behaviors>
</TextBlock>

结尾

本例中还有许多可以完善的地方,比如字符跳动的幅度可以根据实际的FontSize来设置,或者增加依赖属性来控制;动画是否倒退播放,是否循环播放,以及动画的速度都可以通过增加依赖属性在调用时灵活设置。

标签:字符,storyboard,动画,AssociatedObject,private,跳动,new,WPF,null
From: https://www.cnblogs.com/czwy/p/17615551.html

相关文章

  • php如何定义多维数组以某个字符去输出对应的值
    $arr=[['id'=>123,'test'=>['id'=>2,'title'=>"测试",'test3'=>['list'=>123]]]];$field="test.test3.list";foreach($ar......
  • 字符设备驱动-11.mmap机制
    1引入mmap应用程序和驱动程序之间传递数据时,可以通过read、write函数进行,用户态和内核态的数据交互一般用copy_from_user,copy_to_user。这种方式在数据量比较小时没什么问题;但是数据量比较大时效率就太低了。比如更新LCD显示时,如果每次都让APP传递一帧数据给内核,假设......
  • String字符串的时间类型比较大小
    两个时间类型的字符串,要进行大小比较比如2023-01-02T00:38:20和2023-11-02T21:00:20这两个时间,是字符串的,要进行比较如果转成时间,多少有点不太乐意,有点麻烦。更为简单的方式是直接使用compareTopublicstaticvoidmain(String[]args){Stringtime1="......
  • 字符串的优化
    C#正确操作字符串编程语言中,字符串类型是操作最频繁的,使用不慎,会带来额外的开销第一、尽量减少装箱。例如stringstr1="stri1"+9;                   //(1)stringstr2="str2"+9.ToString();   //(2)(1)中9发生了装箱的操作,(2)没有发生装箱行......
  • 【Python】日期格式转换 <字符串、datetime、date>
    #coding:utf-8importdatetimefromdateutil.parserimportparsefromdateutil.relativedeltaimportrelativedeltafromloguruimportloggeraslogsclassdate_transform:"""日期格式转换"""defauto(self,d):"......
  • 界面控件DevExpress WPF Chart组件——拥有超快的数据可视化库!
    DevExpressWPF Chart组件拥有超大的可视化数据集,并提供交互式仪表板与高性能WPF图表库。DevExpressCharts提供了全面的2D/3D图形集合,包括数十个UI定制和数据分析/数据挖掘选项。PS:DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。......
  • 【pandas小技巧】--字符串转数值
    字符串转数字的用途和场景很多,其中主要包括以下几个方面:数据清洗:在进行数据处理时,经常会遇到一些数据类型不匹配的问题,比如某些列中的字符串类型被误认为是数字类型,此时需要将这些字符串类型转换为数字类型,才能进行后续的数值计算或统计分析。数据整理:有时候输入的原始数据可能......
  • 【随手记】Mybatis报错 错误信息:ORA-00911: 无效字符
    注意@param注解是属于哪个包的这个有的时候会有影响接收不到参数xml里面不要加分号查了半天Bug最后发现是xml里面的sql语句后面加了个;,删掉就好了。......
  • 解决winform调用wpf窗体时原窗体缩小的问题
    在使用winform调用wpf窗体时,原来的winform窗体会缩小,同时分辨率会发生变化,用如下方法来解决这个问题。首先找到winform项目中的Properties ==>AssemblyInfo.cs,打开该文件,在末尾加入如下代码,之后重新运行即可。[assembly:System.Windows.Media.DisableDpiAwareness]//禁用WPF......
  • WPF自定义TreeView滚动条样式
     根据客户需求,要在TreeView目录树上显示10万+个节点,但是目录树显示10万加节点后,整个页面操作起来非常卡,所以给目录树增加了虚拟化设置。但是虚拟化设置一直没生效,后来经过排查发现是使用的自定义滚动条导致了虚拟化设置没有生效,后来自己写了一个滚动条样式,问题解决了。目录树虚......