public override void OnDestroy() { base.OnDestroy(); // force our image view to release its references to our bitmaps ImageView.SetImageBitmap(null); // free the resources we're done with if (SourceImage != null) { SourceImage.Dispose( ); SourceImage = null; } if (ScaledCroppedImage != null) { ScaledCroppedImage.Dispose( ); ScaledCroppedImage = null; } if (ScaledSourceImage != null) { ScaledSourceImage.Dispose( ); ScaledSourceImage = null; } // free the cropped image if (CroppedImage != null) { CroppedImage.Dispose( ); CroppedImage = null; } SetMode(CropMode.None); }
void SetMode(CropMode mode) { if (mode == Mode) { throw new Exception(string.Format("Crop Mode {0} requested, but already in that mode.", mode)); } switch (mode) { case CropMode.Editing: { // If we're entering edit mode for the first time if (Mode == CropMode.None) { // Animate in the mask SimpleAnimator_Float floatAnimator = new SimpleAnimator_Float(MaskLayer.Opacity, MaskFadeAmount, MaskFadeTime, delegate(float percent, object value) { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { MaskLayer.Opacity = (float)value; }); }, null); floatAnimator.Start( ); // turn on our cropper CropView.Visibility = ViewStates.Visible; // and set the source image to the scaled source. ImageView.SetImageBitmap(ScaledSourceImage); } // else we're coming FROM Preview Mode, so we need to animate else { Animating = true; // setup the dimension changes System.Drawing.SizeF startSize = new System.Drawing.SizeF(ImageView.Width, ImageView.Height); System.Drawing.SizeF endSize = new System.Drawing.SizeF(CropView.Width, CropView.Height); PointF startPos = new PointF(ImageView.GetX( ), ImageView.GetY( )); PointF endPos = new PointF(CropView.GetX( ), CropView.GetY( )); // now animate the cropped image up to its full size AnimateImageView(ImageView, startPos, endPos, startSize, endSize, ImageAnimationTime, delegate { ImageView.SetImageBitmap(null); // release any cropped image we had. if (CroppedImage != null) { CroppedImage.Dispose( ); CroppedImage = null; } // release the scaled version if we had it if (ScaledCroppedImage != null) { ScaledCroppedImage.Dispose( ); ScaledCroppedImage = null; } ImageView.SetImageBitmap(ScaledSourceImage); ImageView.LayoutParameters.Width = ScaledSourceImage.Width; ImageView.LayoutParameters.Height = ScaledSourceImage.Height; // center the image ImageView.SetX((ScreenSize.Width - ImageView.LayoutParameters.Width) / 2); ImageView.SetY((ScreenSize.Height - ImageView.LayoutParameters.Height) / 2); MaskLayer.Visibility = ViewStates.Visible; CropView.Visibility = ViewStates.Visible; SimpleAnimator_Float floatAnimator = new SimpleAnimator_Float(MaskLayer.Opacity, MaskFadeAmount, MaskFadeTime, delegate(float percent, object value) { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { MaskLayer.Opacity = (float)value; CropView.Alpha = percent; }); }, // FINISHED MASK FADE-OUT delegate { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { Animating = false; }); }); floatAnimator.Start( ); }); } break; } case CropMode.Previewing: { // don't allow a state change while we're animating Animating = true; SimpleAnimator_Float floatAnimator = new SimpleAnimator_Float(MaskLayer.Opacity, 1.00f, MaskFadeTime, delegate(float percent, object value) { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { MaskLayer.Opacity = (float)value; CropView.Alpha = 1.0f - percent; }); }, // FINISHED MASK FADE-IN delegate { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { // hide the mask and cropper MaskLayer.Visibility = ViewStates.Gone; CropView.Visibility = ViewStates.Gone; // create the cropped image CroppedImage = CropImage(SourceImage, new System.Drawing.RectangleF(CropView.GetX( ) - CropViewMinPos.X, CropView.GetY( ) - CropViewMinPos.Y, CropView.LayoutParameters.Width, CropView.LayoutParameters.Height)); // create a scaled version of the cropped image float scaledWidth = (float)CroppedImage.Width * (1.0f / ScreenToImageScalar); float scaledHeight = (float)CroppedImage.Height * (1.0f / ScreenToImageScalar); ScaledCroppedImage = Bitmap.CreateScaledBitmap(CroppedImage, (int)scaledWidth, (int)scaledHeight, false); // set the scaled cropped image ImageView.SetImageBitmap(null); ImageView.SetImageBitmap(ScaledCroppedImage); // start the scaled cropped image scaled down further to match its size within the full image. ImageView.SetX(CropView.GetX( )); ImageView.SetY(CropView.GetY( )); ImageView.LayoutParameters.Width = CropView.Width; ImageView.LayoutParameters.Height = CropView.Height; // setup the dimension changes System.Drawing.SizeF startSize = new System.Drawing.SizeF(ScaledCroppedImage.Width, ScaledCroppedImage.Height); System.Drawing.SizeF endSize; if (ScreenSize.Width < ScreenSize.Height) { endSize = new System.Drawing.SizeF(ScreenSize.Width, (float)System.Math.Ceiling(ScreenSize.Width * (startSize.Width / startSize.Height))); } else { endSize = new System.Drawing.SizeF((float)System.Math.Ceiling(ScreenSize.Height * (startSize.Height / startSize.Width)), ScreenSize.Height); } PointF startPos = new PointF(CropView.GetX( ), CropView.GetY( )); PointF endPos = new PointF((ScreenSize.Width - endSize.Width) / 2, (ScreenSize.Height - endSize.Height) / 2); // now animate the cropped image up to its full size AnimateImageView(ImageView, startPos, endPos, startSize, endSize, ImageAnimationTime, delegate { Animating = false; }); }); }); floatAnimator.Start( ); break; } } Mode = mode; }