WP7:模拟开始屏幕Tile漂动效果
在WP7手机的开始屏幕,如果你Hold住某一个瓷贴,就会发现除了你按住的那个瓷贴其他全部下沉半透明,然后开始在不停地漂来漂去~~
今天来模仿一下这个效果。
新建一个项目,然后在Grid里放一个ListBox。
OK 开始编写 ListBox 的模版。
首先是ItemsPanelTemplate。
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
我们放一个WrapPanel,让它进行自动排列和换行。
然后就是主要的 ItemTemplate。
<ListBox.ItemTemplate>
<DataTemplate>
<Canvas Width="185" Height="185">
<Grid x:Name="grid">
<Grid.RenderTransform>
<TranslateTransform/>
</Grid.RenderTransform>
<Grid.Resources>
<Storyboard x:Name="sbTranslate">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="grid"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)">
<EasingDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<EasingDoubleKeyFrame x:Name="translateX" KeyTime="00:00:1" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<SineEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="grid"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)">
<EasingDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<EasingDoubleKeyFrame x:Name="translateY" KeyTime="00:00:1" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<SineEase EasingMode="EaseInOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Grid.Resources>
<Image Visibility="Collapsed" Source="/Images/StartPage_DeleteFav.png" Width="42" Height="42" HorizontalAlignment="Right" VerticalAlignment="Top" Canvas.ZIndex="1" MouseLeftButtonUp="Image_MouseLeftButtonUp" Margin="-10"/>
<toolkit:HubTile GroupTag="QuickLink" Notification="{Binding Notification}" Message="{Binding Message}" Title="{Binding Title}" Source="{Binding Src}" Background="{StaticResource PhoneAccentBrush}"/>
</Grid>
</Canvas>
</DataTemplate>
</ListBox.ItemTemplate>
这里 放了一个 Grid ,里面很简单,一个 Image 用来 做 右上角删除图标。然后是一个 HubTile,简单模仿一下。
主要的是 Grid 里 写的 资源,两个 DoubleAnimationUsingKeyFrames,用来操作 TranslateTransform。
好,前台很简单。来看看后台。先为 ListBox 添加几个事件:
Loaded="listBox_Loaded" LostFocus="listBox_LostFocus" SelectionChanged="listBox_SelectionChanged"
先来写Loaded 事件:
for (int i = 0; i < listBox.Items.Count; i++)
{
ListBoxItem item = (ListBoxItem)listBox.ItemContainerGenerator.ContainerFromIndex(i);
item.Hold += item_Hold;
}
为每个listboxitem 添加 必须的 Hold 事件。
void item_Hold(object sender, System.Windows.Input.GestureEventArgs e)
{
if (!isHold)
{
isHold = true;
var item = sender as ListBoxItem;
holdItem = item;
for (int i = 0; i < items.Count; i++)
{
ListBoxItem _item = (ListBoxItem)listBox.ItemContainerGenerator.ContainerFromItem(items[i]);
if (_item != item)
{
Transform(1, 0.75, _item, 0.2);
var grid = FindVisualChild<Grid>(_item);
var sb = grid.Resources["sbTranslate"] as Storyboard;
storyList.Add(Bouncing(sb,false), grid.DataContext);
}
else
{
//显示按住item的关闭按钮
var img = FindVisualChild<Image>(_item);
img.Visibility = System.Windows.Visibility.Visible;
}
}
}
}
在item_Hold事件里做了两件事:显示按住item的关闭按钮,其他item 下沉半透明,然后是启动漂动:Bouncing(sb,false);
Bouncing 这个方法,返回一个 DispatcherTimer 第一个参数是一个Storyboard,那这里放入的就是在前台为 Grid 添加的 资源里的 Storyboard。
先是找到这个 ListboxItem 里的 FindVisualChild<Grid>(_item),然后再找出他的 Storyboard。
那重点来说一下这个函数。
private DispatcherTimer Bouncing(Storyboard sb,bool isover)
{
Random random = new Random();
DoubleKeyFrame sp0 = ((DoubleAnimationUsingKeyFrames)sb.Children[0]).KeyFrames[0];
DoubleKeyFrame ea0 = ((DoubleAnimationUsingKeyFrames)sb.Children[0]).KeyFrames[1];
DoubleKeyFrame sp1 = ((DoubleAnimationUsingKeyFrames)sb.Children[1]).KeyFrames[0];
DoubleKeyFrame ea1 = ((DoubleAnimationUsingKeyFrames)sb.Children[1]).KeyFrames[1];
sp0.Value = 0;
sp1.Value = 0;
ea0.Value = 0;
ea1.Value = 0;
DispatcherTimer dispatcherTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(0) };
dispatcherTimer.Tick += delegate
{
sb.Stop();
sp0.Value = ea0.Value;
sp1.Value = ea1.Value;
if (!isover)
{
ea0.Value = random.Next(-10, 10);//随机数 X
ea1.Value = random.Next(-10, 10);//随机数 Y
sb.Begin();
}
dispatcherTimer.Interval = TimeSpan.FromSeconds(random.Next(9, 13) *0.1);
};
if (isover)
sb.Begin();
else
dispatcherTimer.Start();
return dispatcherTimer;
}
先是根据传入的 Storyboard 找出 它里面DoubleAnimationUsingKeyFrames的每个DoubleKeyFrame。
然后在找出每个DoubleKeyFrame的Value,并初始化。
接下来是生成一个DispatcherTimer对象,这里初始化的Interval是0,也就是立马执行下面Tick的事件,不会造成刚开始动画延迟的感觉。
在对ea0和ea1赋值,用一个随机数,范围在-10到10。也这就在这个方圆内漂动。
而上面的sp0和sp1的赋值是为了下一次动画,不是从零开始的。
下面还有一句:dispatcherTimer.Interval = TimeSpan.FromSeconds(random.Next(9, 13) *0.1);
这句是为了让每个Timer的间隔有所不同,就不会造成所有瓷贴同时漂动同时结束的统一动作。
OK,主要的代码都介绍完了,上图

