0

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

我有 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;

            }

        }

但是此代码的工作原理非常不稳定。

如何改进这个解决方案?

4

1 回答 1

1

我已经写了我自己的解决方案。解决方案中使用了WindowsPhone Toolkit(工具包:GestureService.GestureListener)

xml:

<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#代码:

    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;
                }
            }
        }
于 2013-11-14T06:59:37.860 回答