windows phone开发学习--加载时的旋转动画
2013-01-18 17:12
399 查看
在进行windows phone开发时,有时候我们使用的是ProgressBar来表示程序正在进行载入操作,但其实也可以通过设计一个旋转的图片(类似于360杀毒软件)来表示正在进行加载。就像下面这样:
下面说说实现过程:
第一步:添加一个旋转png图片到工程中去
第二步:在MainPage,xaml中添加下面的动画效果
<phone:PhoneApplicationPage.Resources>
<Storyboard x:Name="SpinningAnimation">
<DoubleAnimation AutoReverse="False"
Duration="0:0:3"
From="0"
RepeatBehavior="Forever"
Storyboard.TargetName="SpinningRotateTransform"
Storyboard.TargetProperty="Angle"
To="360" />
</Storyboard>
</phone:PhoneApplicationPage.Resources>
第三步:把下面代码加入到MainPage.xaml中:
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Background="Red">
<Image
Margin="10,10"
Height="50"
Width="50"
Source="/Spinner.png"
Stretch="Uniform">
<Image.RenderTransform>
<RotateTransform x:Name="SpinningRotateTransform"
CenterX="25"
CenterY="25" />
</Image.RenderTransform>
</Image>
</StackPanel>
第四步:添加两个按钮来开始结束动画
<Button Content="Start Animation" Click="btnStart_Click"/>
<Button Content="Stop Animation" Click="btnStop_Click"/>
第五步:如果运行后看不到效果,一般情况下是旋转图片的颜色和页面的背景色一致,改变颜色以后就能看到了,这里可以使用一个不透明性的Mask,添加下面代码:
<Ellipse Fill="{StaticResource PhoneForegroundBrush}" Height="50" Width="50" Margin="10,10">
<Ellipse.OpacityMask>
<ImageBrush ImageSource="/Spinner.png" Stretch="Uniform"/>
</Ellipse.OpacityMask>
<Ellipse.RenderTransform>
<RotateTransform x:Name="SpinningRotateTransform"
CenterX="25"
CenterY="25" />
</Ellipse.RenderTransform>
</Ellipse>
最终全部代码如下:
MainPage.xaml:
<phone:PhoneApplicationPage
x:Class="SpinningProgressBar.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<Storyboard x:Name="SpinningAnimation">
<DoubleAnimation AutoReverse="False"
Duration="0:0:3"
From="0"
RepeatBehavior="Forever"
Storyboard.TargetName="SpinningRotateTransform"
Storyboard.TargetProperty="Angle"
To="360" />
</Storyboard>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" >
<Ellipse Fill="{StaticResource PhoneForegroundBrush}" Height="50" Width="50" Margin="10,10">
<Ellipse.OpacityMask>
<ImageBrush ImageSource="/Spinner.png" Stretch="Uniform"/>
</Ellipse.OpacityMask>
<Ellipse.RenderTransform>
<RotateTransform x:Name="SpinningRotateTransform"
CenterX="25"
CenterY="25" />
</Ellipse.RenderTransform>
</Ellipse>
<Button Content="Start Animation" Click="btnStart_Click"/>
<Button Content="Stop Animation" Click="btnStop_Click"/>
</StackPanel>
</Grid>
</phone:PhoneApplicationPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SpinningProgressBar.Resources;
namespace SpinningProgressBar
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void btnStart_Click(object sender, RoutedEventArgs e) { this.SpinningAnimation.Begin(); } private void btnStop_Click(object sender, RoutedEventArgs e) { this.SpinningAnimation.Stop(); }
}
}
源代码可以在这里下载:http://www.windowsphonegeek.com/upload/articles/SpinningAnimation.zip
下面说说实现过程:
第一步:添加一个旋转png图片到工程中去
第二步:在MainPage,xaml中添加下面的动画效果
<phone:PhoneApplicationPage.Resources>
<Storyboard x:Name="SpinningAnimation">
<DoubleAnimation AutoReverse="False"
Duration="0:0:3"
From="0"
RepeatBehavior="Forever"
Storyboard.TargetName="SpinningRotateTransform"
Storyboard.TargetProperty="Angle"
To="360" />
</Storyboard>
</phone:PhoneApplicationPage.Resources>
第三步:把下面代码加入到MainPage.xaml中:
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Background="Red">
<Image
Margin="10,10"
Height="50"
Width="50"
Source="/Spinner.png"
Stretch="Uniform">
<Image.RenderTransform>
<RotateTransform x:Name="SpinningRotateTransform"
CenterX="25"
CenterY="25" />
</Image.RenderTransform>
</Image>
</StackPanel>
第四步:添加两个按钮来开始结束动画
<Button Content="Start Animation" Click="btnStart_Click"/>
<Button Content="Stop Animation" Click="btnStop_Click"/>
private void btnStart_Click(object sender, RoutedEventArgs e) { this.SpinningAnimation.Begin(); } private void btnStop_Click(object sender, RoutedEventArgs e) { this.SpinningAnimation.Stop(); }
第五步:如果运行后看不到效果,一般情况下是旋转图片的颜色和页面的背景色一致,改变颜色以后就能看到了,这里可以使用一个不透明性的Mask,添加下面代码:
<Ellipse Fill="{StaticResource PhoneForegroundBrush}" Height="50" Width="50" Margin="10,10">
<Ellipse.OpacityMask>
<ImageBrush ImageSource="/Spinner.png" Stretch="Uniform"/>
</Ellipse.OpacityMask>
<Ellipse.RenderTransform>
<RotateTransform x:Name="SpinningRotateTransform"
CenterX="25"
CenterY="25" />
</Ellipse.RenderTransform>
</Ellipse>
最终全部代码如下:
MainPage.xaml:
<phone:PhoneApplicationPage
x:Class="SpinningProgressBar.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<Storyboard x:Name="SpinningAnimation">
<DoubleAnimation AutoReverse="False"
Duration="0:0:3"
From="0"
RepeatBehavior="Forever"
Storyboard.TargetName="SpinningRotateTransform"
Storyboard.TargetProperty="Angle"
To="360" />
</Storyboard>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" >
<Ellipse Fill="{StaticResource PhoneForegroundBrush}" Height="50" Width="50" Margin="10,10">
<Ellipse.OpacityMask>
<ImageBrush ImageSource="/Spinner.png" Stretch="Uniform"/>
</Ellipse.OpacityMask>
<Ellipse.RenderTransform>
<RotateTransform x:Name="SpinningRotateTransform"
CenterX="25"
CenterY="25" />
</Ellipse.RenderTransform>
</Ellipse>
<Button Content="Start Animation" Click="btnStart_Click"/>
<Button Content="Stop Animation" Click="btnStop_Click"/>
</StackPanel>
</Grid>
</phone:PhoneApplicationPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SpinningProgressBar.Resources;
namespace SpinningProgressBar
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void btnStart_Click(object sender, RoutedEventArgs e) { this.SpinningAnimation.Begin(); } private void btnStop_Click(object sender, RoutedEventArgs e) { this.SpinningAnimation.Stop(); }
}
}
源代码可以在这里下载:http://www.windowsphonegeek.com/upload/articles/SpinningAnimation.zip
相关文章推荐
- windows phone开发学习--加载时的旋转动画
- windows phone开发学习--Pivot动态加载数据
- windows phone开发学习--storyboard的动画效果
- 安卓开发之自定义粒子旋转动画加载控件
- ios开发学习--动画(Animation)效果源码分享
- 转载:Windows Phone 7.5 (Mango) 开发学习资源整理
- iOS开发动画(Animation)图片360度不停旋转
- Windows Phone开发(36):动画之DoubleAnimation 转:http://blog.csdn.net/tcjiaan/article/details/7521388
- Windows Phone实用开发技巧(1):保存图片及加载图片
- cocos2d-js 开发基础练习代码(1)——熟悉场景加载,精灵,动画使用等
- Windows Phone 7常用的开发技巧&学习总结
- iOS开发动画(Animation)图片360度不停旋转
- CSS 实现加载动画之一-菊花旋转
- Windows Phone 7 开发探索笔记7——加载XML文件
- Windows Phone开发经验谈(18)-总结两种滚动条到底部加载数据的方法
- Windows Phone开发(36):动画之DoubleAnimation
- python web开发之flask框架学习(2) 加载模版
- Windows Phone实用开发技巧(1):保存图片及加载图片
- 【学习】CALayer实现,界限、透明度、位置、旋转、缩放组合动画
- 【Android开发新手的学习笔记】异步加载知识梳理