Example #1
0
 private void touch_start(float x, float y)
 {
     mPath.Reset();
     mPath.MoveTo(x, y);
     mX = x;
     mY = y;
 }
    private void touch_up()
    {
        mPath.LineTo(mX, mY);
        mCanvas.DrawPath(mPath, mPaint);

        // kill this so we don't double draw
        mPath.Reset();
    }
Example #3
0
        public override bool OnTouchEvent(MotionEvent e)
        {
            var touchX = e.GetX();
            var touchY = e.GetY();

            switch (e.Action)
            {
            case MotionEventActions.Down:
                DrawPath.MoveTo(touchX, touchY);
                break;

            case MotionEventActions.Move:
                DrawPath.LineTo(touchX, touchY);
                break;

            case MotionEventActions.Up:
                DrawCanvas.DrawPath(DrawPath, DrawPaint);
                DrawPath.Reset();
                break;

            default:
                return(false);
            }

            Invalidate();

            return(true);
        }
Example #4
0
        private Path CreateMotherPath()
        {
            mMotherOvalPath.Reset();

            mMotherOvalPath.AddOval(new RectF(mMotherPosition[0] - mMotherOvalHalfWidth, mMotherPosition[1] - mMotherOvalHalfHeight, mMotherPosition[0] + mMotherOvalHalfWidth, mMotherPosition[1] + mMotherOvalHalfHeight), Path.Direction.Cw);

            return(mMotherOvalPath);
        }
