public void AddHighlightView(HighlightView hv) { try { _hightlightViews.Add(hv); Invalidate(); } catch (Exception) { } }
// If the cropping rectangle's size changed significantly, change the // view's center and scale according to the cropping rectangle. private void CenterBasedOnHighlightView(HighlightView hv) { try { var drawRect = hv.DrawRect; float width = drawRect.Width(); float height = drawRect.Height(); float thisWidth = Width; float thisHeight = Height; var z1 = thisWidth / width * .6F; var z2 = thisHeight / height * .6F; var zoom = Math.Min(z1, z2); zoom = zoom * GetScale(); zoom = Math.Max(1F, zoom); if ((Math.Abs(zoom - GetScale()) / zoom) > .1) { float[] coordinates = { hv.CropRect.CenterX(), hv.CropRect.CenterY() }; ImageMatrix.MapPoints(coordinates); ZoomTo(zoom, coordinates[0], coordinates[1], 300F); } EnsureVisible(hv); } catch (Exception) { } }
// Pan the displayed image to make sure the cropping rectangle is visible. private void EnsureVisible(HighlightView hv) { try { var r = hv.DrawRect; var panDeltaX1 = Math.Max(0, IvLeft - r.Left); var panDeltaX2 = Math.Min(0, IvRight - r.Right); var panDeltaY1 = Math.Max(0, IvTop - r.Top); var panDeltaY2 = Math.Min(0, IvBottom - r.Bottom); var panDeltaX = panDeltaX1 != 0 ? panDeltaX1 : panDeltaX2; var panDeltaY = panDeltaY1 != 0 ? panDeltaY1 : panDeltaY2; if (panDeltaX != 0 || panDeltaY != 0) { PanBy(panDeltaX, panDeltaY); } } catch (Exception) { } }
private void AddHighlightView() { Crop = new HighlightView(_imageView); var width = _bitmap.Width; var height = _bitmap.Height; var imageRect = new Rect(0, 0, width, height); // make the default size about 4/5 of the width or height var cropWidth = Math.Min(width, height) * 4 / 5; var cropHeight = cropWidth; if (_aspectX != 0 && _aspectY != 0) { if (_aspectX > _aspectY) { cropHeight = cropWidth * _aspectY / _aspectX; } else { cropWidth = cropHeight * _aspectX / _aspectY; } } var x = (width - cropWidth) / 2; var y = (height - cropHeight) / 2; var cropRect = new RectF(x, y, x + cropWidth, y + cropHeight); Crop.Setup(_imageView.ImageMatrix, imageRect, cropRect, _aspectX != 0 && _aspectY != 0); _imageView.ClearHighlightViews(); Crop.Focused = true; _imageView.AddHighlightView(Crop); }
public override bool OnTouchEvent(MotionEvent ev) { try { var cropImage = (CropImage)_context; if (cropImage.Saving) { return(false); } switch (ev.Action) { case MotionEventActions.Down: foreach (var hv in _hightlightViews) { var edge = hv.GetHit(ev.GetX(), ev.GetY()); if (edge != HighlightView.HitPosition.None) { _motionEdge = edge; _mMotionHighlightView = hv; _mLastX = ev.GetX(); _mLastY = ev.GetY(); _mMotionHighlightView.Mode = (edge == HighlightView.HitPosition.Move) ? HighlightView.ModifyMode.Move : HighlightView.ModifyMode.Grow; break; } } break; case MotionEventActions.Up: if (_mMotionHighlightView != null) { CenterBasedOnHighlightView(_mMotionHighlightView); _mMotionHighlightView.Mode = HighlightView.ModifyMode.None; } _mMotionHighlightView = null; break; case MotionEventActions.Move: if (_mMotionHighlightView != null) { _mMotionHighlightView.HandleMotion(_motionEdge, ev.GetX() - _mLastX, ev.GetY() - _mLastY); _mLastX = ev.GetX(); _mLastY = ev.GetY(); if (true) { // This section of code is optional. It has some user // benefit in that moving the crop rectangle against // the edge of the screen causes scrolling but it means // that the crop rectangle is no longer fixed under // the user's finger. EnsureVisible(_mMotionHighlightView); } } break; } switch (ev.Action) { case MotionEventActions.Up: Center(true, true); break; case MotionEventActions.Move: // if we're not zoomed then there's no point in even allowing // the user to move the image around. This call to center puts // it back to the normalized location (with false meaning don't // animate). if (Math.Abs(GetScale() - 1F) < double.Epsilon) { Center(true, true); } break; } } catch (Exception) { } return(true); }