代码创建 WPF 旋转动画_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 代码创建 WPF 旋转动画

代码创建 WPF 旋转动画

 2013/7/14 17:16:16  幕三少  博客园  我要评论(0)
  • 摘要:先建立一个button<ButtonWidth="80"Height="60"Content="旋转"Name="trans"Click="trans_Click"Style="{x:Null}"/>方法一:publicvoidTransform1(){RotateTransformrtf=newRotateTransform();trans.RenderTransform=rtf;DoubleAnimationdbAscending=newDoubleAnimation(0
  • 标签:创建 代码

先建立一个button

        <Button  Width="80" Height="60" Content="旋转" Name="trans" Click="trans_Click"  Style="{x:Null}"/>

方法一:

 public void Transform1()
        {
            RotateTransform rtf = new RotateTransform();
            trans.RenderTransform = rtf;
            DoubleAnimation dbAscending = new DoubleAnimation(0, 360, new Duration

            (TimeSpan.FromSeconds(1)));
            dbAscending.RepeatBehavior = RepeatBehavior.Forever;
            rtf.BeginAnimation(RotateTransform.AngleProperty, dbAscending);
        }

方法二:

public void Transform2()
        {
            RotateTransform rtf = new RotateTransform();
            trans.RenderTransform = rtf;
            DoubleAnimation dbAscending = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(1)));
            Storyboard storyboard = new Storyboard();
            dbAscending.RepeatBehavior = RepeatBehavior.Forever;
            storyboard.Children.Add(dbAscending);
            Storyboard.SetTarget(dbAscending, trans);
            Storyboard.SetTargetProperty(dbAscending, new PropertyPath("RenderTransform.Angle"));
            storyboard.Begin();
        }

效果如下:

截图不怎么能看出效果,这两种方法是按某个角进行旋转的。

方法三:Xaml动画

<Window x:Class="Oland.HSS.InHospital.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Height="300" Width="300">
    <Window.Resources>
        <Storyboard x:Key="LoadHeadStoryboard" >
            <DoubleAnimationUsingKeyFrames BeginTime="0:0:0" Duration="0:0:5"
                                           RepeatBehavior="Forever"
                                           Storyboard.TargetName="DesignerHead"
                                           Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
                <SplineDoubleKeyFrame   Value="1"/>
                <SplineDoubleKeyFrame    Value="-1"/>
                <SplineDoubleKeyFrame    Value="1"/>
            </DoubleAnimationUsingKeyFrames>

        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource LoadHeadStoryboard}"/>
        </EventTrigger>
    </Window.Triggers>
    <Grid>
        <Button  Content="旋转" x:Name="DesignerHead"  Height="40" Width="60" RenderTransformOrigin="0.4,0.5"  Style="{x:Null}">
            <Button.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleX="1" ScaleY="1"></ScaleTransform>
                </TransformGroup>
            </Button.RenderTransform>
        </Button>
    </Grid>
</Window>

可设置是延X中线或者是Y中线旋转,上边那种是按左上角旋转。

发表评论
用户名: 匿名