Example #5
0
        private void TouchUp()
        {
            var left = (_canvasSize.Width - _mutableForeground.Width) / 2;
            var top  = (_canvasSize.Height - _mutableForeground.Height) / 2;

            if (_currentStroke.IsEmpty)
            {
                _strokeBrush.SetStyle(Paint.Style.Fill);
                ForegroundMutator.DrawCircle((float)(_currentTouch.X - left), (float)(_currentTouch.Y - top), (float)StrokeThickness / 2, _strokeBrush);
            }
            else
            {
                _currentStroke.LineTo((float)_currentTouch.X, (float)_currentTouch.Y);
                // Commit the path to our offscreen canvas
                var m = new Matrix();
                m.SetTranslate((float)-left, (float)-top);
                _currentStroke.Transform(m);
                ForegroundMutator.DrawPath(_currentStroke, _strokeBrush);
            }
            // Reset the stroke so that it is not redrawn in OnDraw.
            _currentStroke.Reset();
        }
		void DrawHexagon (Canvas canvas)
		{
			// The extra padding is to avoid edges being clipped
			var padding = (int)TypedValue.ApplyDimension (ComplexUnitType.Dip, 8, Resources.DisplayMetrics);
			var halfHeight = (Height - padding) / 2;
			var side = (Width - padding) / 2;
			var foo = (int)Math.Sqrt (side * side - halfHeight * halfHeight);

			var path = hexagon ?? (hexagon = new Path ());
			hexagon.Reset ();
			path.MoveTo (Width / 2, padding / 2);
			path.RLineTo (-side / 2, 0);
			path.RLineTo (-foo, halfHeight);
			path.RLineTo (foo, halfHeight);
			path.RLineTo (side, 0);
			path.RLineTo (foo, -halfHeight);
			path.RLineTo (-foo, -halfHeight);
			path.Close ();

			var m = transformationMatrix ?? (transformationMatrix = new Matrix ());
			m.Reset ();
			var centerX = Width / 2;
			var centerY = Height / 2;
			m.PostRotate (Rotation, centerX, centerY);
			m.PostScale (ScaleX, ScaleY, centerX, centerY);
			path.Transform (m);

			if (hexagonPaint == null) {
				hexagonPaint = new Paint {
					Color = new Android.Graphics.Color (0x22, 0x76, 0xB9),
					AntiAlias = true,
				};
				hexagonPaint.SetPathEffect (new CornerPathEffect (30));
			}

			canvas.DrawPath (path, hexagonPaint);
		}
		void DrawCross (Canvas canvas)
		{
			var smallSegment = Width / 6;

			var path = cross ?? (cross = new Path ());
			cross.Reset ();

			path.MoveTo (0, 0);
			path.RLineTo (smallSegment, 0);
			path.LineTo (Width / 2, Height / 2);
			path.LineTo (Width - smallSegment, 0);
			path.RLineTo (smallSegment, 0);
			path.LineTo (Width / 2 + smallSegment, Height / 2);
			path.LineTo (Width, Height);
			path.RLineTo (-smallSegment, 0);
			path.LineTo (Width / 2, Height / 2);
			path.LineTo (smallSegment, Height);
			path.RLineTo (-smallSegment, 0);
			path.LineTo (Width / 2 - smallSegment, Height / 2);
			path.Close ();

			if (crossPaint == null) {
				crossPaint = new Paint {
					AntiAlias = true,
					Color = Android.Graphics.Color.White
				};
			}

			canvas.DrawPath (path, crossPaint);
		}
		protected override void OnDraw(Canvas ca)  {
			if (fullImage == null || shouldUpdate) {
				fullImage = Bitmap.CreateBitmap (Width, Height, Bitmap.Config.Argb8888);
				Canvas canvas = new Canvas(fullImage);
				String max = (int)maxY+"";// used to display max
				String min = (int)minY+"";// used to display min
				paint.Reset ();
				Path path = new Path();

				float bottomPadding = 1, topPadding = 0;
				float sidePadding = 10;
				if (this.showMinAndMax)
					sidePadding = txtPaint.MeasureText(max);

				float usableHeight = Height - bottomPadding - topPadding;
				float usableWidth = Width - sidePadding*2;
				float lineSpace = usableHeight/10;

				int lineCount = 0;
				foreach (Line line in lines) {
					int count = 0;
					float lastXPixels = 0, newYPixels;
					float lastYPixels = 0, newXPixels;
					float maxYd = getMaxY ();
					float minYd = getMinY();
					float maxXd = getMaxX();
					float minXd = getMinX();

					if (lineCount == lineToFill){
						paint.Color = Color.White;
						paint.Alpha = 30;	
						paint.StrokeWidth = 2;
						for (int i = 10; i-Width < Height; i = i+20){
							canvas.DrawLine(i, Height-bottomPadding, 0, Height-bottomPadding-i, paint);
						}

						paint.Reset();

						paint.SetXfermode (new PorterDuffXfermode (Android.Graphics.PorterDuff.Mode.Clear));
						foreach (LinePoint p  in line.getPoints()) {
							float yPercent = (p.getY()-minY)/(maxYd - minYd);
							float xPercent = (p.getX()-minX)/(maxXd - minXd);
							if (count == 0){
								lastXPixels = sidePadding + (xPercent*usableWidth);
								lastYPixels = Height - bottomPadding - (usableHeight*yPercent);
								path.MoveTo(lastXPixels, lastYPixels);
							} else {
								newXPixels = sidePadding + (xPercent*usableWidth);
								newYPixels = Height - bottomPadding - (usableHeight*yPercent);
								path.LineTo(newXPixels, newYPixels);
								Path pa = new Path();
								pa.MoveTo(lastXPixels, lastYPixels);
								pa.LineTo(newXPixels, newYPixels);
								pa.LineTo(newXPixels, 0);
								pa.LineTo(lastXPixels, 0);
								pa.Close();
								canvas.DrawPath(pa, paint);
								lastXPixels = newXPixels;
								lastYPixels = newYPixels;
							}
							count++;
						}

						path.Reset();

						path.MoveTo(0, Height-bottomPadding);
						path.LineTo(sidePadding, Height-bottomPadding);
						path.LineTo(sidePadding, 0);
						path.LineTo(0, 0);
						path.Close();
						canvas.DrawPath(path, paint);

						path.Reset();

						path.MoveTo(Width, Height-bottomPadding);
						path.LineTo(Width-sidePadding, Height-bottomPadding);
						path.LineTo(Width-sidePadding, 0);
						path.LineTo(Width, 0);
						path.Close();

						canvas.DrawPath(path, paint);

					}

					lineCount++;
				}

				paint.Reset();

				paint.Color = Color.White;
				//paint.setColor(this.gridColor);
				paint.Alpha = 50;
				paint.AntiAlias = true;
				canvas.DrawLine(sidePadding, Height - bottomPadding, Width, Height-bottomPadding, paint);
				if(this.showHorizontalGrid1)
					for(int i=1;i<=10;i++)
					{
						canvas.DrawLine(sidePadding, Height - bottomPadding-(i*lineSpace), Width, Height-bottomPadding-(i*lineSpace), paint);
					}
				paint.Alpha = 255;

				foreach (Line line in lines) {
					int count = 0;
					float lastXPixels = 0, newYPixels;
					float lastYPixels = 0, newXPixels;
					float maxYd = getMaxY();
					float minYd = getMinY();
					float maxXd = getMaxX();
					float minXd = getMinX();

					paint.Color = Color.Yellow;
					//paint.setColor(line.getColor());
					paint.StrokeWidth = 6;

					foreach (LinePoint p in line.getPoints()) {
						float yPercent = (p.getY()-minY)/(maxYd - minYd);
						float xPercent = (p.getX()-minX)/(maxXd - minXd);
						if (count == 0){
							lastXPixels = sidePadding + (xPercent*usableWidth);
							lastYPixels = Height - bottomPadding - (usableHeight*yPercent);
						} else {
							newXPixels = sidePadding + (xPercent*usableWidth);
							newYPixels = Height - bottomPadding - (usableHeight*yPercent);
							canvas.DrawLine(lastXPixels, lastYPixels, newXPixels, newYPixels, paint);
							lastXPixels = newXPixels;
							lastYPixels = newYPixels;
						}
						count++;
					}
				}


				int pointCount = 0;

				foreach (Line line  in lines) {
					float maxYd = getMaxY();
					float minYd = getMinY();
					float maxXd = getMaxX();
					float minXd = getMinX();

					paint.Color = Color.Yellow;
					//paint.setColor(line.getColor());
					paint.StrokeWidth = 6;
					paint.StrokeCap = Paint.Cap.Round;

					if (line.isShowingPoints()){
						foreach (LinePoint p in line.getPoints()) {
							float yPercent = (p.getY()-minYd)/(maxYd - minYd);
							float xPercent = (p.getX()-minXd)/(maxXd - minXd);
							float xPixels = sidePadding + (xPercent*usableWidth);
							float yPixels = Height - bottomPadding - (usableHeight*yPercent);

							paint.Color = Color.Gray;
							canvas.DrawCircle(xPixels, yPixels, 10, paint);
							paint.Color = Color.White;
							canvas.DrawCircle(xPixels, yPixels, 5, paint);

							Path path2 = new Path();
							path2.AddCircle(xPixels, yPixels, 30, Android.Graphics.Path.Direction.Cw);
							p.setPath(path2);
							p.setRegion(new Region((int)(xPixels-30), (int)(yPixels-30), (int)(xPixels+30), (int)(yPixels+30)));
							if (indexSelected == pointCount && listener != null){
								paint.Color=Color.ParseColor("#33B5E5");
								paint.Alpha = 100;
								canvas.DrawPath(p.getPath(), paint);
								paint.Alpha = 255;
							}

							pointCount++;
						}
					}
				}

				shouldUpdate = false;
				if (this.showMinAndMax) {
					ca.DrawText(max, 0, txtPaint.TextSize, txtPaint);
					ca.DrawText(min,0,this.Height,txtPaint);
				}
			}
			ca.DrawBitmap(fullImage, 0, 0, null);
		}
