Windows Phone Image Pinch&缩放重置

时间:2014-06-27 10:13:12

标签: image windows-phone-8 windows-phone viewport pinchzoom

我正在使用此代码进行捏合和缩放,但它工作正常,但我必须双击重置。

我的Xaml代码;

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ViewportControl x:Name="viewport"  
            ManipulationStarted="OnManipulationStarted" ManipulationDelta="OnManipulationDelta"  
                         ManipulationCompleted="OnManipulationCompleted" ViewportChanged="viewport_ViewportChanged">
            <Canvas x:Name="canvas">
                <Image x:Name="TestImage" Source="/Assets/test.jpg"  
                        RenderTransformOrigin="0,0" CacheMode="BitmapCache"
                       ImageOpened="OnImageOpened">
                    <Image.RenderTransform>
                        <ScaleTransform x:Name="xform"/>
                    </Image.RenderTransform>
                </Image>
            </Canvas>
        </ViewportControl>
    </Grid>

我的变数:

const double MaxScale = 10;

    double _scale = 1.0;
    double _minScale;
    double _coercedScale;
    double _originalScale;

    Size _viewportSize;
    bool _pinching;
    Point _screenMidpoint;
    Point _relativeMidpoint;

    BitmapImage _bitmap;

我的方法;

    void viewport_ViewportChanged(object sender, System.Windows.Controls.Primitives.ViewportChangedEventArgs e)
    {
        Size newSize = new Size(viewport.Viewport.Width, viewport.Viewport.Height);
        if (newSize != _viewportSize)
        {
            _viewportSize = newSize;
            CoerceScale(true);
            ResizeImage(false);
        }
    }


    void OnManipulationStarted(object sender, ManipulationStartedEventArgs e)
    {
        _pinching = false;
        _originalScale = _scale;
    }


    void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
    {
        if (e.PinchManipulation != null)
        {
            e.Handled = true;

            if (!_pinching)
            {
                _pinching = true;
                Point center = e.PinchManipulation.Original.Center;
                _relativeMidpoint = new Point(center.X / TestImage.ActualWidth, center.Y / TestImage.ActualHeight);

                var xform = TestImage.TransformToVisual(viewport);
                _screenMidpoint = xform.Transform(center);
            }

            _scale = _originalScale * e.PinchManipulation.CumulativeScale;

            CoerceScale(false);
            ResizeImage(false);
        }
        else if (_pinching)
        {
            _pinching = false;
            _originalScale = _scale = _coercedScale;
        }
    }


    void OnManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
    {
        _pinching = false;
        _scale = _coercedScale;
    }



    void OnImageOpened(object sender, RoutedEventArgs e)
    {
        _bitmap = (BitmapImage)TestImage.Source;
        _scale = 0;
        CoerceScale(true);
        _scale = _coercedScale;

        ResizeImage(true);
    }


    void ResizeImage(bool center)
    {
        if (_coercedScale != 0 && _bitmap != null)
        {
            double newWidth = canvas.Width = Math.Round(_bitmap.PixelWidth * _coercedScale);
            double newHeight = canvas.Height = Math.Round(_bitmap.PixelHeight * _coercedScale);

            xform.ScaleX = xform.ScaleY = _coercedScale;

            viewport.Bounds = new Rect(0, 0, newWidth, newHeight);

            if (center)
            {
                viewport.SetViewportOrigin(
                    new Point(
                        Math.Round((newWidth - viewport.ActualWidth) / 2),
                        Math.Round((newHeight - viewport.ActualHeight) / 2)
                        ));
            }
            else
            {
                Point newImgMid = new Point(newWidth * _relativeMidpoint.X, newHeight * _relativeMidpoint.Y);
                Point origin = new Point(newImgMid.X - _screenMidpoint.X, newImgMid.Y - _screenMidpoint.Y);
                viewport.SetViewportOrigin(origin);
            }
        }
    }


    void CoerceScale(bool recompute)
    {
        if (recompute && _bitmap != null && viewport != null)
        {
            double minX = viewport.ActualWidth / _bitmap.PixelWidth;
            double minY = viewport.ActualHeight / _bitmap.PixelHeight;

            _minScale = Math.Min(minX, minY);
        }

        _coercedScale = Math.Min(MaxScale, Math.Max(_scale, _minScale));

    }

private void imgZoom_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        //Reset Viewport and Image
    }

如何重置我的视口和图像以便将它们用于另一个?

感谢。

0 个答案:

没有答案