Exemplo n.º 1
0
        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)
            {
                cropHeight = (_aspectX > _aspectY)
                                        ? cropWidth * _aspectY / _aspectX
                                        : 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);
        }
Exemplo n.º 2
0
        // Pan the displayed image to make sure the cropping rectangle is visible.
        private void EnsureVisible(HighlightView hv)
        {
            var panDeltaX1 = Math.Max(0, IvLeft - hv.DrawRect.Left);
            var panDeltaX2 = Math.Min(0, IvRight - hv.DrawRect.Right);

            var panDeltaY1 = Math.Max(0, IvTop - hv.DrawRect.Top);
            var panDeltaY2 = Math.Min(0, IvBottom - hv.DrawRect.Bottom);

            var panDeltaX = panDeltaX1 != 0 ? panDeltaX1 : panDeltaX2;
            var panDeltaY = panDeltaY1 != 0 ? panDeltaY1 : panDeltaY2;

            if (panDeltaX != 0 || panDeltaY != 0)
            {
                PanBy(panDeltaX, panDeltaY);
            }
        }
Exemplo n.º 3
0
        // 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)
        {
            float width  = hv.DrawRect.Width();
            float height = hv.DrawRect.Height();

            float thisWidth  = Width;
            float thisHeight = Height;

            var z1 = thisWidth / width * .6F;
            var z2 = thisHeight / height * .6F;

            var zoom = Math.Max(1F, ((Math.Min(z1, z2)) * GetScale()));

            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);
        }
Exemplo n.º 4
0
 public void AddHighlightView(HighlightView hv)
 {
     _hightlightViews.Add(hv);
     Invalidate();
 }
Exemplo n.º 5
0
        public override bool OnTouchEvent(MotionEvent e)
        {
            var cropImage = (CropImageActivity)_context;

            if (cropImage.Saving)
            {
                return(false);
            }

            switch (e.Action)
            {
            case MotionEventActions.Down:
                foreach (var hv in _hightlightViews)
                {
                    var edge = hv.GetHit(e.GetX(), e.GetY());
                    if (edge != HighlightView.HitPosition.None)
                    {
                        _motionEdge           = edge;
                        _mMotionHighlightView = hv;
                        _mLastX = e.GetX();
                        _mLastY = e.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, (e.GetX() - _mLastX), (e.GetY() - _mLastY));
                    _mLastX = e.GetX();
                    _mLastY = e.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 (e.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;
            }

            return(true);
        }