Example #9
0
 public void Clean()
 {
     path.Reset();
     Invalidate();
 }
Example #10
0
        public override bool OnTouchEvent(MotionEvent e)
        {
            var touchX = e.GetX(0);
            var touchY = e.GetY(0);

            var m     = _transformMatrix;
            var touch = Transform(new PointF(touchX, touchY), m);

            switch (e.Action)
            {
            case MotionEventActions.Down:
                _pointerCount++;
                _lastTouch          = new PointF(touchX, touchY);
                _lastTranslateStart = new PointF(touchX - _previousTranslate.X, touchY - _previousTranslate.Y);


                if (CanvasMode == CanvasMode.Draw && _pointerCount == 1)
                {
                    DrawPath.MoveTo(touch.X, touch.Y);
                }
                else if (CanvasMode == CanvasMode.Pan && _pointerCount == 1 && _scaleListener.ScaleFactor != 1f)
                {
                    // nothing to do here
                }
                else if (CanvasMode == CanvasMode.Select && _pointerCount == 1)
                {
                    if (SelectedElement != null)
                    {
                        _selectedElementFingerOffset   = new PointF();
                        _selectedElementFingerOffset.X = touch.X - SelectedElement.Position.X;
                        _selectedElementFingerOffset.Y = touch.Y - SelectedElement.Position.Y;
                    }
                }
                break;

            case MotionEventActions.Move:
                if (CanvasMode == CanvasMode.Draw && _pointerCount == 1)
                {
                    DrawPath.LineTo(touch.X, touch.Y);
                }
                else if (CanvasMode == CanvasMode.Pan && _pointerCount == 1 && _scaleListener.ScaleFactor != 1f)
                {
                    _translate.X = e.GetX() - _lastTranslateStart.X;
                    _translate.Y = e.GetY() - _lastTranslateStart.Y;
                }
                else if (CanvasMode == CanvasMode.Select && _pointerCount == 1)
                {
                    if (SelectedElement != null)
                    {
                        // TODO: Take distance between finger and element into account
                        var x = touch.X - _selectedElementFingerOffset.X;
                        var y = touch.Y - _selectedElementFingerOffset.Y;
                        SelectedElement.Position = new System.Drawing.PointF(x, y);
                    }
                }
                break;

            case MotionEventActions.Up:
                if (CanvasMode == CanvasMode.Draw && _pointerCount == 1)
                {
                    DrawCanvas.DrawPath(DrawPath, DrawPaint);
                    DrawPath.Reset();
                }
                else if (CanvasMode == CanvasMode.Pan && _pointerCount == 1 && _scaleListener.ScaleFactor != 1f)
                {
                    _previousTranslate = _translate;
                }
                else if (CanvasMode == CanvasMode.Add && _pointerCount == 1)
                {
                    OnElementAdded(new System.Drawing.PointF(touch.X, touch.Y));
                    ////_elements.Add(new AndroidCircleElement(touch.X, touch.Y, 50, new Paint(DrawPaint)));
                    //Elements.Add(new AndroidRectangleElement(touch.X, touch.Y, 50, 80, new Paint(DrawPaint)));
                }
                else if (CanvasMode == CanvasMode.Select && _pointerCount == 1)
                {
                    if (SelectedElement == null)
                    {
                        // do some primitive hittesting
                        // reverse order as last drawn element is the topmos
                        for (int i = Elements.Count - 1; i >= 0; i--)
                        {
                            var elem = Elements[i];
                            if (elem.IsBelow(touch))
                            {
                                OnSelectElement(elem);
                                break;
                            }
                        }
                    }
                    else
                    {
                        OnSelectElement(null);
                    }
                }
                _pointerCount--;
                break;

            case MotionEventActions.Pointer1Down:
                _pointerCount++;
                break;

            case MotionEventActions.Pointer1Up:
                _pointerCount--;
                break;

            case MotionEventActions.Pointer2Down:
                _pointerCount++;
                _lastTouch2.X = e.GetX(1);
                _lastTouch2.Y = e.GetY(1);

                //_center.X = (_lastTouch.X + _lastTouch2.X)/2;
                //_center.Y = (_lastTouch.Y + _lastTouch2.Y)/2;
                var lt1 = Transform(_lastTouch, m);
                var lt2 = Transform(_lastTouch2, m);
                _center.X = (lt1.X + lt2.X) / 2;
                _center.Y = (lt1.Y + lt2.Y) / 2;


                var dist = Math.Sqrt(Math.Pow(_lastTouch2.X - _lastTouch.X, 2) + Math.Pow(_lastTouch2.Y - _lastTouch.Y, 2));
                if (dist < 10f)
                {
                    _pointerCount--;
                }

                break;

            case MotionEventActions.Pointer2Up:
                _pointerCount--;
                break;

            case MotionEventActions.Pointer3Down:
                _pointerCount++;
                break;

            case MotionEventActions.Pointer3Up:
                _pointerCount--;
                break;

            default:
                return(false);
            }

            if (_pointerCount == 2)
            {
                _scaleDetector.OnTouchEvent(e);
                if (_scaleListener.ScaleFactor == 1f)
                {
                    _translate         = new PointF();
                    _previousTranslate = new PointF();
                }
            }

            if (CanvasMode != CanvasMode.Select)
            {
                SelectedElement = null;
            }
            //// The only time we want to re-draw the canvas is if we are panning (which happens when the mode is
            //// DRAG and the zoom factor is not equal to 1) or if we're zooming
            //if((_pointerCount == 1 && CanvasMode == CanvasMode.Pan && _scaleListener.ScaleFactor != 1f) || _pointerCount == 2)
            Invalidate();

            return(true);
        }