Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implement image zooming in a scrollviewer on windows phone

I placed an image control in a scrollviewer just like tnis:

        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" >
        <Image Source="/Test/1.jpg" Width="320">
            <Image.RenderTransform>
                <CompositeTransform ScaleX="{Binding Path=Value, ElementName=slider}"/>
            </Image.RenderTransform>
        </Image>
    </ScrollViewer>

As th code showed , I added a slider to control the Compositetransform of the image, but when I change the value of the slider, nothing happened?

And I also tried to attach a zoom&pan behavior(depended on the toolkit) on the image, unfortunately, I could scroll up and down, but I could not zoom in/out the image. It seemed that the Scrollviewer blocked the pinch manipulation.

As we know, the Scrollviewer control had a "ZoomMode" property in WPF but deprecated in Windows Phone. So how could I implement the iamge zooming in the scrollviewer, Could anyone give me a help?

like image 876
HamGuy Avatar asked Jan 23 '13 02:01

HamGuy


1 Answers

xaml code:

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ScrollViewer x:Name="scrl" Height="300" Width="300" BorderBrush="red"
                      BorderThickness="2" VerticalScrollBarVisibility="Disabled">
            <StackPanel>
                <Image x:Name="img" Source="Assets/Mountain.jpg" Height="100" Width="150"
                       RenderTransformOrigin="0.5,0.5">
                    <Image.RenderTransform>
                        <CompositeTransform x:Name="trans"/>
                    </Image.RenderTransform>
                    <toolkit:GestureService.GestureListener>
                        <toolkit:GestureListener PinchStarted="GestureListener_PinchStarted" PinchDelta="GestureListener_PinchDelta" DragDelta="GestureListener_DragDelta"/>
                    </toolkit:GestureService.GestureListener>
                </Image>
            </StackPanel>
        </ScrollViewer>
    </Grid>

cs code:

    double _initialAngle, _initialScale;
    public MainPage()
    {
        InitializeComponent();
        scrl.ManipulationMode = ManipulationMode.Control;
    }

    private void GestureListener_PinchStarted(object sender, PinchStartedGestureEventArgs e)
    {
        _initialAngle = trans.Rotation;
        _initialScale = trans.ScaleX;
    }

    private void GestureListener_PinchDelta(object sender, PinchGestureEventArgs e)
    {
        trans.Rotation = _initialAngle + e.TotalAngleDelta;
        var curZoom = _initialScale * e.DistanceRatio;
        if (curZoom >= 1 && curZoom <= 3)
        {
            trans.ScaleX = _initialScale * e.DistanceRatio;
            trans.ScaleY = _initialScale * e.DistanceRatio;
        }
    }

    private void GestureListener_DragDelta(object sender, DragDeltaGestureEventArgs e)
    {
        trans.TranslateX += e.HorizontalChange;
        trans.TranslateY += e.VerticalChange;
    }

this code works for Scale, Rotate and Zoom.

like image 69
Dev Avatar answered Oct 21 '22 15:10

Dev