How to draw a lines between points and pull those points?

前端 未结 4 765
臣服心动
臣服心动 2020-12-30 09:36

I want to draw the lines between points on the view, and then pull those points upto desired positions even the shape will change.

i know how to draw the line betwe

4条回答
  •  隐瞒了意图╮
    2020-12-30 10:11

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Paint.Style;
    import android.graphics.PointF;
    import android.view.MotionEvent;
    import android.view.View;
    
    public class TestView extends View
    {
    
        private Paint paint;
        private PointF startPoint, endPoint;
        private boolean isDrawing;
    
        public TestView(Context context)
        {
            super(context);
            init();
        }
    
        private void init()
        {
            paint = new Paint();
            paint.setColor(Color.RED);
            paint.setStyle(Style.STROKE);
            paint.setStrokeWidth(2);
            paint.setAntiAlias(true);
        }
    
        @Override
        protected void onDraw(Canvas canvas)
        {
            if(isDrawing)
            {
                canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, paint);
            }
        }
    
    
        @Override
        public boolean onTouchEvent(MotionEvent event)
        {
            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    startPoint = new PointF(event.getX(), event.getY());
                    endPoint = new PointF();
                    isDrawing = true;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if(isDrawing)
                    {
                        endPoint.x = event.getX();
                        endPoint.y = event.getY();
                        invalidate();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if(isDrawing)
                    {
                        endPoint.x = event.getX();
                        endPoint.y = event.getY();
                        isDrawing = false;
                        invalidate();
                    }
                    break;
                default:
                    break;
            }
            return true;
        }
    }
    

提交回复
热议问题