UWP Button添加圆角阴影(三)
Composition
DropShadow是CompositionAPI中的东西,使用Storyboard设置某个属性,就是频繁的触发put_xxx()方法,效率远远不如使用CompositionAnimation。
Composition对象的基类CompositionObject拥有一个属性叫ImplicitAnimations,可以通过他实现累死css的transition的效果,也就是对应属性修改的时候,平滑的过渡过去。
可以从DropShadowPanel的源代码中看到,DropShadow是设置在ShadowElement上的ChildVisual。
相关内容可以查阅将可视化层与 XAML 结合使用 - ElementCompositionPreview.SetElementChildVisual 方法。
而我们要做的,是把整个构造过程倒过来,通过VisualTreeHelper,从DropShadow中拿到ShadowElement,然后获取他的ChildVisual和Shadow,将ImplicitAnimations设置到Shadow上。
下面贴代码:
public static class DropShadowPanelHelper
{
public static bool GetIsTransitionEnable(DropShadowPanel obj)
{
return (bool)obj.GetValue(IsTransitionEnableProperty);
}
public static void SetIsTransitionEnable(DropShadowPanel obj, bool value)
{
obj.SetValue(IsTransitionEnableProperty, value);
}
public static readonly DependencyProperty IsTransitionEnableProperty =
DependencyProperty.RegisterAttached("IsTransitionEnable", typeof(bool), typeof(DropShadowPanelHelper), new PropertyMetadata(false, IsTransitionEnablePropertyChanged));
private static void IsTransitionEnablePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != e.OldValue)
{
if (d is DropShadowPanel sender)
{
//尝试获取ShadowElement,如果为空,可能是DropShadowPanel还没有ApplyTemplate,注册DropShadowPanel的Loaded事件,在Loaded中再获取一次。
var shadowElement = sender.FindDescendantByName("ShadowElement") as Border;
if (shadowElement != null)
{
SetImplicitAnimation(shadowElement, (bool)e.NewValue);
}
else
{
sender.Loaded += DropShadowPanel_Loaded;
}
}
}
}
private static void DropShadowPanel_Loaded(object sender, RoutedEventArgs e)
{
var dropShadowPanel = (DropShadowPanel)sender;
dropShadowPanel.Loaded -= DropShadowPanel_Loaded;
var shadowElement = dropShadowPanel.FindDescendantByName("ShadowElement") as Border;
if (shadowElement != null)
{
SetImplicitAnimation(shadowElement, GetIsTransitionEnable(dropShadowPanel));
}
}
private static void SetImplicitAnimation(FrameworkElement element, bool IsEnable)
{
if (ElementCompositionPreview.GetElementChildVisual(element) is SpriteVisual shadowVisual &&
shadowVisual.Shadow is DropShadow shadow)
{
if (IsEnable)
{
//获取合成器
var compositor = shadowVisual.Compositor;
//创建ImplicitAnimationCollection
var imp = compositor.CreateImplicitAnimationCollection();
//创建BlurRadius动画,注意不要忘记设置Duration和Target
var bluran = compositor.CreateScalarKeyFrameAnimation();
//插入一个表达式关键帧,帧在进度为1的时候,值是最终值
bluran.InsertExpressionKeyFrame(1f, "this.FinalValue");
bluran.Duration = TimeSpan.FromSeconds(0.2d);
bluran.Target = "BlurRadius";
//创建Offset动画
var offsetan = compositor.CreateVector3KeyFrameAnimation();
offsetan.InsertExpressionKeyFrame(1f, "this.FinalValue");
offsetan.Duration = TimeSpan.FromSeconds(0.2d);
offsetan.Target = "Offset";
//创建Opacity动画
var opacityan = compositor.CreateScalarKeyFrameAnimation();
opacityan.InsertExpressionKeyFrame(1f, "this.FinalValue");
opacityan.Duration = TimeSpan.FromSeconds(0.2d);
opacityan.Target = "Opacity";
//ImplicitAnimationCollection是IDictionary,每个子项都要是KeyFrame动画,子项的Key和动画的Target要一样。
ImplictAnimationCollection
imp[bluran.Target] = bluran;
imp[offsetan.Target] = offsetan;
imp[opacityan.Target] = opacityan;
//给shadow设置ImplicitAnimations
shadow.ImplicitAnimations = imp;
}
else
{
var imp = shadow.ImplicitAnimations;
shadow.ImplicitAnimations = null;
if (imp != null)
{
imp.Dispose();
imp = null;
}
}
}
}
}
表达式关键帧的关键字相关的内容可以查阅:ExpressionAnimation Class - Expression Keywords
最后的Xaml是这样的:
<Style TargetType="Button" x:Key="CornerRadiusShadowButtonStyle">
<Setter Property="Background" Value="#007acc" />
<Setter Property="Foreground" Value="White" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Padding" Value="20,10,20,10" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
<Setter Property="UseSystemFocusVisuals" Value="True" />
<Setter Property="FocusVisualMargin" Value="-3" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="RootGrid">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="PointerOver">
<VisualState.Setters>
<Setter Target="Shadow.OffsetY" Value="2" />
<Setter Target="Shadow.BlurRadius" Value="8" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="Shadow" Storyboard.TargetProperty="OffsetY" To="3" Duration="0" />
<DoubleAnimation Storyboard.TargetName="Shadow" Storyboard.TargetProperty="BlurRadius" To="12" Duration="0" />
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Background" Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundDisabled}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<control:DropShadowPanel x:Name="Shadow"
xmlns:control="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:helper="using:TestApp1.Helpers"
HorizontalContentAlignment="Stretch"
helper:DropShadowPanelHelper.IsTransitionEnable="True"
BlurRadius="5" OffsetX="1" OffsetY="1" Color="Black">
<Rectangle x:Name="Background" Fill="{TemplateBinding Background}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RadiusX="5" RadiusY="5" />
</control:DropShadowPanel>
<ContentPresenter x:Name="ContentPresenter"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Content="{TemplateBinding Content}"
ContentTransitions="{TemplateBinding ContentTransitions}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Padding="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
AutomationProperties.AccessibilityView="Raw" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
启用方式就是中的xmlns:helper="using:TestApp1.Helpers" helper:DropShadowPanelHelper.IsTransitionEnable="True"
VisualState中有两种写法,第一种是写在Setter中,优点是写的少,缺点是不能控制起始时间。
第二种是写在Storyboard中,Duration一定要是0,因为真正的动画我们定义在ImplicitAnimation中了,这里的Animation只是用来触发值修改,不需要插值。
最终效果有点小掉帧,是DropShadow的问题,17763中的ThemeShadow应该会有改善。。。
UWP Button添加圆角阴影(三)的更多相关文章
- UWP Button添加圆角阴影(二)
原文:UWP Button添加圆角阴影(二) 阴影 对于阴影呢,WindowsCommunityToolkit中已经有封装好的DropShadowPanel啦,只要引用Microsoft.Toolki ...
- UWP Button添加圆角阴影(一)
原文:UWP Button添加圆角阴影(一) 众所周知,17763之前的UWP控件,大部分是没有圆角属性的:而阴影也只有17763中的ThemeShadow可以直接在xaml中使用,之前的版本只能用D ...
- 为input输入框添加圆角并去除阴影
<input type="text" name="bianhao" value="" placeholder="请输入商品编 ...
- iOS 高效添加圆角效果实战讲解
圆角(RounderCorner)是一种很常见的视图效果,相比于直角,它更加柔和优美,易于接受.但很多人并不清楚如何设置圆角的正确方式和原理.设置圆角会带来一定的性能损耗,如何提高性能是另一个需要重点 ...
- iOS开发-添加圆角效果高效实现
圆角(RounderCorner)是一种很常见的视图效果,相比于直角,它更加柔和优美,易于接受.但很多人并不清楚如何设置圆角的正确方式和原理.设置圆角会带来一定的性能损耗,如何提高性能是另一个需要重点 ...
- {django模型层(二)多表操作}一 创建模型 二 添加表记录 三 基于对象的跨表查询 四 基于双下划线的跨表查询 五 聚合查询、分组查询、F查询和Q查询
Django基础五之django模型层(二)多表操作 本节目录 一 创建模型 二 添加表记录 三 基于对象的跨表查询 四 基于双下划线的跨表查询 五 聚合查询.分组查询.F查询和Q查询 六 xxx 七 ...
- CSS3圆角,阴影,透明
CSS实现圆角,阴影,透明的方法很多,传统的方法都比较复杂,用CSS3就方便很多了,虽然现在各浏览器对CSS3的支持还不是很好,但不久的将来CSS3就会普及. 1.圆角 CSS3实现圆角有两种方法. ...
- HackTwelve 为背景添加圆角边框
1.概要: ShapeDrawable是一个为UI控件添加特效的好工具.这个技巧适用于那些可以添加背景的控件 2.添加圆角边框其实就是添加的背景那里不是直接添加图片,而是添加一个XML文件即可 ...
- (二)AS给button添加点击事件
三种方法给Button添加点击事件 (一)通过button的id,添加继承View.OnClickListener的监听实现 <Button android:id="@+id/btn_ ...
随机推荐
- android DatagramSocket send 发送数据出错
安卓4.0以后好像不能在主线程里面使用 socket 所以不管是发送数据还是接收数据需要新开一个了线程: 以下代码是我点击发送是代码: new Thread(new Runnable() { @Ove ...
- js中 xpath 使用
一.使用: 非IE浏览器,使用 document.evaluate var result = document.evaluate("//a[@href]", document, n ...
- tomcat探索
双击apache-tomcat-7.0.64.exe,一路默认点. 静态页路径 C:\Program Files\Apache Software Foundation\Tomcat 7.0\webap ...
- SQL判断如果一列值为null则取另一列值代替 isnull()
[chClientCode] ,[nvcClientName] ,[chRegionCode] ,isnull(chUltimateHeadClientCode,[chClientCode]) as ...
- 解疑网络监控卡壳 视觉体验400ms延时
http://security.zol.com.cn/413/4130220.html 我的眼睛可以轻松判断出400ms延时误差,你可以吗?很多用户都反映手机移动监控.PC远程监控视频会卡顿,抛开设备 ...
- CSS-弹性布局-动画-过渡
1.弹性布局 1.项目的属性 该组属性只能设置在某项目元素上,只控制一个项目,是不影响容器以及其他项目的效果. 1.order 作用:定义项目的排列顺序,值越小,越靠近起点,默认值是0 取值:整数数字 ...
- MyBatis 实现新增
MyBatis实现新增 1.概念学习:(角度不同) 1.1 功能:从应用程序角度出发,软件具有哪些功能 1.2 业务:完成功能时的逻辑,对应Service中一个方法 1.3 事务:从数据库角度出发,完 ...
- JAVA折腾微信公众平台(Token验证)[转]
JAVA折腾微信公众平台(Token验证) BAE的JAVA还在内测的时候,抱着好奇的态度发邮件申请了内测权限,当时折腾了一天,然后就没折腾了.现在BAE的JAVA都已经正式开放使用了,我又蛋疼的想写 ...
- Java的背景、影响及前景
一.背景 詹姆斯·高斯林出生于加拿大,是一位计算机编程天才.在卡内基·梅隆大学攻读计算机博士学位时,他编写了多处理器版本的Unix操作系统,是JAVA编程语言的创始人. 高斯林生于1955年,已婚,育 ...
- Tomcat架构解析(一)-----Tomcat总体架构
Tomcat是非常常用的应用服务器,了解Tomcat的总体架构以及实现细节,对于理解整个java web也是有非常大的帮助. 一.Server 1.最简单的服务器结构 最简单的服务器结构如图所示: ...