Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning a path.lineTo()

I'm currently learning Canvas on Android Studio and I'm not so sure about using the right title, but I'm stuck on how to control the triangle position when dragging the Red dot using Seekbar. below are the details

Below are the codes that I've successfully drawn a triangle.

        @Override
        protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setColor(Color.RED);
        canvas.drawRoundRect(100, 300, 900, 700, 50, 50,paint);

        Point pointTop = new Point(250, 400);
        Point pointLeft = new Point(150, 150);
        Point pointRight = new Point(350, 150);


        paint.setColor(Color.YELLOW);
        path.setFillType(Path.FillType.EVEN_ODD);
        path.lineTo(pointTop.x, pointTop.y);
        path.lineTo(pointLeft.x, pointLeft.y);
        path.lineTo(pointRight.x, pointRight.y);
        path.lineTo(pointTop.x, pointTop.y);
        path.close();

        canvas.drawPath(path, paint);
        }

Image result:

enter image description here

Expected result

Note: there are only 1 Red dot and 1 triangle.

enter image description here

much appreciated =)

like image 851
Arduino Avatar asked Nov 24 '25 19:11

Arduino


1 Answers

I've solved my problem after reading through the documents as below link and added the codes

https://developer.android.com/reference/android/graphics/Path

Enjoy :)

        @Override
        protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setColor(Color.RED);
        canvas.drawRoundRect(100, 300, 900, 700, 50, 50,paint);

        Point pointTop = new Point(250, 400);
        Point pointLeft = new Point(150, 150);
        Point pointRight = new Point(350, 150);


        paint.setColor(Color.YELLOW);
--------------------- added -------------------------------
        path.moveTo(pointLeft.x, pointLeft.y);
        path.moveTo(pointRight.x, pointRight.y);
        path.moveTo(pointTop.x, pointTop.y);
        path.reset();
--------------------- added -------------------------------
        path.setFillType(Path.FillType.EVEN_ODD);
        path.lineTo(pointTop.x, pointTop.y);
        path.lineTo(pointLeft.x, pointLeft.y);
        path.lineTo(pointRight.x, pointRight.y);
        path.lineTo(pointTop.x, pointTop.y);
        path.close();

        canvas.drawPath(path, paint);
        }
like image 91
Arduino Avatar answered Nov 26 '25 08:11

Arduino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!