Silverlight StoryboardManager管理类

写的不好,希望志同道合者给点建议,或者直接修改来完善它,当前版本暂定为V1.0

通过简单的设置ScaleDoubleAnimation、SkewDoubleAnimation、RotateDoubleAnimation、TranslateDoubleAnimation属性快速的实现放大缩小、倾斜、旋转、位移的动画效果

然后执行 storyboardManager.Storyboard.Begin();即可实现简单的动画效果,也可以同时给多个属性赋值,实现多种动画效果组合。

目前还不太灵活,希望大家帮忙改进,或提一些宝贵意见,谢谢

具体使用方法在下面

StoryboardManager全部代码

 

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Alex.Silverlight.Controls
{
    /// 
    /// 
    public class StoryboardManager
    {
        private const string PROPERTYPATH_PART = "(FrameworkElement.RenderTransform).(TransformGroup.Children)[";

        Storyboard _storyboard = null;

        public Storyboard Storyboard
        {
            get { return _storyboard; }
        }

        private FrameworkElement _frameworkElement;

        public FrameworkElement FrameworkElement
        {
            get { return _frameworkElement; }
        }

        private ScaleDoubleAnimation _scaleDoubleAnimation = new ScaleDoubleAnimation();

        public ScaleDoubleAnimation ScaleDoubleAnimation
        {
            get { return _scaleDoubleAnimation; }
            set
            {
                _scaleDoubleAnimation = value;
            }
        }

        private SkewDoubleAnimation _skewDoubleAnimation = new SkewDoubleAnimation();

        public SkewDoubleAnimation SkewDoubleAnimation
        {
            get { return _skewDoubleAnimation; }
            set
            {
                _skewDoubleAnimation = value;
            }
        }

        private RotateDoubleAnimation _rotateDoubleAnimation = new RotateDoubleAnimation();

        public RotateDoubleAnimation RotateDoubleAnimation
        {
            get { return _rotateDoubleAnimation; }
            set
            {
                _rotateDoubleAnimation = value;
            }
        }

        private TranslateDoubleAnimation _translateDoubleAnimation = new TranslateDoubleAnimation();

        public TranslateDoubleAnimation TranslateDoubleAnimation
        {
            get { return _translateDoubleAnimation; }
            set
            {
                _translateDoubleAnimation = value;
            }
        }

        private Point _renderTransformOrigin = new Point(0.5, 0.5);

        public Point RenderTransformOrigin
        {
            get { return _renderTransformOrigin; }
            set { _renderTransformOrigin = value; }
        }


        public StoryboardManager(FrameworkElement frameworkElement)
        {
            this._frameworkElement = frameworkElement;

            FrameworkElementSet();
            _storyboard = new Storyboard();

            _scaleDoubleAnimation.DoubleAnimationChange += new DoubleAnimationChangeEventHandler(DoubleAnimation_DoubleAnimationChange);
            _skewDoubleAnimation.DoubleAnimationChange += new DoubleAnimationChangeEventHandler(DoubleAnimation_DoubleAnimationChange);
            _rotateDoubleAnimation.DoubleAnimationChange += new DoubleAnimationChangeEventHandler(DoubleAnimation_DoubleAnimationChange);
            _translateDoubleAnimation.DoubleAnimationChange += new DoubleAnimationChangeEventHandler(DoubleAnimation_DoubleAnimationChange);
        }

        void DoubleAnimation_DoubleAnimationChange(object sender, DoubleAnimationChangeEventArgs e)
        {
            DoubleAnimation doubleAnimation = (DoubleAnimation)sender;
            _storyboard.Children.Add(doubleAnimation);
            Storyboard.SetTarget(doubleAnimation, _frameworkElement);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(PROPERTYPATH_PART + (int)e.AnimationType + e.Propertypath, new object[] { }));
        }

        public void FrameworkElementSet()
        {
            _frameworkElement.RenderTransform = CreateTransformGroup();
            _frameworkElement.RenderTransformOrigin = _renderTransformOrigin;
        }

        private TransformGroup CreateTransformGroup()
        {
            TransformGroup transformGroup = new TransformGroup();

            ScaleTransform scaleTransform = new ScaleTransform();
            SkewTransform skewTransform = new SkewTransform();
            RotateTransform rotateTransform = new RotateTransform();
            TranslateTransform translateTransform = new TranslateTransform();

            transformGroup.Children.Add(scaleTransform);
            transformGroup.Children.Add(skewTransform);
            transformGroup.Children.Add(rotateTransform);
            transformGroup.Children.Add(translateTransform);

            return transformGroup;
        }
    }


    public delegate void DoubleAnimationChangeEventHandler(object sender, DoubleAnimationChangeEventArgs e);

    public class DoubleAnimationChangeEventArgs : EventArgs
    {
        public AnimationType AnimationType { set; get; }
        public DoubleAnimation Value { set; get; }
        public string Propertypath { set; get; }
    }

    public class ScaleDoubleAnimation
    {
        private DoubleAnimation _scaleX;

        public DoubleAnimation ScaleX
        {
            get { return _scaleX; }
            set
            {
                _scaleX = value;
                if (DoubleAnimationChange != null)
                    DoubleAnimationChange(this.ScaleX, new DoubleAnimationChangeEventArgs()
                    {
                        AnimationType = AnimationType.Scale,
                        Value = value,
                        Propertypath = "].(ScaleTransform.ScaleX)"
                    });
            }
        }

        private DoubleAnimation _scaleY;

        public DoubleAnimation ScaleY
        {
            get { return _scaleY; }
            set
            {
                _scaleY = value;
                if (DoubleAnimationChange != null)
                    DoubleAnimationChange(this.ScaleY, new DoubleAnimationChangeEventArgs()
                    {
                        AnimationType = AnimationType.Scale,
                        Value = value,
                        Propertypath = "].(ScaleTransform.ScaleY)"
                    });
            }
        }

        public event DoubleAnimationChangeEventHandler DoubleAnimationChange;
    }

    public class SkewDoubleAnimation
    {
        private DoubleAnimation _angleX;

        public DoubleAnimation AngleX
        {
            get { return _angleX; }
            set
            {
                _angleX = value;
                if (DoubleAnimationChange != null)
                    DoubleAnimationChange(this.AngleX, new DoubleAnimationChangeEventArgs()
                    {
                        AnimationType = AnimationType.Skew,
                        Value = value,
                        Propertypath = "].(SkewTransform.AngleX)"
                    });
            }
        }

        private DoubleAnimation _angleY;

        public DoubleAnimation AngleY
        {
            get { return _angleY; }
            set
            {
                _angleY = value;
                if (DoubleAnimationChange != null)
                    DoubleAnimationChange(this.AngleY, new DoubleAnimationChangeEventArgs()
                    {
                        AnimationType = AnimationType.Skew,
                        Value = value,
                        Propertypath = "].(SkewTransform.AngleY)"
                    });
            }
        }

        public event DoubleAnimationChangeEventHandler DoubleAnimationChange;
    }

    public class RotateDoubleAnimation
    {
        private DoubleAnimation _angle;

        public DoubleAnimation Angle
        {
            get { return _angle; }
            set
            {
                _angle = value;
                if (DoubleAnimationChange != null)
                    DoubleAnimationChange(this.Angle, new DoubleAnimationChangeEventArgs()
                    {
                        AnimationType = AnimationType.Rotate,
                        Value = value,
                        Propertypath = "].(RotateTransform.Angle)"
                    });
            }
        }

        public event DoubleAnimationChangeEventHandler DoubleAnimationChange;
    }

    public class TranslateDoubleAnimation
    {
        private DoubleAnimation _x;

        public DoubleAnimation X
        {
            get { return _x; }
            set
            {
                _x = value;
                if (DoubleAnimationChange != null)
                    DoubleAnimationChange(this.X, new DoubleAnimationChangeEventArgs()
                    {
                        AnimationType = AnimationType.Translate,
                        Value = value,
                        Propertypath = "].(TranslateTransform.X)"
                    });
            }
        }

        private DoubleAnimation _y;

        public DoubleAnimation Y
        {
            get { return _y; }
            set
            {
                _y = value;
                if (DoubleAnimationChange != null)
                    DoubleAnimationChange(this.Y, new DoubleAnimationChangeEventArgs()
                    {
                        AnimationType = AnimationType.Translate,
                        Value = value,
                        Propertypath = "].(TranslateTransform.Y)"
                    });
            }
        }

        public event DoubleAnimationChangeEventHandler DoubleAnimationChange;
    }

    public enum AnimationType
    {
        Scale,
        Skew,
        Rotate,
        Translate
    }
}

 

