首页 新闻 会员 周边 捐助

图片旋转后无法缩放(WP7)

0
悬赏园豆:15 [已关闭问题] 关闭于 2012-08-14 09:55

各位同仁,大家好,我这里遇到一个问题:

我参照这个例子做了一个简单的照片缩放功能,本来一切正常,但是当我旋转屏屏幕后,无法正常缩放照片了。单步调试跟踪了好长时间,终于发现问题所在:原来是添加了旋转的缘故,把

preImage.RenderTransform= rt; 这句注释掉就好了,但是没就没旋转动画效果了。下面是我的代码:

Xmal:

<Image x:Name="preImage" CacheMode="BitmapCache" Height="Auto" Width="Auto">
                <Image.RenderTransform>
                    <CompositeTransform x:Name="imageTranform" TranslateX="0" TranslateY="0" ScaleX="0" SkewY="0"/>
                </Image.RenderTransform>
                <toolkit:GestureService.GestureListener>
                    <toolkit:GestureListener PinchStarted="GestureListener_PinchStarted"
                                             PinchDelta="GestureListener_PinchDelta" 
                                             DoubleTap="GestureListener_DoubleTap"
                                             DragStarted="GestureListener_DragStarted"
                                             DragDelta="GestureListener_DragDelta"
                                             Flick="GestureListener_Flick"/>
                </toolkit:GestureService.GestureListener>
            </Image>

 

cs代码,大致与例子中的相同,根据实际需要做了一些微小的改动。
 void ImagePreview_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            Duration duration = new Duration(TimeSpan.FromSeconds(0.7));
            Storyboard sb = new Storyboard();
            sb.Duration = duration;

            DoubleAnimation da = new DoubleAnimation();
            da.Duration = duration;

            sb.Children.Add(da);

            RotateTransform rt = new RotateTransform();

            Storyboard.SetTarget(da, rt);
            Storyboard.SetTargetProperty(da, new PropertyPath("Angle"));

            if(e.Orientation == PageOrientation.Landscape ||
                e.Orientation ==PageOrientation.LandscapeLeft || 
                e.Orientation == PageOrientation.LandscapeRight)
            {
                preImage.Stretch = Stretch.UniformToFill;
                da.From = 90;
                da.To = 0;
            }
            else if(e.Orientation == PageOrientation.Portrait ||
                e.Orientation == PageOrientation.PortraitDown ||
                e.Orientation == PageOrientation.PortraitUp)
            {
                preImage.Stretch = Stretch.Uniform;
 
                da.From = 270;
                da.To = 360;
            }
            sb.Begin();
            preImage.RenderTransform = rt;
            preImage.RenderTransformOrigin = new Point(0.5, 0.5);//(0.5,0.5) 将使呈现转换位于元素中心
        }        

private void GestureListener_PinchStarted(object sender, PinchStartedGestureEventArgs e)
        {
            _oldFinger1 = e.GetPosition(preImage, 0);
            _oldFinger2 = e.GetPosition(preImage, 1);
            _oldScaleFactor = 1;
        }

        private void GestureListener_PinchDelta(object sender, PinchGestureEventArgs e)
        {
            var scaleFactor = e.DistanceRatio / _oldScaleFactor;
           
            var currentFinger1 = e.GetPosition(preImage, 0);
            var currentFinger2 = e.GetPosition(preImage, 1);
            
            Point tranlationDelta;
            tranlationDelta = GetTranslationDelta(currentFinger1, currentFinger2, _oldFinger1, _oldFinger2, _imagePosition, scaleFactor);

            _oldFinger1 = currentFinger1;
            _oldFinger2 = currentFinger2;
            _oldScaleFactor = e.DistanceRatio;
          
            UpdateImage(scaleFactor, tranlationDelta);
        }

               private Point GetTranslationDelta(Point currentFinger1, Point currentFinger2,Point oldFinger1,
            Point oldFinger2,Point currentPosition, double scaleFactor)
        {
            var newPos1 = new Point(
                currentFinger1.X + (currentPosition.X - oldFinger1.X) * scaleFactor,
                currentFinger1.Y + (currentPosition.Y - oldFinger1.Y) * scaleFactor);

            var newPos2 = new Point(
                currentFinger2.X + (currentPosition.X - oldFinger2.X) * scaleFactor,
                currentFinger2.Y + (currentPosition.Y - oldFinger2.Y) * scaleFactor);

            var newPos = new Point(
                (newPos1.X + newPos2.X) / 2,
                (newPos1.Y + newPos2.Y) / 2);

            return new Point(
                newPos.X - currentPosition.X,
                newPos.Y - currentPosition.Y);
        }

        private void UpdateImage(double scaleFactor, Point delta)
        {
            _totalImageScale *= scaleFactor;
            _imagePosition = new Point(_imagePosition.X + delta.X, _imagePosition.Y + delta.Y);

            if (_totalImageScale > 1)
            {
                imageTranform.ScaleX = _totalImageScale;
                imageTranform.ScaleY = _totalImageScale;
                imageTranform.TranslateX = _imagePosition.X;
                imageTranform.TranslateY = _imagePosition.Y;
            }
            else
            {
                imageTranform.ScaleX = imageTranform.ScaleY = 1;
                imageTranform.TranslateX = imageTranform.TranslateY = 0;
                _imagePosition = new Point(0, 0);
            }
        }
我还在codeproject发现了这篇文章,作者说为了能在landscape和portrait两种模式下都能进行缩放操作,所以他必须知道image控件的width和height属性值(Now notice the OrientationChanged event of the Silverlight page is here, the reason is that I want the user to be able to use the zoom and panning either in portrait or landscape mode, but for that, I do really need Width andHeight of the Image control to be set),但是没说明为什么,我想了大半天也没想出个所以然来,所以在此发帖请教各位,还望各位不吝赐教,非常感谢!
吹牛的啤酒的主页 吹牛的啤酒 | 初学一级 | 园豆:199
提问于:2012-08-13 15:48
< >
分享
所有回答(1)
0

在一个国际友人的帮助下解决了:将旋转动画rotatetransform替换为compositetransform即可

吹牛的啤酒 | 园豆:199 (初学一级) | 2012-08-14 09:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册