Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I draw an arrowhead (in Android)?
    primarykey
    data
    text
    <p>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 is working.</p> <p>I have searched a bit and found a Java example, but Android doesn't have <code>GeneralPath</code> or <code>AffineTransform</code>.</p> <p>Right now my code looks like the following (the arrowhead looks nothing like an arrowhead):</p> <pre><code>public class DrawableView extends View { Context mContext; private int centerX; private int centerY; private int radius; private double arrLength; private double arrHeading; private int margin = 10; public DrawableView(Context context) { super(context); mContext = context; } @Override protected void onDraw(Canvas canvas) { //Paint Background Paint background = new Paint(); background.setColor(getResources().getColor(R.color.background); canvas.drawRect(0, 0, getWidth(), getHeight(), background); //Set vars for Arrow Paint Paint paint = new Paint(); paint.setColor(getResources().getColor(R.color.arrowColor); centerX = getWidth() / 2; centerY = getHeight() / 2; arrLength = radius - 10; if(centerX &lt; centerY) radius = centerX - margin; else radius = centerY - margin; //Draw Shaft int[] xy = findArrowPos(arrLength, arrHeading); canvas.drawLine(centerX, centerY, xy[0], xy[1], paint); //Draw ArrowHead //This is where I'm confused } private int[] findArrowPos(double length, double angle) { int[] points = new int[2]; double theta = Math.toRadians(angle); points[0] = centerX + (int) (length * Math.cos(theta)); points[1] = centerY + (int) (length * Math.sin(theta)); return points; } } </code></pre> <p>I have taken a look at the following threads for guidance: <br> * <a href="http://www.java-forums.org/awt-swing/6241-how-u-rotate-arrow-mark-line-moves-accordingly.html" rel="nofollow noreferrer">http://www.java-forums.org/awt-swing/6241-how-u-rotate-arrow-mark-line-moves-accordingly.html</a> <br> * <a href="https://stackoverflow.com/questions/2027613/how-to-draw-a-directed-arrow-line-in-java">How to draw a directed arrow line in Java?</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload