using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
System.Windows.Threading;
using
System.Windows.Media.Imaging;
namespace
SilverlightDemo
{
public
partial
class
ShopShow : UserControl
{
private
double
centerX
=
400
;
private
double
centerY
=
300
;
private
double
width
=
400
;
private
double
height
=
60
;
private
double
degree
=
0
;
//
度数值
List
<
ShopItem
>
objList
=
new
List
<
ShopItem
>
();
//
项集合类
private
double
itemWidth
=
160
;
private
double
itemHeight
=
80
;
private
double
count
=
14
;
private
double
currentOpacity
=
0
;
private
DispatcherTimer timer;
public
ShopShow()
{
InitializeComponent();
this
.Loaded
+=
new
RoutedEventHandler(ShopShow_Loaded);
}
private
void
ShopShow_Loaded(
object
sender, RoutedEventArgs e)
{
this
.timer
=
new
DispatcherTimer();
for
(var i
=
1
; i
<=
this
.count; i
++
)
{
//
实例化用户控件
ShopItem myShopItem
=
new
ShopItem();
Image myImage
=
myShopItem.obj;
//
加载唱片图片
Uri myUri
=
new
Uri(String.Format(
"
http://localhost:2149/Images/album{0}.jpg
"
, i));
BitmapImage bitmap
=
new
BitmapImage(myUri);
myImage.Source
=
bitmap;
//
绑定控件事件
myImage.MouseEnter
+=
new
MouseEventHandler(myImage_MouseEnter);
myImage.MouseLeave
+=
new
MouseEventHandler(myImage_MouseLeave);
myImage.MouseLeftButtonDown
+=
new
MouseButtonEventHandler(myImage_MouseLeftButtonDown);
//
添加到用户控件里
this
.objList.Add(myShopItem);
moveCanvas.Children.Add(myShopItem);
}
timer.Tick
+=
new
EventHandler(timer_Tick);
TimeSpan sp
=
new
TimeSpan(
0
,
0
,
0
,
0
,
10
);
timer.Interval
=
sp;
timer.Start();
}
public
void
myImage_MouseLeftButtonDown(
object
sender, MouseButtonEventArgs e)
{
Image img
=
sender
as
Image;
shower.Visibility
=
Visibility.Visible;
shower.Source
=
img.Source;
}
public
void
myImage_MouseEnter(
object
sender, MouseEventArgs e)
{
timer.Stop();
Image img
=
sender
as
Image;
currentOpacity
=
img.Opacity;
img.Opacity
=
1
;
}
public
void
myImage_MouseLeave(
object
sender, MouseEventArgs e)
{
timer.Start();
Image img
=
sender
as
Image;
img.Opacity
=
currentOpacity;
}
public
void
timer_Tick(
object
sender, EventArgs e)
{
StartMove();
}
private
void
StartMove()
{
for
(var i
=
0
; i
<
objList.Count;i
++
)
{
//
根据控件数量总数和周圆计算一个平均值
var tmp
=
(
this
.degree
+
(
360
/
this
.count
*
i))
%
360
;
tmp
=
tmp
*
Math.PI
/
180
;
var posX
=
(
this
.width)
*
Math.Sin(tmp);
//
更新X坐标
var posY
=
(
this
.height)
*
Math.Cos(tmp);
//
更新Y坐标
ShopItem obj
=
this
.objList[i];
//
根据高宽计算缩放比例
double
scale
=
(
2
*
this
.height
-
posY)
/
(
3
*
this
.height
+
this
.itemHeight
/
2
);
Canvas.SetLeft(obj, centerX
+
posX
-
(itemWidth
/
2
)
*
scale);
Canvas.SetTop(obj, centerY
-
posY
-
(itemHeight
/
2
)
*
scale);
Canvas.SetZIndex(obj,
int
.Parse(Math.Ceiling(count
*
scale).ToString()));
//
创建并应用变形属性
ScaleTransform st
=
new
ScaleTransform();
st.ScaleX
=
scale;
st.ScaleY
=
scale;
obj.RenderTransform
=
st;
obj.Opacity
=
scale;
}
this
.degree
=
this
.degree
-
0.3
;
}
private
void
btnStart_Click(
object
sender, RoutedEventArgs e)
{
timer.Start();
}
private
void
btnStop_Click(
object
sender, RoutedEventArgs e)
{
timer.Stop();
}
}
}