c# - How to make to limit the image dragging area in app? -
now, i'm developing photo application(windows phone 8.1 runtime) got problem did not limit image dragging area while photos zooming.
here below code:
<canvas name="zoomgrid" visibility="collapsed"> <image x:name="zoomimages" stretch="fill" width="480" height="800" manipulationdelta="img_intro_manipulationdelta" rendertransformorigin="0.5,0.5" manipulationmode="all"> <image.rendertransform> <compositetransform/> </image.rendertransform> </image> </canvas> double mincale = 0.5; double maxscale = 10.0; private void image_manipulationdelta(object sender, manipulationdeltaroutedeventargs e) { image elemt = sender image; compositetransform transform = elemt.rendertransform compositetransform; transform.scalex *= e.delta.scale; transform.scaley *= e.delta.scale; transform.translatex += e.delta.translation.x; transform.translatey += e.delta.translation.y; if (transform.scalex < mincale) transform.scalex = mincale; if (transform.scaley < mincale) transform.scaley = mincale; if (transform.scalex > maxscale) transform.scalex = maxscale; if (transform.scaley > maxscale) transform.scaley = maxscale; //to limit images dragging did not success. double scalewidth = zoomimages.actualwidth * ct.scalex; double scleheight = zoomimages.actualheight * ct.scaley; double xdiff = math.max(0, (scalewidth - this.content.actualwidth) / 2); double ydiff = math.max(0, (scleheight - this.content.actualheight) / 2); if (math.abs(ct.translatex) > xdiff) ct.translatex = xdiff * math.sign(e.delta.translation.x); if (math.abs(ct.translatey) > ydiff) ct.translatey = ydiff * math.sign(e.delta.translation.y); }
i've achieved using helper method check whether image within bounds of element.
in case, element you'd want check image, , want check it's within bounds of canvas.
private bool iselementvisible(frameworkelement element, frameworkelement container) { if (!element.isvisible) return false; rect bounds = element.transformtoancestor(container).transformbounds(new rect(0.0, 0.0, element.actualwidth, element.actualheight)); rect rect = new rect(0.0, 0.0, container.actualwidth, container.actualheight); if (rect.left > bounds.right || rect.right < bounds.left || rect.bottom < bounds.top || rect.top > bounds.bottom) { return false; } return true; }
in case, i've used matrixtransform instead of compositetransform, method should same.
i create handlers 'manipulationstarting' , 'manipulationcompleted', where:
manipulationstarting:
- stores current (pre-manipulation) copy of transform.
manipulationcompleted:
- check see if image out of bounds (using method i've posted).
- if out of bounds, revert original transform.
this way, if image dragged out of view - user lifts finger, image pop last location.
if you're interested broader context in used code, have wpf behaviour encapsulates functionality. code on codeplex. class may of interest you.
i hope helps :)
Comments
Post a Comment