当前位置:网站首页>Win10 uwp use ScaleTransform magnify an element
Win10 uwp use ScaleTransform magnify an element
2022-08-04 20:05:00 【Lin Dexi】
本文告诉大家如何通过 ScaleTransform 放大元素 Zoom in an element method has many a,通过 ScaleTransform Amplification is halal
在 UWP 中 ScaleTransform 是属于 RenderTransform 的内容,所有的 UIElement 都有 RenderTransform 属性,By setting this attribute can be done at run time to modify the rendering elements
As a new simple UWP 程序,Put a button inside
<Button VerticalAlignment="Center" HorizontalAlignment="Center" Content="Click" Click="Button_OnClick">
</Button>
If you want to amplify the button shows two times,简单的方法是使用 ScaleTransform Set two directions amplification
修改一下代码
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<Button Margin="10,10,10,10" Content="Enlarge button before">
</Button>
<Button VerticalAlignment="Center" HorizontalAlignment="Center" Content="放大的按钮">
<Button.RenderTransform>
<ScaleTransform x:Name="ScaleTransform" ScaleX="2" ScaleY="2"></ScaleTransform>
</Button.RenderTransform>
</Button>
</StackPanel>
代码请看 github
从上面看到 ScaleTransform Support the two directions of larger,Can set two directions to different values
其实 ScaleTransform Can also set the zoom center,From that point as the center amplification
The default is not set from (0,0) The point is the upper left corner of the PM amplification,After amplification will keep unchanged the coordinates of the upper left corner
Most of the time will be used to enlarge from center,Enlarge the center of the need to set the zoom element from the center,请看代码,When click on the button to zoom in,Center is the center button
<Button VerticalAlignment="Center" HorizontalAlignment="Center" Content="放大的按钮" Click="Button_OnClick">
<Button.RenderTransform>
<ScaleTransform x:Name="ScaleTransform" ScaleX="1" ScaleY="1"></ScaleTransform>
</Button.RenderTransform>
</Button>
private void Button_OnClick(object sender, RoutedEventArgs e)
{
var button = (Button) sender;
ScaleTransform.CenterX = button.ActualWidth / 2;
ScaleTransform.CenterY = button.ActualHeight / 2;
ScaleTransform.ScaleX = 1.5;
ScaleTransform.ScaleY = 1.5;
}
Don't set to contrast the enlarge from the top left corner
private void Button_OnClick(object sender, RoutedEventArgs e)
{
ScaleTransform.ScaleX = 1.5;
ScaleTransform.ScaleY = 1.5;
}
Then how to do scaling,
I use a little advantage under the method to do animation,Friend, please don't learn the writing
private void Button_OnClick(object sender, RoutedEventArgs e)
{
Task.Run(async () =>
{
while (true)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
ScaleTransform.ScaleX++;
ScaleTransform.ScaleY++;
});
await Task.Delay(100);
}
});
}
I open a thread,Use an infinite loop,在里面使用 Task.Delay 做延迟
因为在 UWP Is not the main thread is unable to access the main thread element,所以就需要通过 Dispatcher.RunAsync Make the code in the main thread running
So a little halal method is how to do it?通过 xaml Write animation is a good method
<Button VerticalAlignment="Center" HorizontalAlignment="Center" Content="放大的按钮" Click="Button_OnClick">
<Button.Resources>
<Storyboard x:Key="Storyboard">
<DoubleAnimation Storyboard.TargetName="ScaleTransform" Storyboard.TargetProperty="ScaleX" To="1.5" Duration="0:0:1"></DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="ScaleTransform" Storyboard.TargetProperty="ScaleY" To="1.5" Duration="0:0:1"></DoubleAnimation>
</Storyboard>
</Button.Resources>
<Button.RenderTransform>
<ScaleTransform x:Name="ScaleTransform" ScaleX="1" ScaleY="1"></ScaleTransform>
</Button.RenderTransform>
</Button>
At this time by clicking on the button to get the resources,运行动画
private void Button_OnClick(object sender, RoutedEventArgs e)
{
var button = (Button) sender;
var storyboard = (Storyboard) button.Resources["Storyboard"];
storyboard.Begin();
}
边栏推荐
- 二叉树是否对称
- "WAIC 2022 · hackers marathon" two ants wealth competition invited you to fight!
- vs Code runs a local web server
- 面试官:JVM运行时数据区包含哪几部分?作用是啥?
- 《支付宝体验设计精髓》一书,跟测试相关性知识记录
- C#移动OA办公系统源码(基于微信企业号)
- IIC驱动OLED
- 电脑一键重装系统后连不上远程了?教你设置的方法
- How to monitor code cyclomatic complexity by refactoring indicators
- 带你了解数据分布式存储原理
猜你喜欢
How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?
刷题-洛谷-P1304 哥德巴赫猜想
PriorityQueue类的使用及底层原理
刷题-洛谷-P1179 数字统计
【着色器实现Glitch单项故障闪烁效果(与Television效果不同)_Shader效果第十四篇】
The book "The Essence of Alipay Experience Design", a record of knowledge related to testing
面试官:JVM运行时数据区包含哪几部分?作用是啥?
二叉树的遍历
刷题-洛谷-P1317 低洼地
Desthiobiotin-PEG4-Azide_脱硫生物素-叠氮化物 100mg
随机推荐
String中的hashcode缓存以及HashMap中String作key的好处
QT(41)-多线程-QTThread-同步QSemaphore-互斥QMutex
带你了解数据分布式存储原理
如何使用 jMeter Parallel Controller - 并行控制器以及一些常犯的错误
【有奖征文】秋招特训,打造你的专属产品体验
格密码入门
六月 致 -.-- -..- -
really time ntp service start command
node 的运行命令
JS new一个构造器发生了什么?从零手写一个new方法
电脑一键重装系统后连不上远程了?教你设置的方法
really time ntp服务启动命令
awk 统计平均 最大 最小值
C#弹出询问对话框
C#的Dictionary字典集合按照key键进行升序和降序排列
SAP 电商云 Accelerator 和 Spartacus UI 的工作机制差异
基于HDF的LED驱动程序开发(2)
MYSQL gets the table name and table comment of the database
手撕SparkSQL五大JOIN的底层机制
vs Code runs a local web server