private void AddHighlightView(Bitmap bitmap) { if (bitmap == null) return; highlightView = new HighlightView(this); int width = bitmap.Width; int height = bitmap.Height; Rect imageRect = new Rect(0, 0, width, height); // make the default size about 4/5 of the width or height int cropWidth = width * 4 / 5; //Math.Min(width, height) * 4 / 5; int cropHeight = height * 4 / 5; if (OutputWidth != 0 && OutputHeight != 0) { if(OutputWidth < OutputHeight) { var aspectRatio = (float)OutputWidth / (float)OutputHeight; cropWidth = (int)(cropHeight * aspectRatio); } else { var aspectRatio = (float)OutputHeight / (float)OutputWidth; cropHeight = (int)(cropWidth * aspectRatio); } } int x = (width - cropWidth) / 2; int y = (height - cropHeight) / 2; RectF cropRect = new RectF(x, y, x + cropWidth, y + cropHeight); highlightView.Setup(this.ImageMatrix, imageRect, cropRect, OutputWidth != 0 && OutputHeight != 0); this.ClearHighlightViews(); highlightView.Focused = true; this.AddHighlightView(highlightView); Center(true, true); }
// 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) { Rect drawRect = hv.DrawRect; float width = drawRect.Width(); float height = drawRect.Height(); float thisWidth = Width; float thisHeight = Height; thisWidth = _width; thisHeight = _height; float z1 = thisWidth / width * .6F; float z2 = thisHeight / height * .6F; float zoom = Math.Min(z1, z2); zoom = zoom * this.GetScale(); zoom = Math.Max(1F, zoom); if ((Math.Abs(zoom - GetScale()) / zoom) > .1) { float[] coordinates = new float[] { hv.CropRect.CenterX(), hv.CropRect.CenterY() }; ImageMatrix.MapPoints(coordinates); } ensureVisible(hv); }
private void AddHighlightView(HighlightView hv) { hightlightViews.Add(hv); Invalidate(); }
// Pan the displayed image to make sure the cropping rectangle is visible. private void ensureVisible(HighlightView hv) { Rect r = hv.DrawRect; int panDeltaX1 = Math.Max(0, IvLeft - r.Left); int panDeltaX2 = Math.Min(0, IvRight - r.Right); int panDeltaY1 = Math.Max(0, IvTop - r.Top); int panDeltaY2 = Math.Min(0, IvBottom - r.Bottom); int panDeltaX = panDeltaX1 != 0 ? panDeltaX1 : panDeltaX2; int panDeltaY = panDeltaY1 != 0 ? panDeltaY1 : panDeltaY2; if (panDeltaX != 0 || panDeltaY != 0) { PanBy(panDeltaX, panDeltaY); } }
public override bool OnTouchEvent(MotionEvent ev) { if (saving) return false; _scaleDetector.OnTouchEvent(ev); switch (ev.Action) { case MotionEventActions.Down: for (int i = 0; i < hightlightViews.Count; i++) { HighlightView hv = hightlightViews[i]; var edge = hv.GetHit(ev.GetX(), ev.GetY()); if (edge != global::Plugin.ImageCrop.HighlightView.HitPosition.None) { motionEdge = edge; mMotionHighlightView = hv; mLastX = ev.GetX(); mLastY = ev.GetY(); mMotionHighlightView.Mode = (edge == global::Plugin.ImageCrop.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; isScaling = false; break; case MotionEventActions.Move: if (!isScaling && 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 (GetScale() == 1F) { Center(true, true); } break; } return true; }