如何在 Android Java 中找到用油漆线绘制的长度?

How to find drawed with paint line's length in Android Java?

这是我用手指画线和画圆的代码。我想知道如何找到线的长度并想用半径线的长度画圆。

我该怎么做?

这条线放在哪里?

circlePath.addCircle(mX, mY, "line's length", Path.Direction.CW);

public class MainActivity extends Activity  {
    /** Called when the activity is first created. */
    Paint mPaint;

    float Mx1,My1;
    float x,y;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);
        MyView view1 =new MyView(this);
        view1.setBackgroundResource(R.color.colorPrimary);
        setContentView(view1);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(10);

    }

    public class MyView extends View {

        private static final float MINP = 0.25f;
        private static final float MAXP = 0.75f;

        private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;
        private Paint circlePaint;
        private Path circlePath;
        public MyView(Context c) {
            super(c);

            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);

            circlePaint = new Paint();
            circlePath = new Path();
            circlePaint.setAntiAlias(true);
            circlePaint.setColor(Color.BLUE);
            circlePaint.setStyle(Paint.Style.STROKE);
            circlePaint.setStrokeJoin(Paint.Join.MITER);
            circlePaint.setStrokeWidth(8f);

        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
        }

        @Override
        protected void onDraw(Canvas canvas) {


            canvas.drawColor(Color.WHITE);
            //canvas.drawLine(mX, mY, Mx1, My1, mPaint);
            //canvas.drawLine(mX, mY, x, y, mPaint);
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            canvas.drawPath(mPath, mPaint);

            canvas.drawPath(circlePath, circlePaint);

        }

        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

        private void touch_start(float x, float y) {
            mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
        }
        private void touch_move(float x, float y) {

            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                //mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                mX = x;
                mY = y;
            }

            //circlePath.reset();
            //circlePath.addCircle(mX, mY, Math.abs(mX - mY), Path.Direction.CW);

        }
        private void touch_up() {
            mPath.lineTo(mX, mY);

            //circlePath.reset();
            // commit the path to our offscreen
            mCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw
            mPath.reset();
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    touch_move(x, y);
                    circlePath.addCircle(mX, mY, x, Path.Direction.CW);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up();
                    //Mx1=(int) event.getX();
                    //My1= (int) event.getY();
                    invalidate();
                    break;
            }
            return true;
        }

    }


}

我解决了

float first_x, first_y, last_x, last_y, line_lenght;
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    first_x = x;
                    first_y = y;

                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    touch_move(x, y);
                    last_x = x;
                    last_y = y;
                    line_lenght = (float) Math.pow(Math.abs(first_x-last_x),2) + (float) Math.pow(Math.abs(first_y-last_y),2);
                    line_lenght = (float) Math.sqrt(line_lenght);
                    circlePath.reset();
                    circlePath.addCircle(mX, mY, line_lenght, Path.Direction.CW);

                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:

                    last_x = x;
                    last_y = y;
                    line_lenght = (float) Math.pow(Math.abs(first_x-last_x),2) + (float) Math.pow(Math.abs(first_y-last_y),2);
                    line_lenght = (float) Math.sqrt(line_lenght);
                    circlePath.reset();
                    circlePath.addCircle(mX, mY, line_lenght, Path.Direction.CW);

                    touch_up();


                    //Mx1=(int) event.getX();
                    //My1= (int) event.getY();
                    invalidate();
                    break;
            }
            return true;
        }