程序里想实现8个方向键,为了节约资源,我只想用一张右方向键来实现。所以我定义了一个自定义的方法来实现图片的旋转,方法中按随即产生的方向来改变旋转的角度。角度是改变了,但是图片缺没有被正确的旋转。刚刚接触。帮小弟瞧瞧:
private void init()
{
rnd = new Random();
for (int i = 0; i < butnum; i++)
{
int p = rnd.Next((int)Pictures.PointArrow.右,(int)Pictures.PointArrow.右上);
Image img = new Image();
img.Source = new BitmapImage(new Uri("img/point.png",UriKind.Relative));
TransformImage(img,(Pictures.PointArrow)p);
borderroundControl bc = new borderroundControl();
Border bdr = bc.FindName("borderround") as Border;
Canvas can = bc.FindName("can") as Canvas;
can.Children.Add(img);
bdr.Child = can;
canvasround.Children.Add(bc);
Canvas.SetLeft(bc, i * (bc.Width+4)+10);
Canvas.SetTop(bc,1);
}
}
private void TransformImage(Image img,Pictures.PointArrow p)
{
RotateTransform rt = new RotateTransform();
switch (p)
{
case Pictures.PointArrow.右:
rt.Angle = 0;
break;
case Pictures.PointArrow.下:
rt.Angle = 90;
break;
case Pictures.PointArrow.左:
rt.Angle = 180;
break;
case Pictures.PointArrow.上:
rt.Angle = 270;
break;
case Pictures.PointArrow.右下:
rt.Angle = 45;
break;
case Pictures.PointArrow.左下:
rt.Angle = 135;
break;
case Pictures.PointArrow.左上:
rt.Angle = 225;
break;
case Pictures.PointArrow.右上:
rt.Angle = 315;
break;
default:
rt.Angle = 0;
break;
}
MessageBox.Show(rt.Angle.ToString());
rt.CenterX = 12;
rt.CenterY = 12;
img.RenderTransform=rt;
}
}
能不能把你的程序发给我,帮你调试一下,如果你是SL4的话,就不要传了,我现在还用的是SL3
你可以参考一下http://blog.csdn.net/beniao277/archive/2010/03/26/5418131.aspx,这个文章有关于旋转的,而且还有其它的动画