How to help move the image does not exceed the limit on screens

こ雲淡風輕ζ 提交于 2020-01-23 17:13:47

问题


I can drag this pictures to right or left,up,down It passed over an screens.I had zoom in and out. I want limit about that. Using windows phone 8.1 app. After the transformation, brings the image back if it is out of the boundary. You can detect if the image is out of boundary by comparing the values of TranslateX/TranslateY and the width/height of the boundary. The boundary is the parent of the image (it is a Grid?), you need to debug the code to determine the boundary for TranslateX and 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:


you can check for which resolution is there , according to set Image height and width.

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");
            }

Plz refer this link for more info http://msdn.microsoft.com/en-us/library/windows/apps/jj206974%28v=vs.105%29.aspx



来源:https://stackoverflow.com/questions/26458205/how-to-help-move-the-image-does-not-exceed-the-limit-on-screens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!