How can I programmatically find the scale, rotate, and translate values when using the MultiTouchBehavior in a Silverlight/WP7 app?
Okay, this may seem like a really silly question but I'm not quite used to the Silverlight realm yet. I'm working on a Silverlight / WP7 application that references the Multitouch.Behaviors.WP7 codeplex project (http://multitouch.codeplex.com/).
<Canvas x:Name="MyCanvas">
<Image Source="Images/image.png"
x:Name="MyImage" Width="120"
Canvas.Left="240" Canvas.Top="235">
<i:Interaction.Behaviors>
<tb:MultiTouchBehavior
AreFingersVisible="True"
IsDebugModeActive="True"
IsMockActive="True"
IsRotateEnabled="True"
IsScaleEnabled="True"
IsTranslateXEnabled="True"
IsTranslateYEnabled="True"
MaximumScale="100"
MinimumScale="0.5"/&开发者_如何学运维gt;
</i:Interaction.Behaviors>
</Image>
Obviously, the user can drag the image around, rotate it, and scale it with the multitouch functionality. How can I access the values for how the image has been scaled/rotated/translated programatically?
The MultitouchBehavior uses a CompositeTransform as the RenderTransform for the object that it is associated with. So, you can use code similar to the following to access the transformed values:
var transform = this.MyImage.RenderTransform as CompositeTransform;
var currentScaleX = transform.ScaleX;
var angle = transform.Rotation;
var offsetX = transform.TranslateX;
You need to use Image.RenderTransform. See this post (particularly the GestureHelper section - http://timheuer.com/blog/archive/2010/09/16/silverlight-toolkit-for-windows-phone-7-released.aspx).
<Image x:Name="GesturedImage" Source="dividbyzero.jpg"
HorizontalAlignment="Center" VerticalAlignment="Center"
Width="450" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<ScaleTransform x:Name="ImageScaling" ScaleX="1" ScaleY="1" />
</Image.RenderTransform>
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener PinchDelta="OnPinchDelta" />
</toolkit:GestureService.GestureListener>
</Image>
And then use DistanceRatio to get the value for translation:
private void OnPinchDelta(object sender, PinchGestureEventArgs e)
{
ImageScaling.ScaleX = e.DistanceRatio;
ImageScaling.ScaleY = e.DistanceRatio;
}
精彩评论