当前位置:网站首页>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();
}
边栏推荐
猜你喜欢
How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?
零知识证明——zkSNARK证明体系
KubeSphere简介,功能介绍,优势,架构说明及应用场景
面试官:JVM运行时数据区包含哪几部分?作用是啥?
JS手写JSON.stringify() (面试)
Zero-knowledge proof - zkSNARK proof system
二叉树是否对称
【TypeScript】深入学习TypeScript枚举
QT(41)-多线程-QTThread-同步QSemaphore-互斥QMutex
简单理解 JS 事件循环
随机推荐
MySQL字段类型
vehemently condemn
How to promote the implementation of rural revitalization
使用 Chrome 开发者工具 coverage 功能分析 web 应用的渲染阻止资源的执行分布情况
Ant Group's time series database CeresDB is officially open source
How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?
Tear down the underlying mechanism of the five JOINs of SparkSQL
Aura clock chip generation configuration file script
5G NR 笔记记录
Elastic Search 根据匹配分和热度分排序
零知识证明——zkSNARK证明体系
SAP UI5 确保控件 id 全局唯一的实现方法
idea源码无法下载
格密码入门
零知识证明笔记——私密交易,pederson,区间证明,所有权证明
hash和history路由的区别
Zero-knowledge proof - zkSNARK proof system
Client Side Cache 和 Server Side Cache 的区别
面试官:索引为什么会失效?
如果是测试 axi dma抓数的话 看这里