如何绘制箭头(在Android中)?

我刚接触Android,并且一直在研究Canvas。我正在尝试绘制箭头,但是在绘制轴时运气不好,没有一个箭头在起作用。

我进行了一些搜索,找到一个Java示例,但是Android没有GeneralPathor AffineTransform

现在,我的代码如下所示(箭头看上去完全不像箭头):

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 < 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;

}

}

我查看了以下线程以获取指导:

* http://www.java-forums.org/awt-swing/6241-how-u-rotate-arrow-mark-line-moves-accordingly.html

回答:

如何使用“ Path myPath = new Path();”

您将在其中指定x和y位置以使用线并填充来创建三角形。您可以阅读有关内容,这是我从某处获取的示例。

// create and draw triangles

// use a Path object to store the 3 line segments

// use .offset to draw in many locations

// note: this triangle is not centered at 0,0

paint.setStyle(Paint.Style.STROKE);

paint.setStrokeWidth(2);

paint.setColor(Color.RED);

Path path = new Path();

path.moveTo(0, -10);

path.lineTo(5, 0);

path.lineTo(-5, 0);

path.close();

path.offset(10, 40);

canvas.drawPath(path, paint);

path.offset(50, 100);

canvas.drawPath(path, paint);

// offset is cumlative

// next draw displaces 50,100 from previous

path.offset(50, 100);

canvas.drawPath(path, paint);

以上是 如何绘制箭头(在Android中)? 的全部内容, 来源链接: utcz.com/qa/431033.html

回到顶部