使用方法

 

void MouseLeave(object sender, MouseEventArgs e)
        {
            StoryboardManager storyboardManager = new StoryboardManager(sender as FrameworkElement);
            storyboardManager.Storyboard.Begin();
        }

        void MouseEnter(object sender, MouseEventArgs e)
        {
            StoryboardManager storyboardManager = new StoryboardManager(sender as FrameworkElement);
            storyboardManager.ScaleDoubleAnimation.ScaleX = new DoubleAnimation() { To = 1.05, Duration = TimeSpan.FromSeconds(0.2) };
            storyboardManager.ScaleDoubleAnimation.ScaleY = new DoubleAnimation() { To = 1.05, Duration = TimeSpan.FromSeconds(0.2) };

            storyboardManager.SkewDoubleAnimation.AngleX = new DoubleAnimation() { To = 1.05, Duration = TimeSpan.FromSeconds(0.2) };
            storyboardManager.SkewDoubleAnimation.AngleY = new DoubleAnimation() { To = 1.05, Duration = TimeSpan.FromSeconds(0.2) };
            storyboardManager.RotateDoubleAnimation.Angle = new DoubleAnimation() { To = 360, Duration = TimeSpan.FromSeconds(0.2) };
            storyboardManager.Storyboard.Begin();
        }

 

 

admin -
共有0个回答