How do I draw an arrowhead (in Android)?

后端 未结 8 532
一个人的身影
一个人的身影 2020-11-29 06:29

I\'m fairly new to Android and have been toying around with Canvas. I\'m attempting to draw an arrow but I\'m only having luck with drawing the shaft, none of the arrowhead

8条回答
  •  甜味超标
    2020-11-29 07:19

    My Arrow Drawing code, maybe it can be of some use for somebody:

        /**
     * Draw an arrow
     * change internal radius and angle to change appearance
     * - angle : angle in degrees of the arrows legs
     * - radius : length of the arrows legs
     * @author Steven Roelants 2017
     *
     * @param paint
     * @param canvas
     * @param from_x
     * @param from_y
     * @param to_x
     * @param to_y
     */
    private void drawArrow(Paint paint, Canvas canvas, float from_x, float from_y, float to_x, float to_y)
    {
        float angle,anglerad, radius, lineangle;
    
        //values to change for other appearance *CHANGE THESE FOR OTHER SIZE ARROWHEADS*
        radius=10;
        angle=15;
    
        //some angle calculations
        anglerad= (float) (PI*angle/180.0f);
        lineangle= (float) (atan2(to_y-from_y,to_x-from_x));
    
        //tha line
        canvas.drawLine(from_x,from_y,to_x,to_y,paint);
    
        //tha triangle
        Path path = new Path();
        path.setFillType(Path.FillType.EVEN_ODD);
        path.moveTo(to_x, to_y);
        path.lineTo((float)(to_x-radius*cos(lineangle - (anglerad / 2.0))),
                (float)(to_y-radius*sin(lineangle - (anglerad / 2.0))));
        path.lineTo((float)(to_x-radius*cos(lineangle + (anglerad / 2.0))),
                (float)(to_y-radius*sin(lineangle + (anglerad / 2.0))));
        path.close();
    
        canvas.drawPath(path, paint);
    }
    

提交回复
热议问题