如何帮助移动图像不超过屏幕上的限制

时间:2014-10-20 03:31:02

标签: c# wpf xaml windows-phone-8 windows-phone-8.1

我可以将这些图片向右或向左,向上,向下拖动它经过一个屏幕。我放大了。我想要限制。使用Windows Phone 8.1应用程序。 转换后,如果图像超出边界,则将图像带回。您可以通过比较TranslateX / TranslateY的值和边界的宽度/高度来检测图像是否超出边界。边界是图像的父级(它是网格?),您需要调试代码以确定TranslateX和TranslateY的边界。

XAML

<Grid>
    <Grid Name="container">
        <Image Name="img_container" Source="/papers.co-mb00-baloon-fly-sea-wallpaper-1920x1080.jpg"
               ManipulationDelta="Image_ManipulationDelta"
               ManipulationMode="Scale,TranslateX,TranslateY"
               Stretch="Uniform"
                RenderTransformOrigin="0.5,0.5">
            <Image.RenderTransform>
                <CompositeTransform/>
            </Image.RenderTransform>
        </Image>
    </Grid>
</Grid>

C#

int mincale = 1;
private void Image_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    Image img = sender as Image;
    CompositeTransform ct = img.RenderTransform as CompositeTransform;
    //zoom
    ct.ScaleX *= e.Delta.Scale;
    ct.ScaleY *= e.Delta.Scale;

    //checking

    if (ct.ScaleX < mincale)
        ct.ScaleX = mincale;
    if (ct.ScaleY < mincale)
        ct.ScaleY = mincale;

    //drag whitle zooming.

    ct.TranslateX += e.Delta.Translation.X;
    ct.TranslateY += e.Delta.Translation.Y;

    //checking drag not passed over a screen.that's fails.

    if (ct.TranslateX < 10 - img.ActualWidth * ct.ScaleX)
        ct.TranslateX = 10 - img.ActualWidth * ct.ScaleX;

    if (ct.TranslateX > container.ActualWidth - 10)
        ct.TranslateX = container.ActualWidth - 10;

    if (ct.TranslateY < 10 - img.ActualHeight * ct.ScaleY)
        ct.TranslateY = 10 - img.ActualHeight * ct.ScaleY;

    if (ct.TranslateY > container.ActualHeight - 10)
        ct.TranslateY = container.ActualHeight - 10;
}

1 个答案:

答案 0 :(得分:0)

根据设置的图像高度和宽度,您可以检查哪个分辨率。

&#13;
&#13;
var scalfactor = App.Current.Host.Content.ScaleFactor;
switch (scalfactor)
            {
                case 150:
//write code for 720p or other screen Resolution

                  
                    break;
                case 160:
//write code for Wxga screen Resolution
          
                    break;
                case 100:
//write code for Wvga screen Resolution
              break;
                default:
                    throw new InvalidOperationException("Unknown resolution type");
            }
&#13;
&#13;
&#13;

请参阅此链接以获取更多信息 http://msdn.microsoft.com/en-us/library/windows/apps/jj206974%28v=vs.105%29.aspx