xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
WIDth ="320" Height ="240" >
< Canvas x:name ="ItemCanvas" WIDth ="320" Height ="240" Canvas.left ="77" Canvas.top ="57" Background ="#FFFFFFFF"
RendertransformOrigin ="0.5,0.5" >
< Canvas.Rendertransform >
< transformGroup >
< Rotatetransform x:name ="RotateItemCanvas" Angle ="0" />
</ transformGroup >
</ Canvas.Rendertransform >
< Image x:name ="Image" WIDth ="300" Height ="220" Canvas.left ="10" Canvas.top ="10" Source ="" Stretch ="Fill" />
< Ellipse x:name ="Handle" WIDth ="15" Height ="15" Fill ="#FFEAFF00" stroke ="#FF000000" Canvas.left ="313" Canvas.top ="233" />
</ Canvas >
</ UserControl >
分析上面的xaml可以知道,整个界面通过基于坐标的Canvas进行布局,默认设置布局容器的旋转角度为0度,在Canvas里面放置了一个图片作为可旋转的对象外观呈现,一个圆形作为旋转焦点。最终实现旋转功能的就是鼠标在Ellipse对象上的事件应用,通过事件处理函数来改变整个布局容器的旋转角度(Angle)。 private bool IsMouseCaptured;
private Point Mouseposition;
private Point Lastposition;
public Point CanvasCenter;
private double LastAngle;
private double CurrentAngle;
private double AngleDelta;
public RotateItem()
{
InitializeComponent();
// 注册Ellipse对象的鼠标事件
Handle.MouseleftbuttonDown += new MousebuttonEventHandler(Handle_MouseleftbuttonDown);
Handle.MouseleftbuttonUp += new MousebuttonEventHandler(Handle_MouseleftbuttonUp);
Handle.MouseMove += new MouseEventHandler(Handle_MouseMove);
}
private voID Handle_MouseleftbuttonUp( object sender, MousebuttonEventArgs e)
{
FrameworkElement Item = sender as FrameworkElement;
Item.ReleaseMouseCapture();
IsMouseCaptured = false ;
Item.Cursor = null ;
}
private voID Handle_MouseleftbuttonDown( object sender, MousebuttonEventArgs e)
{
FrameworkElement Item = sender as FrameworkElement;
Item.CaptureMouse();
Item.Cursor = Cursors.Hand;
IsMouseCaptured = true ;
Lastposition = e.Getposition( null );
} 最关键的就是MouseMove事件了,在MouseMove事件处理函数中,通过计算鼠标点下时的坐标和当前所在的坐标进行弧度转化角度的计算,将得到的角度值设置为Canvas的旋转角度就达到了实现对象的自由旋转功能。 以下为弧度转化为角度的计算公式以及MouseMove事件算法实现: /// <summary>
/// 弧度转化为角度
/// </summary>
/// <param name="radians"></param>
/// <returns></returns>
private double radiansTodegrees( double radians)
{
return radians * 180 / Math.PI;
} private voID Handle_MouseMove( object sender, MouseEventArgs e)
{
Mouseposition = e.Getposition( null );
if (IsMouseCaptured)
{
LastAngle = Math.atan2(Lastposition.Y - CanvasCenter.Y, Lastposition.X - CanvasCenter.X);
CurrentAngle = Math.atan2(Mouseposition.Y - CanvasCenter.Y, Mouseposition.X - CanvasCenter.X);
AngleDelta = CurrentAngle - LastAngle;
RotateItemCanvas.Angle += radiansTodegrees(AngleDelta);
Lastposition = Mouseposition;
}
} 可旋转UserControl完整代码 public partial class RotateItem : UserControl
{
private bool IsMouseCaptured;
private Point Mouseposition;
private Point Lastposition;
public Point CanvasCenter;
private double LastAngle;
private double CurrentAngle;
private double AngleDelta;
public RotateItem()
{
InitializeComponent();
Handle.MouseleftbuttonDown += new MousebuttonEventHandler(Handle_MouseleftbuttonDown);
Handle.MouseleftbuttonUp += new MousebuttonEventHandler(Handle_MouseleftbuttonUp);
Handle.MouseMove += new MouseEventHandler(Handle_MouseMove);
}
private voID Handle_MouseleftbuttonUp( object sender, MousebuttonEventArgs e)
{
FrameworkElement Item = sender as FrameworkElement;
Item.ReleaseMouseCapture();
IsMouseCaptured = false ;
Item.Cursor = null ;
}
private voID Handle_MouseleftbuttonDown( object sender, MousebuttonEventArgs e)
{
FrameworkElement Item = sender as FrameworkElement;
Item.CaptureMouse();
Item.Cursor = Cursors.Hand;
IsMouseCaptured = true ;
Lastposition = e.Getposition( null );
}
private voID Handle_MouseMove( object sender, MouseEventArgs e)
{
Mouseposition = e.Getposition( null );
if (IsMouseCaptured)
{
LastAngle = Math.atan2(Lastposition.Y - CanvasCenter.Y, Lastposition.X - CanvasCenter.X);
CurrentAngle = Math.atan2(Mouseposition.Y - CanvasCenter.Y, Mouseposition.X - CanvasCenter.X);
AngleDelta = CurrentAngle - LastAngle;
RotateItemCanvas.Angle += radiansTodegrees(AngleDelta);
Lastposition = Mouseposition;
}
}
/// <summary>
/// 弧度转化为角度
/// </summary>
/// <param name="radians"></param>
/// <returns></returns>
private double radiansTodegrees( double radians)
{
return radians * 180 / Math.PI;
}
} 使用也是非常简单的,动态创建上面所创建的UserControl然后将其添加到主容器控件中就可以了,如下演示代码: public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
var Picture1 = new RotateItem();
Picture1.Image.source = new BitmAPImage( new Uri( " Marigold.jpg " , UriKind.relative));
Picture1.SetValue(Canvas.leftProperty, 100.00 );
Picture1.SetValue(Canvas.topProperty, 100.00 );
Picture1.CanvasCenter.X = ( double )Picture1.GetValue(Canvas.leftProperty) + Picture1.WIDth / 2 ;
Picture1.CanvasCenter.Y = ( double )Picture1.GetValue(Canvas.topProperty) + Picture1.Height / 2 ;
Picture1.RotateItemCanvas.Angle = - 15 ;
LayoutRoot.Children.Add(Picture1);
}
} 推荐资源: Silverlight & Blend动画设计系列文章 《Function Silverlight 3 Animation》----本篇中使用的示例素材选自此书 总结
以上是内存溢出为你收集整理的Silverlight & Blend动画设计系列十二:三角函数(Trigonometry)动画之自由旋转(Free-form rotation)全部内容,希望文章能够帮你解决Silverlight & Blend动画设计系列十二:三角函数(Trigonometry)动画之自由旋转(Free-form rotation)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)