如何为Windows Phone的单轴制作缩放图像解决方案?

时间:2013-11-13 07:06:29

标签: c# windows-phone-7 windows-phone-8 windows-phone scale

我希望用户可以在一个轴上缩放图像,用户可以按比例缩放图像。

我有xaml ::

<Image Source="my_image.jpg" ManipulationDelta="UIElement_OnManipulationDelta" Width="400" Height="400">
 <Image.RenderTransform>
  <ScaleTransform x:Name="scaleImage" CenterX="200" CenterY="200"></ScaleTransform>
 </Image.RenderTransform>
</Image>

我有代码:

 private void UIElement_OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            //scale image by only x
            if (e.DeltaManipulation.Scale.X != 0 && e.DeltaManipulation.Scale.Y == 1)
            {
                    scaleImage.ScaleX *= e.DeltaManipulation.Scale.X;
            }
            //scale image by only y
            else if (e.DeltaManipulation.Scale.X == 1 && e.DeltaManipulation.Scale.Y != 0)
            {
                    scaleImage.ScaleY *= e.DeltaManipulation.Scale.Y;
            }
             //proportion scale
            else if (e.DeltaManipulation.Scale.X != 0 && e.DeltaManipulation.Scale.Y != 0)
            {                   
                    scaleImage.ScaleX *= e.DeltaManipulation.Scale.X;

                    scaleImage.ScaleY *= e.DeltaManipulation.Scale.X;

            }

        }

但是这段代码非常不稳定。

如何改进此解决方案?

1 个答案:

答案 0 :(得分:1)

我已经写了自己的解决方案。 WindowsPhone Toolkit用于解决方案(toolkit:GestureService.GestureListener)

的Xaml:

<Image Name="my_image" Width="450" Height="450" Source="my_image.jpg" CacheMode="BitmapCache">
 <Image.RenderTransform>
  <CompositeTransform CenterX="225" CenterY="225" x:Name="ImageTransformation" ScaleX="1" ScaleY="1" />
 </Image.RenderTransform>
 <toolkit:GestureListener PinchStarted="GestureListener_PinchStarted" PinchDelta="OnPinchDelta" />
 </toolkit:GestureService.GestureListener>
</Image>

c#c​​ode:

    private double InitialScale;
    private Point firstTouch; 
    private Point secondTouch;

private void GestureListener_PinchStarted(object sender, PinchStartedGestureEventArgs e)
        {
            // Store the initial scaling
            InitialScale = ImageTransformation.ScaleX;

            firstTouch = e.GetPosition(photo, 0);
            secondTouch = e.GetPosition(photo, 1);

        }

        private void OnPinchDelta(object sender, PinchGestureEventArgs e)
        {
            double difX;
            double difY;
            if (firstTouch.Y >= secondTouch.Y)
            {
                difY = firstTouch.Y - secondTouch.Y;
            }
            else
            {
                difY = secondTouch.Y - firstTouch.Y;
            }

            if (firstTouch.X >= secondTouch.X)
            {
                difX = firstTouch.X - secondTouch.X;
            }
            else
            {
                difX = secondTouch.X - firstTouch.X;
            }

            if (difX <= difY)
            {
                if (difX < 50)
                {
                    ImageTransformation.ScaleY = InitialScale * e.DistanceRatio;
                }
                else
                {
                    ImageTransformation.ScaleX = InitialScale * e.DistanceRatio;
                    ImageTransformation.ScaleY = InitialScale * e.DistanceRatio;
                }
            }
            else
            {
                if (difY < 50)
                {
                    ImageTransformation.ScaleX = InitialScale * e.DistanceRatio;
                }
                else
                {
                    ImageTransformation.ScaleX = InitialScale * e.DistanceRatio;
                    ImageTransformation.ScaleY = InitialScale * e.DistanceRatio;
                }
            }
        }