还有很多功能,比如 listBox_SelectionChanged,OnBackKeyPress,StopBouncing,就不多介绍了,这里就说一下我实现的方法思路,
就是通过,调用动画通过 Timer 来控制,来实现 飘动效果。
如果有兴趣,可以继续了解我下面的 DEMO。
BouncingDemo:http://dl.dbank.com/c0upp01pv7
WP7:模拟开始屏幕Tile漂动效果的更多相关文章
- 手机端轻应用模拟原生的下拉刷新效果(JavaScript)
方案一:使用iscoll等有下拉功能的框架. 分析:因为项目的结构已经基本完成,再使用框架,会与原来的结构互相影响: 方案二:用JavaScript.Jquery写. 分析:可能没有直接使用框架的效果 ...
- ANDROID模拟火花粒子的滑动喷射效果
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 年前换了一个手机,SONY的Z3C.这个手机在解锁屏幕时有一个滑动动画,类似火 ...
- Android与.Net交互模拟用户屏幕操作添加APN和网络4G/3G切换
前几天接到一个需求,我们的客户需要对手机网络接入点进行可用性测试,简单点说就是需要实现Android上的APN配置的添加,APN切换网络模式4G/3G/2G切换,我要调研下写个demo. 因为是要实现 ...
- WPF界面设计技巧(7)—模拟电梯升降的缓动动画
原文:WPF界面设计技巧(7)-模拟电梯升降的缓动动画 如同Flash一样,WPF的亮点之一也在于其擅于表现平滑的动画效果,但以移动动画来说,仅凭简单的起始位置.目标位置,所产生的动画仍会非常生硬,这 ...
- 过渡与动画 - 缓动效果&基于贝塞尔曲线的调速函数
难题 给过渡和动画加上缓动效果是一种常见的手法(比如具有回弹效果的过渡过程)是一种流行的表现手法,可以让界面显得更加生动和真实:在现实世界中,物体A点到B点往往也是不完全匀速的 以纯技术的角度来看,回 ...
- iOS开发之使用Storyboard预览UI在不同屏幕上的运行效果
在公司做项目一直使用Storyboard,虽然有时会遇到团队合作的Storyboard冲突问题,但是对于Storyboard开发效率之高还是比较划算的.在之前的博客中也提到过,团队合作使用Storyb ...
- canvas弹动效果
弹动效果,用物体与目标的距离乘上系数再累加至速度上,让物体呈加速度运动,再让速度乘与摩擦力系数,让物体最终停止运动 代码如下所示 var canvas = document.getElementByI ...
- javascript的缓动效果
这部分对原先的缓动函数进行抽象化,并结合缓动公式进行强化.成品的效果非常惊人逆天.走过路过不要错过. 好了,打诨到此为止.普通的加速减速是难以让人满意的,为了实现弹簧等让人眼花缭乱的效果必须动用缓动公 ...
- animation js控制 缓动效果
<!DOCTYPE html><html><head><meta charset="utf-8" /><title>缓动 ...
随机推荐
- move 和 CopyMemory的区别
Move(ABuffer,P, Sizeof(ABuffer)); //指针传递 Move(ABuffer^,P^, Sizeof(TArrayByte)); //复制内 ...
- 同时运行多个scrapy爬虫的几种方法(自定义scrapy项目命令)
试想一下,前面做的实验和例子都只有一个spider.然而,现实的开发的爬虫肯定不止一个.既然这样,那么就会有如下几个问题:1.在同一个项目中怎么创建多个爬虫的呢?2.多个爬虫的时候是怎么将他们运行起来 ...
- Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. Notice The minimu ...
- makefile中的自动化变量$@,$%,$
转自:http://www.2cto.com/os/201302/191344.html makefile中的自动化变量$@,$%,$ 自动化变量 模式规则中,规则的目标和依赖文件名代表了一 ...
- JavaScript toFixed()使用的注意事项
以下是w3school的定义: 定义和用法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 语法 NumberObject.toFixed(num) 参数 描述 num 必 ...
- 【转】Python 代码调试技巧
转载自:http://www.ibm.com/developerworks/cn/linux/l-cn-pythondebugger/ Debug 对于任何开发人员都是一项非常重要的技能,它能够帮助我 ...
- Android 中的code sign
Android 中和ios中都有code sign.它们的目的一样,都是要保证程序的可靠性,最基本实现原理也一样.但是sign的过程比较不同. 下面记录一点Android sign的重要知识. 请参看 ...
- Java for LeetCode 035 Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ios学习总结(2) -- UIButton的使用
原文地址 UIButton的类是一个UIControl子类,它实现了在触摸屏上的按钮.触摸一个按钮拦截事件和动作消息发送到目标对象时,它的挖掘.设定的目标和行动方法都继承自UIControl.这个类提 ...
- [Eclipse] Eclipse中,Add Jars与Add Library的区别
refer to : http://blog.csdn.net/gaojinshan/article/details/16948075 Eclipse中,工程属性的Java Build Path的Li ...