如何不将图像拖出框架? [英] How to not drag an Image out of the frame?

查看:109
本文介绍了如何不将图像拖出框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到我需要的属性或对象..

I cannot find the property or the object that I need..

我已经实现了一个可拖动和可缩放的图像,但我可以将它拖出屏幕,所以我从图像中丢失了我的图像。

I have implemented a draggable and zoomable image but I can drag it out of the screen, so I lose my Image out of the view.

我应该将图像放在哪里以便将其放在一个盒子里(并且可能具有良好的弹跳效果)?

Where should I put my image to keep it inside a box (and, possibly with the nice bouncing effect)?

编辑:

实际上我的图像必须保留在堆叠面板中,只有这种可能性放大。
(最大缩小为第一次渲染)。

Pratically my image has to remain in the stackpanel, with only the possibility of zoom-in. (max zoom-out as the first rendering).

一些代码:

    private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e)
    {
        initialAngle = compositeTransform.Rotation;
        initialScale = compositeTransform.ScaleX;
    }

    private void OnPinchDelta(object sender, PinchGestureEventArgs e)
    {
        compositeTransform.ScaleX = initialScale * e.DistanceRatio;
        compositeTransform.ScaleY = initialScale * e.DistanceRatio;
    }

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

    <StackPanel x:Name="container">
        <Image x:Name="image_chart">
            <Image.RenderTransform>
                <TransformGroup>
                    <ScaleTransform x:Name="scale" />
                    <TranslateTransform x:Name="transform" />
                    <CompositeTransform x:Name="compositeTransform"/>
                </TransformGroup>
            </Image.RenderTransform>
            <toolkit:GestureService.GestureListener>
                <toolkit:GestureListener PinchStarted="OnPinchStarted" PinchDelta="OnPinchDelta"
                                         DragDelta="OnDragDelta"/>
            </toolkit:GestureService.GestureListener>
        </Image>
    </StackPanel>

编辑2 - 半答案

我终于找到了如何以原始尺寸停止缩小!

I've finally find out how to stop the zoom-out at the original size!

private void OnPinchDelta(object sender, PinchGestureEventArgs e)
    {
        if (1.0 <= (initialScale * e.DistanceRatio))
        {
            compositeTransform.ScaleX = initialScale * e.DistanceRatio;
            compositeTransform.ScaleY = initialScale * e.DistanceRatio;
        }
    }

If条件意味着:如果我正在放大 - >没问题,因为e.DistanceRatio> 1。如果我缩小,我将停止,直到initialScale将是相同的!

The If condition means: if I'm zooming in -> no problem because the e.DistanceRatio is >1. If I'm zooming out I will stop until the initialScale will be the same!

现在我仍然需要帮助避免外面的拖动。

Now I still need help on how avoid the Drag outside.

推荐答案

好的,我找到了解决方案,但我需要一些改进。
缩放工作,但当图像在右边时,它全部在左边(因为它从top_left角开始缩放..

Ok, I find out the solution, but I need some improvement. The scaling work but when the image is on the right it goes all on the left (because it's scaling starting from the top_left corner..

这是代码对于阻塞拖动:

Here's the code for the "blocking drag":

private void OnDragDelta(object sender, DragDeltaGestureEventArgs e)
    {

        double realWidth = image_chart.ActualWidth*compositeTransform.ScaleX;
        double realHeight = image_chart.ActualHeight * compositeTransform.ScaleY;

        if(compositeTransform.TranslateX>=0)
           compositeTransform.TranslateX = Math.Max(container.ActualWidth - realWidth,
                                                 Math.Min(0, compositeTransform.TranslateX + e.HorizontalChange));
        else
            compositeTransform.TranslateX = Math.Max(container.ActualWidth - realWidth,
                                                     Math.Min(0, compositeTransform.TranslateX + e.HorizontalChange));
        if(compositeTransform.TranslateY>=0)
           compositeTransform.TranslateY = Math.Max(container.ActualHeight - realHeight,
                                                 Math.Min(0, compositeTransform.TranslateY + e.VerticalChange));
        else
            compositeTransform.TranslateY = Math.Max(container.ActualHeight - realHeight,
                                                     Math.Min(0, compositeTransform.TranslateY + e.VerticalChange));

编辑:

最后我决定使用WebBrowser ..更流畅和愉快!

In the end I've decide to use the WebBrowser.. much more "smoother" and enjoyable!

这篇关于如何不将图像拖出框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