private void SetRotation(PageOrientation orientation) { if (orientation.HasFlag(PageOrientation.LandscapeLeft)) { BufferRotate = 0; } else if (orientation.HasFlag(PageOrientation.LandscapeRight)) { BufferRotate = 180; } else if (orientation.HasFlag(PageOrientation.PortraitUp)) { //The back camera renders upsidedown so use a different rotate if (Capture != null && Capture.SensorLocation == CameraSensorLocation.Front) { BufferRotate = 270; } else if (Capture != null && Capture.SensorLocation == CameraSensorLocation.Back) { BufferRotate = 90; } else { BufferRotate = 270; } } else if (orientation.HasFlag(PageOrientation.PortraitDown)) { //The back camera renders upsidedown so use a different rotate if (Capture != null && Capture.SensorLocation == CameraSensorLocation.Front) { BufferRotate = 90; } else if (Capture != null && Capture.SensorLocation == CameraSensorLocation.Back) { BufferRotate = 270; } else { BufferRotate = 90; } } if (Preview != null) { Preview.Rotate(BufferRotate); } }
/// <summary> /// Apply orientation specific layout. /// </summary> /// <param name="orientation">Page orientation</param> private void ApplyOrientation(PageOrientation orientation) { if (orientation.HasFlag(PageOrientation.Portrait)) { TitleImage.Visibility = Visibility.Visible; Pivot.Margin = new Thickness(0); } else { TitleImage.Visibility = Visibility.Collapsed; Pivot.Margin = new Thickness(0, -16, 0, 0); } }
/// <summary> /// Handles the OrientationChanged event. /// </summary> private void HandleOrientationChanged(object sender, OrientationChangedEventArgs e) { // Stop/complete Storyboard in case it's active _storyboard.Stop(); HandleStoryboardCompleted(null, null); if (IsAnimationEnabled) { // Capture device width/height var actualWidth = ActualWidth; var actualHeight = ActualHeight; // Get "before" width/height bool normal = _previousOrientation.HasFlag(PageOrientation.Portrait); var width = normal ? actualWidth : actualHeight; var height = normal ? actualHeight : actualWidth; // Capture "before" visuals in a WriteableBitmap var writeableBitmap = new WriteableBitmap((int)width, (int)height); writeableBitmap.Render(this, null); writeableBitmap.Invalidate(); // Create transforms for "before" content var beforeTranslateTransform = new TranslateTransform(); var beforeRotateTransform = new RotateTransform { CenterX = actualWidth / 2, CenterY = actualHeight / 2 }; var beforeTransforms = new TransformGroup(); beforeTransforms.Children.Add(beforeTranslateTransform); beforeTransforms.Children.Add(beforeRotateTransform); // Configure transforms for "before" content var translateDelta = (actualHeight - actualWidth) / 2; var beforeAngle = 0.0; if (_previousOrientation == PageOrientation.LandscapeLeft) { beforeAngle = -90; beforeTranslateTransform.X = -translateDelta; beforeTranslateTransform.Y = translateDelta; } else if (_previousOrientation == PageOrientation.LandscapeRight) { beforeAngle = 90; beforeTranslateTransform.X = -translateDelta; beforeTranslateTransform.Y = translateDelta; } beforeRotateTransform.Angle = -beforeAngle; // Configure for "after" content var afterAngle = 0.0; if (e.Orientation == PageOrientation.LandscapeLeft) { afterAngle = -90; } else if (e.Orientation == PageOrientation.LandscapeRight) { afterAngle = 90; } _afterRotateTransform.CenterX = actualWidth / 2; _afterRotateTransform.CenterY = actualHeight / 2; // Create content with default background and WriteableBitmap overlay for "before" var container = new Grid { Width = width, Height = height, Background = (Brush)Application.Current.Resources["PhoneBackgroundBrush"], RenderTransform = beforeTransforms, IsHitTestVisible = false }; var content = new Rectangle { Fill = new ImageBrush { ImageSource = writeableBitmap, Stretch = Stretch.None } }; container.Children.Add(content); // Configure Popup for displaying "before" content _popup.Child = container; _popup.IsOpen = true; // Update animations to fade from "before" to "after" Storyboard.SetTarget(_beforeOpacityAnimation, container); _beforeOpacityAnimation.Duration = Duration; _beforeOpacityAnimation.EasingFunction = EasingFunction; Storyboard.SetTarget(_afterOpacityAnimation, this); _afterOpacityAnimation.Duration = Duration; _afterOpacityAnimation.EasingFunction = EasingFunction; // Update animations to rotate from "before" to "after" Storyboard.SetTarget(_beforeRotationAnimation, beforeRotateTransform); _beforeRotationAnimation.From = beforeRotateTransform.Angle; _beforeRotationAnimation.To = _beforeRotationAnimation.From + (beforeAngle - afterAngle); _beforeRotationAnimation.Duration = Duration; _beforeRotationAnimation.EasingFunction = EasingFunction; Storyboard.SetTarget(_afterRotationAnimation, _afterRotateTransform); _afterRotationAnimation.From = afterAngle - beforeAngle; _afterRotationAnimation.To = 0; _afterRotationAnimation.Duration = Duration; _afterRotationAnimation.EasingFunction = EasingFunction; // Play the animations _storyboard.Begin(); } // Save current orientation for next time _previousOrientation = e.Orientation; }