如何编写Silverlight动画效果控件
2011-06-18 22:37
501 查看
如何编写Silverlight动画效果控件
作为开发人员我一直存在2个问题,相信很多人也有这样的问题:
1、 懒惰,为了实现一些Silverlight特殊效果要写烦乱的XAML语句,这太可怕了;
2、 因为不是设计人员,很难做出一些非常美观的界面
于是,我就写了EffectControls控件集,目的是用最短的时间和最短的代码实现很酷的控件效果。开放源码,欢迎您的使用和改进,如果有好玩的特效,请一定要通知我。
View Code public static Storyboard JumpUpFrameworkElement(FrameworkElement frameworkElement, Double duration, Double delay, double height)
{
// Create the animation
DoubleAnimation AnimationUp = new DoubleAnimation();
// Set the values that the animation starts and ends with
AnimationUp.From = 0;
AnimationUp.To = height;
if (height < 23)
{
AnimationUp.To = 23;
}
// Set the time the animation is to begin
AnimationUp.BeginTime = TimeSpan.FromSeconds(delay);
// Set the duration of the animation
AnimationUp.Duration = new Duration(TimeSpan.FromSeconds(duration));
// Set the target of the animation
Storyboard.SetTarget(AnimationUp, frameworkElement);
// Set the property the animation is to affect
Storyboard.SetTargetProperty(AnimationUp, new PropertyPath(FrameworkElement.HeightProperty));
// Create a new storyboard
Storyboard stb = new Storyboard();
// Add the animation to the storyboard
stb.Children.Add(AnimationUp);
// Set return variable to the new storyboard
return stb;
}
public static Storyboard JumpLeftFrameworkElement(FrameworkElement frameworkElement, Double duration, Double delay, double width)
{
// Create the animation
DoubleAnimation AnimationUp = new DoubleAnimation();
// Set the values that the animation starts and ends with
AnimationUp.From = 0;
AnimationUp.To = width;
if (width < 150)
{
AnimationUp.To = 150;
}
// Set the time the animation is to begin
AnimationUp.BeginTime = TimeSpan.FromSeconds(delay);
// Set the duration of the animation
AnimationUp.Duration = new Duration(TimeSpan.FromSeconds(duration));
// Set the target of the animation
Storyboard.SetTarget(AnimationUp, frameworkElement);
// Set the property the animation is to affect
Storyboard.SetTargetProperty(AnimationUp, new PropertyPath(FrameworkElement.WidthProperty));
// Create a new storyboard
Storyboard stb = new Storyboard();
// Add the animation to the storyboard
stb.Children.Add(AnimationUp);
// Set return variable to the new storyboard
return stb;
}
public static Storyboard ColorChange(Control control, Double duration, Double delay, Color from, Color to,string property)
{
System.Windows.Media.Animation.ColorAnimation Animation = new System.Windows.Media.Animation.ColorAnimation();
Animation.From = from;
Animation.To = to;
SolidColorBrush myAnimatedBrush = new SolidColorBrush();
myAnimatedBrush.Color = from;
if (property == "Background")
{
(control as Button).Background = myAnimatedBrush;
}
if (property == "Foreground")
{
(control as Button).Foreground = myAnimatedBrush;
}
//page.RegisterName("MyAnimatedBrush", myAnimatedBrush);
Storyboard.SetTargetName(Animation, "MyAnimatedBrush");
// Set the property the animation is to affect
Storyboard.SetTargetProperty(
Animation, new PropertyPath(SolidColorBrush.ColorProperty));
// Set the time the animation is to begin
Animation.BeginTime = TimeSpan.FromSeconds(delay);
// Set the duration of the animation
Animation.Duration = new Duration(TimeSpan.FromSeconds(duration));
// Set the target of the animation
Storyboard.SetTarget(Animation, control);
// Create a new storyboard
Storyboard stb = new Storyboard();
// Add the animation to the storyboard
stb.Children.Add(Animation);
// Set return variable to the new storyboard
return stb;
}
public static Storyboard FadeInFrameworkElement(FrameworkElement frameworkElement, Double duration, Double delay)
{
// Create the animation
DoubleAnimation Animation = new DoubleAnimation();
// Set the values that the animation starts and ends with
Animation.From = 0.3;
Animation.To = 1;
// Set the time the animation is to begin
Animation.BeginTime = TimeSpan.FromSeconds(delay);
// Set the duration of the animation
Animation.Duration = new Duration(TimeSpan.FromSeconds(duration));
// Set the target of the animation
Storyboard.SetTarget(Animation, frameworkElement);
// Set the property the animation is to affect
Storyboard.SetTargetProperty(Animation, new PropertyPath(FrameworkElement.OpacityProperty));
// Create a new storyboard
Storyboard stb = new Storyboard();
// Add the animation to the storyboard
stb.Children.Add(Animation);
// Set return variable to the new storyboard
return stb;
}
public static Storyboard FadeOutFrameworkElement(FrameworkElement frameworkElement, Double duration, Double delay)
{
// Create the animation
DoubleAnimation FadeOutAnimation = new DoubleAnimation();
// Set the values that the animation starts and ends with
FadeOutAnimation.From = 1;
FadeOutAnimation.To = 0.3;
// Set the time the animation is to begin
FadeOutAnimation.BeginTime = TimeSpan.FromSeconds(delay);
// Set the duration of the animation
FadeOutAnimation.Duration = new Duration(TimeSpan.FromSeconds(duration));
// Set the target of the animation
Storyboard.SetTarget(FadeOutAnimation, frameworkElement);
// Set the property the animation is to affect
Storyboard.SetTargetProperty(FadeOutAnimation, new PropertyPath(FrameworkElement.OpacityProperty));
// Create a new storyboard
Storyboard FadeInTBSB = new Storyboard();
// Add the animation to the storyboard
FadeInTBSB.Children.Add(FadeOutAnimation);
// Set return variable to the new storyboard
return FadeInTBSB;
} }
然后,定义扩展的控件,比如EffectButton,是从Button继承的
比如要在鼠标进入时增加效果。
我们可以这样写:
void Effect_MouseEnter(object sender, MouseEventArgs e)
{
EffectControlHelper.EffectMouseEnter(sender, EffectType, EffectRadius);
}
例如:
xmlns:efcs="clr-namespace:EffectControls;assembly=EffectControlsLibrary"
使用方法:
例1:
<efcs:EffectButton EffectType="DropShadowEffect"
Content="DropShadowEffect" >
</efcs:EffectButton>
例2:
<efcs:EffectTextBox EffectType="DropShadowEffect" EffectColor="Red" EffectRadius="10" EffectDirection="30" EffectOpacity="0.6" EffectShadowDepth="5"
Text="CustomDropShadow" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="125" >
</efcs:EffectTextBox>
效果
正常状态:
例1:
<efcs:EffectCheckBox EffectType="BlurEffect" EffectRadius="2"
Content="BlurEffect" >
</efcs:EffectCheckBox>
例2:
<efcs:EffectLabel EffectType="BlurEffect" EffectRadius="2"
Content="BlurEffect" >
</efcs:EffectLabel>
效果:
正常状态:
鼠标经过:
其他效果也是类似的,请看帮助文档。
源代码下载:
Open Source Web Address: http://EffectControls.codeplex.com/
Studio Address: http://www.CrmWin.com
作为开发人员我一直存在2个问题,相信很多人也有这样的问题:
1、 懒惰,为了实现一些Silverlight特殊效果要写烦乱的XAML语句,这太可怕了;
2、 因为不是设计人员,很难做出一些非常美观的界面
于是,我就写了EffectControls控件集,目的是用最短的时间和最短的代码实现很酷的控件效果。开放源码,欢迎您的使用和改进,如果有好玩的特效,请一定要通知我。
View Code public static Storyboard JumpUpFrameworkElement(FrameworkElement frameworkElement, Double duration, Double delay, double height)
{
// Create the animation
DoubleAnimation AnimationUp = new DoubleAnimation();
// Set the values that the animation starts and ends with
AnimationUp.From = 0;
AnimationUp.To = height;
if (height < 23)
{
AnimationUp.To = 23;
}
// Set the time the animation is to begin
AnimationUp.BeginTime = TimeSpan.FromSeconds(delay);
// Set the duration of the animation
AnimationUp.Duration = new Duration(TimeSpan.FromSeconds(duration));
// Set the target of the animation
Storyboard.SetTarget(AnimationUp, frameworkElement);
// Set the property the animation is to affect
Storyboard.SetTargetProperty(AnimationUp, new PropertyPath(FrameworkElement.HeightProperty));
// Create a new storyboard
Storyboard stb = new Storyboard();
// Add the animation to the storyboard
stb.Children.Add(AnimationUp);
// Set return variable to the new storyboard
return stb;
}
public static Storyboard JumpLeftFrameworkElement(FrameworkElement frameworkElement, Double duration, Double delay, double width)
{
// Create the animation
DoubleAnimation AnimationUp = new DoubleAnimation();
// Set the values that the animation starts and ends with
AnimationUp.From = 0;
AnimationUp.To = width;
if (width < 150)
{
AnimationUp.To = 150;
}
// Set the time the animation is to begin
AnimationUp.BeginTime = TimeSpan.FromSeconds(delay);
// Set the duration of the animation
AnimationUp.Duration = new Duration(TimeSpan.FromSeconds(duration));
// Set the target of the animation
Storyboard.SetTarget(AnimationUp, frameworkElement);
// Set the property the animation is to affect
Storyboard.SetTargetProperty(AnimationUp, new PropertyPath(FrameworkElement.WidthProperty));
// Create a new storyboard
Storyboard stb = new Storyboard();
// Add the animation to the storyboard
stb.Children.Add(AnimationUp);
// Set return variable to the new storyboard
return stb;
}
public static Storyboard ColorChange(Control control, Double duration, Double delay, Color from, Color to,string property)
{
System.Windows.Media.Animation.ColorAnimation Animation = new System.Windows.Media.Animation.ColorAnimation();
Animation.From = from;
Animation.To = to;
SolidColorBrush myAnimatedBrush = new SolidColorBrush();
myAnimatedBrush.Color = from;
if (property == "Background")
{
(control as Button).Background = myAnimatedBrush;
}
if (property == "Foreground")
{
(control as Button).Foreground = myAnimatedBrush;
}
//page.RegisterName("MyAnimatedBrush", myAnimatedBrush);
Storyboard.SetTargetName(Animation, "MyAnimatedBrush");
// Set the property the animation is to affect
Storyboard.SetTargetProperty(
Animation, new PropertyPath(SolidColorBrush.ColorProperty));
// Set the time the animation is to begin
Animation.BeginTime = TimeSpan.FromSeconds(delay);
// Set the duration of the animation
Animation.Duration = new Duration(TimeSpan.FromSeconds(duration));
// Set the target of the animation
Storyboard.SetTarget(Animation, control);
// Create a new storyboard
Storyboard stb = new Storyboard();
// Add the animation to the storyboard
stb.Children.Add(Animation);
// Set return variable to the new storyboard
return stb;
}
public static Storyboard FadeInFrameworkElement(FrameworkElement frameworkElement, Double duration, Double delay)
{
// Create the animation
DoubleAnimation Animation = new DoubleAnimation();
// Set the values that the animation starts and ends with
Animation.From = 0.3;
Animation.To = 1;
// Set the time the animation is to begin
Animation.BeginTime = TimeSpan.FromSeconds(delay);
// Set the duration of the animation
Animation.Duration = new Duration(TimeSpan.FromSeconds(duration));
// Set the target of the animation
Storyboard.SetTarget(Animation, frameworkElement);
// Set the property the animation is to affect
Storyboard.SetTargetProperty(Animation, new PropertyPath(FrameworkElement.OpacityProperty));
// Create a new storyboard
Storyboard stb = new Storyboard();
// Add the animation to the storyboard
stb.Children.Add(Animation);
// Set return variable to the new storyboard
return stb;
}
public static Storyboard FadeOutFrameworkElement(FrameworkElement frameworkElement, Double duration, Double delay)
{
// Create the animation
DoubleAnimation FadeOutAnimation = new DoubleAnimation();
// Set the values that the animation starts and ends with
FadeOutAnimation.From = 1;
FadeOutAnimation.To = 0.3;
// Set the time the animation is to begin
FadeOutAnimation.BeginTime = TimeSpan.FromSeconds(delay);
// Set the duration of the animation
FadeOutAnimation.Duration = new Duration(TimeSpan.FromSeconds(duration));
// Set the target of the animation
Storyboard.SetTarget(FadeOutAnimation, frameworkElement);
// Set the property the animation is to affect
Storyboard.SetTargetProperty(FadeOutAnimation, new PropertyPath(FrameworkElement.OpacityProperty));
// Create a new storyboard
Storyboard FadeInTBSB = new Storyboard();
// Add the animation to the storyboard
FadeInTBSB.Children.Add(FadeOutAnimation);
// Set return variable to the new storyboard
return FadeInTBSB;
} }
然后,定义扩展的控件,比如EffectButton,是从Button继承的
比如要在鼠标进入时增加效果。
我们可以这样写:
void Effect_MouseEnter(object sender, MouseEventArgs e)
{
EffectControlHelper.EffectMouseEnter(sender, EffectType, EffectRadius);
}
使用帮助
首先要在页面中加入引用:例如:
xmlns:efcs="clr-namespace:EffectControls;assembly=EffectControlsLibrary"
DropShadowEffect
用于控件阴影的显示使用方法:
例1:
<efcs:EffectButton EffectType="DropShadowEffect"
Content="DropShadowEffect" >
</efcs:EffectButton>
例2:
<efcs:EffectTextBox EffectType="DropShadowEffect" EffectColor="Red" EffectRadius="10" EffectDirection="30" EffectOpacity="0.6" EffectShadowDepth="5"
Text="CustomDropShadow" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="125" >
</efcs:EffectTextBox>
效果
正常状态:
BlurEffect
使用方法:例1:
<efcs:EffectCheckBox EffectType="BlurEffect" EffectRadius="2"
Content="BlurEffect" >
</efcs:EffectCheckBox>
例2:
<efcs:EffectLabel EffectType="BlurEffect" EffectRadius="2"
Content="BlurEffect" >
</efcs:EffectLabel>
效果:
正常状态:
鼠标经过:
其他效果也是类似的,请看帮助文档。
源代码下载:
Open Source Web Address: http://EffectControls.codeplex.com/
Studio Address: http://www.CrmWin.com
相关文章推荐
- 如何编写Silverlight动画效果控件
- Android的一段常用动画效果代码(如何让点击的图片控件加速飞入到指定位置)
- Android的一段常用动画效果代码(如何让点击的图片控件加速飞入到指定位置)
- Silverlight代码编写对控件的PlaneProjection.RotationY属性控制动画
- Silverlight MMORPG网页游戏开发课程[一期] 第八课:场景之切换与动画效果
- animations 之 对控件设置动画效果
- 如何写一个点击view带动画的下滑展开显示隐藏内容的控件
- 控件旋转动画效果
- 如何添加WPF,SilverLight控件到ToolBox
- 编写的activeX 控件如何被IE调用?
- iOS仿今日头条频道选择、仿微信项目、音频控件、多种动画效果等源码
- Android动画效果 translate、scale、alpha、rotate 切换Activity动画 控件位置调整
- 论坛源码推荐(6月19日):用Swift编写的Twitter启动动画 iOS 7图片和视频管理控件
- 如何实现一个原质化菜单项或按钮HTML5动画效果
- 给控件添加炫酷的动画效果
- wince如何去掉APP窗口的动画显示效果
- 如何在Silverlight 控件中设置默认.xaml 页面
- android所有控件、ProgressBar 、Android 动画效果、SQLite、四大组件、Android多媒体
- Silverlight中实现类似Telerik的TileView控件效果
- 如何去掉滑动控件(比如ScrollView、ListView、ViewPager等)滑动到最顶部或最底部时出现的阴影效果。