控件 UI RenderTransform, Projection, Clip, UseLa
介紹
重新想象 Windows 8 Store Apps 之 控件 UI
RenderTransform - 變換(用於做位移,旋轉,縮放,扭曲等變換)
Projection - 映射
Clip - 剪裁並顯示 UIElement 的指定區域
UseLayoutRounding - 是否使用完整像素布局
示例
1、演示 RenderTransform 的應用
Controls/UI/RenderTransform.xaml
<Page x:Class="XamlDemo.Controls.UI.RenderTransform" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Controls.UI" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <Grid Margin="120 0 0 0"> <!-- RenderTransform - 變換(用於做位移,旋轉,縮放,扭曲等變換) --> <Grid HorizontalAlignment="Left" VerticalAlignment="Top"> <Rectangle Width="200" Height="100" StrokeDashArray="3,1" Stroke="Blue" StrokeThickness="3" /> <Rectangle Width="200" Height="100" Fill="Yellow" Stroke="Red" StrokeThickness="3" Opacity="0.3"> <Rectangle.RenderTransform> <!--RotateTransform - 旋轉變換(順時針)--> <!-- Angle - 旋轉角度。默認值 0 CenterX - 旋轉中心點的 X 軸坐標。默認值 0 CenterY - 旋轉中心點的 Y 軸坐標。默認值 0 --> <RotateTransform Angle="15" CenterX="100" CenterY="50" /> </Rectangle.RenderTransform> </Rectangle> </Grid> <Grid Margin="400 0 0 0" HorizontalAlignment="Left" VerticalAlignment="Top"> <Rectangle Width="200" Height="100" StrokeDashArray="3,1" Stroke="Blue" StrokeThickness="3" /> <!-- RenderTransformOrigin - 位置變換的中心點 --> <Rectangle Width="200" Height="100" Fill="Yellow" Stroke="Red" StrokeThickness="3" Opacity="0.3" RenderTransformOrigin="0.5,0.5"> <Rectangle.RenderTransform> <RotateTransform Angle="15" /> </Rectangle.RenderTransform> </Rectangle> </Grid> <Grid Margin="800 0 0 0" HorizontalAlignment="Left" VerticalAlignment="Top"> <Rectangle Width="200" Height="100" StrokeDashArray="3,1" Stroke="Blue" StrokeThickness="3" /> <Rectangle Width="200" Height="100" Fill="Yellow" Stroke="Red" StrokeThickness="3" Opacity="0.3"> <Rectangle.RenderTransform> <!--TranslateTransform - 平移變換--> <!-- X - 水平方向上移動的距離。默認值 0 Y - 垂直方向上移動的距離。默認值 0 --> <TranslateTransform X="100" Y="10" /> </Rectangle.RenderTransform> </Rectangle> </Grid> <Grid Margin="0 200 0 0" HorizontalAlignment="Left" VerticalAlignment="Top"> <Rectangle Width="200" Height="100" StrokeDashArray="3,1" Stroke="Blue" StrokeThickness="3" /> <Rectangle Width="200" Height="100" Fill="Yellow" Stroke="Red" StrokeThickness="3" Opacity="0.3"> <Rectangle.RenderTransform> <!--ScaleTransform - 縮放變換--> <!-- ScaleX - X 軸方向上縮放的倍數。默認值 1 ScaleY - Y 軸方向上縮放的倍數。默認值 1 CenterX - 縮放中心點的 X 軸坐標。默認值 0 CenterY - 縮放中心點的 Y 軸坐標。默認值 0 --> <ScaleTransform ScaleX="1.1" ScaleY="1.3" CenterX="100" CenterY="50" /> </Rectangle.RenderTransform> </Rectangle> </Grid> <Grid Margin="400 200 0 0" HorizontalAlignment="Left" VerticalAlignment="Top"> <Rectangle Width="200" Height="100" StrokeDashArray="3,1" Stroke="Blue" StrokeThickness="3" /> <Rectangle Width="200" Height="100" Fill="Yellow" Stroke="Red" StrokeThickness="3" Opacity="0.3"> <Rectangle.RenderTransform> <!--SkewTransform - 扭曲變換(典型應用:在 二維 對象中模擬 三維 深度)--> <!-- CenterX - 扭曲中心點的 X 軸坐標。默認值 0 CenterY - 扭曲中心點的 Y 軸坐標。默認值 0 AngleX - X 軸扭曲角度,Y 軸繞原點旋轉(逆時針)。CenterX 對此值所產生的呈現結果沒有影響。默認值 0 AngleY - Y 軸扭曲角度,X 軸繞原點旋轉(順時針)。CenterY 對此值所產生的呈現結果沒有影響。默認值 0 --> <SkewTransform AngleX="30" AngleY="5" CenterX="0" CenterY="0" /> </Rectangle.RenderTransform> </Rectangle> </Grid> <Grid Margin="800 200 0 0" HorizontalAlignment="Left" VerticalAlignment="Top"> <Rectangle Width="200" Height="100" StrokeDashArray="3,1" Stroke="Blue" StrokeThickness="3" /> <Rectangle Width="200" Height="100" Fill="Yellow" Stroke="Red" StrokeThickness="3" Opacity="0.3"> <Rectangle.RenderTransform> <!--TransformGroup - 多個 Transform 組成的復合變換--> <TransformGroup> <TranslateTransform X="100" Y="10" /> <RotateTransform Angle="15" CenterX="100" CenterY="50" /> </TransformGroup> </Rectangle.RenderTransform> </Rectangle> </Grid> <!-- CompositeTransform - 將多種變換方式合而為一 CenterX - 變換中心點的 X 坐標 CenterY - 變換中心點的 Y 坐標 Rotation - 順時針旋轉角度 ScaleX - 沿 X 軸方向上的縮放比例 ScaleY - 沿 Y 軸方向上的縮放比例 SkewX - X 軸扭曲角度 SkewY - Y 軸扭曲角度 TranslateX - 沿 X 軸方向上的平移距離 TranslateY - 沿 Y 軸方向上的平移距離 --> <Grid Margin="0 400 0 0" HorizontalAlignment="Left" VerticalAlignment="Top"> <Rectangle Width="200" Height="100" StrokeDashArray="3,1" Stroke="Blue" StrokeThickness="3" /> <Rectangle Width="200" Height="100" Fill="Yellow" Stroke="Red" StrokeThickness="3" Opacity="0.3"> <Rectangle.RenderTransform> <CompositeTransform SkewX="30" Rotation="60" ScaleX="0.6" ScaleY="0.3" /> </Rectangle.RenderTransform> </Rectangle> </Grid> <!-- MatrixTransform - 仿射矩陣變換 |X| |M11(默認值 1) M21(默認值 0) 0| |Y| = |x y 1| * |M12(默認值 0) M22(默認值 1) 0| |1| |OffsetX(默認值 0) OffsetY(默認值 0) 1| X = x * M11 + y * M12 + OffsetX Y = x * M21 + y * M22 + OffsetY 利用 MatrixTransform 實現平移、旋轉、縮放、扭曲的 Demo 詳見 http://www.cnblogs.com/webabcd/archive/2008/11/03/1325150.html --> <Grid Margin="400 400 0 0" HorizontalAlignment="Left" VerticalAlignment="Top"> <Rectangle Width="200" Height="100" StrokeDashArray="3,1" Stroke="Blue" StrokeThickness="3" /> <Rectangle Width="200" Height="100" Fill="Yellow" Stroke="Red" StrokeThickness="3" Opacity="0.3"> <Rectangle.RenderTransform> <!-- m11, m12, m21, m22, offsetX, offsetY --> <MatrixTransform Matrix="1, 0.5, 0, 1, 30, 10" /> </Rectangle.RenderTransform> </Rectangle> </Grid> </Grid> </Grid> </Page>
2、演示 Projection 的應用
Controls/UI/Projection.xaml
<Page x:Class="XamlDemo.Controls.UI.Projection" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Controls.UI" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <Grid Margin="120 0 0 0"> <!-- Projection - 映射 PlaneProjection - 平面映射,使 UIElement 實現 3D 效果 RotationX, RotationY, RotationZ - 繞 X軸, Y軸, Z軸 旋轉的角度 CenterOfRotationX, CenterOfRotationY, CenterOfRotationZ - X軸, Y軸, Z軸 旋轉中心點的相對位置(CenterOfRotationX, CenterOfRotationY 默認值為 0.5 , CenterOfRotationZ 默認值 為 0) GlobalOffsetX, GlobalOffsetY, GlobalOffsetZ - 沿 X軸, Y軸, Z軸 的偏移量,此 3 個方向與屏幕的 3 個方向相同 LocalOffsetX, LocalOffsetY, LocalOffsetZ - 沿 X軸, Y軸, Z軸 的偏移量,此 3 個方向與相關的 UIElement 當前的 3 個方向相同 Matrix3DProjection - 矩陣映射 --> <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Rectangle Width="200" Height="100" StrokeDashArray="3,1" Stroke="Blue" StrokeThickness="3" /> <Rectangle Width="200" Height="100" Fill="Yellow" Stroke="Red" StrokeThickness="3" Opacity="0.3"> <Rectangle.Projection> <PlaneProjection x:Name="planeProjection" /> </Rectangle.Projection> </Rectangle> </Grid> <StackPanel> <TextBlock FontSize="14.667" Text="RotationX:" Margin="0 10 0 0" /> <Slider Width="150" Minimum="0" Maximum="360" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 -10 0 0" Value="{Binding ElementName=planeProjection, Path=RotationX, Mode=TwoWay}" /> <TextBlock FontSize="14.667" Text="RotationY:" Margin="0 -10 0 0" /> <Slider Width="150" Minimum="0" Maximum="360" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 -10 0 0" Value="{Binding ElementName=planeProjection, Path=RotationY, Mode=TwoWay}" /> <TextBlock FontSize="14.667" Text="RotationZ:" Margin="0 -10 0 0" /> <Slider Width="150" Minimum="0" Maximum="360" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 -10 0 0" Value="{Binding ElementName=planeProjection, Path=RotationZ, Mode=TwoWay}" /> <TextBlock FontSize="14.667" Text="LocalOffsetX:" Margin="0 -10 0 0" /> <Slider Width="150" Minimum="-150" Maximum="150" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 -10 0 0" Value="{Binding ElementName=planeProjection, Path=LocalOffsetX, Mode=TwoWay}" /> <TextBlock FontSize="14.667" Text="LocalOffsetY:" Margin="0 -10 0 0" /> <Slider Width="150" Minimum="-150" Maximum="150" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 -10 0 0" Value="{Binding ElementName=planeProjection, Path=LocalOffsetY, Mode=TwoWay}" /> <TextBlock FontSize="14.667" Text="LocalOffsetZ:" Margin="0 -10 0 0" /> <Slider Width="150" Minimum="-150" Maximum="150" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 -10 0 0" Value="{Binding ElementName=planeProjection, Path=LocalOffsetZ, Mode=TwoWay}" /> <TextBlock FontSize="14.667" Text="GlobalOffsetX:" Margin="0 -10 0 0" /> <Slider Width="150" Minimum="-150" Maximum="150" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 -10 0 0" Value="{Binding ElementName=planeProjection, Path=GlobalOffsetX, Mode=TwoWay}" /> <TextBlock FontSize="14.667" Text="GlobalOffsetY:" Margin="0 -10 0 0" /> <Slider Width="150" Minimum="-150" Maximum="150" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 -10 0 0" Value="{Binding ElementName=planeProjection, Path=GlobalOffsetY, Mode=TwoWay}" /> <TextBlock FontSize="14.667" Text="GlobalOffsetZ:" Margin="0 -10 0 0" /> <Slider Width="150" Minimum="-150" Maximum="150" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 -10 0 0" Value="{Binding ElementName=planeProjection, Path=GlobalOffsetZ, Mode=TwoWay}" /> </StackPanel> </Grid> </Grid> </Page>
3、演示 Clip 的應用
Controls/UI/Clip.xaml
<Page x:Class="XamlDemo.Controls.UI.Clip" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Controls.UI" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <Grid HorizontalAlignment="Left" VerticalAlignment="Top"> <Rectangle Width="200" Height="100" Fill="Red" /> <Rectangle Width="200" Height="100" Fill="Green"> <!-- Clip - 剪裁並顯示 UIElement 的指定區域 注:win8 中只能通過 RectangleGeometry 剪裁 UIElement --> <Rectangle.Clip> <RectangleGeometry Rect="10 10 50 50" /> </Rectangle.Clip> </Rectangle> </Grid> </StackPanel> </Grid> </Page>
4、演示 UseLayoutRounding 的應用
Controls/UI/UseLayoutRounding.xaml
<Page x:Class="XamlDemo.Controls.UI.UseLayoutRounding" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Controls.UI" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <!-- UseLayoutRounding - 是否使用完整像素布局。默認值為 true 舉例: 、如果設置了Margin="1.6"(非完整像素) 、那麼UseLayoutRounding="true"時,則容器的外邊距不相等;UseLayoutRounding="false"時,則容器的外 邊距相等 --> <Grid Width="200" Height="200" HorizontalAlignment="Left" UseLayoutRounding="True"> <Border BorderBrush="#FF884909" Background="#FFFBECA3" BorderThickness="1"></Border> <Border BorderBrush="#FF884909" Background="#FFFBECA3" BorderThickness="1" Margin="1.6"></Border> </Grid> <Grid Width="200" Height="200" Margin="0 10 0 0" HorizontalAlignment="Left" UseLayoutRounding="False"> <Border BorderBrush="#FF884909" Background="#FFFBECA3" BorderThickness="1"></Border> <Border BorderBrush="#FF884909" Background="#FFFBECA3" BorderThickness="1" Margin="1.6"></Border> </Grid> </StackPanel> </Grid> </Page>
OK
[源碼下載]:http://files.cnblogs.com/webabcd/Windows8.rar