我有一个圆弧,我希望在0度,45度,90度,135度,180度绘制刻度标记,是否有人可以帮助我获得在此草图上获得点5和点30的x,y所需的数学?
@H_301_4@private void drawScale(Canvas canvas) {
//canvas.drawOval(scaleRect,scalePaint);
canvas.save();
Paint p = new Paint();
p.setColor(Color.WHITE);
p.setStrokeWidth(10f);
canvas.drawLine(rectF.left-getWidth()/20,rectF.height()/2,rectF.left,p);
canvas.restore();
}
解决方法
您可以使用sin和cos计算其旋转.让我们假设您有零点A,并希望将其旋转到旋转30°的B点.
像这样的东西:
像这样的东西:
基本上新点是(cx x,cy y).在这个特殊情况下,sin和cos的定义将是下一个:
@H_301_4@sin = x/R cos = y/R获得精确的x和y并不难.因此,要在已知半径的圆圈中旋转特定角度的点,我们需要以下一个方式计算坐标:
@H_301_4@x = cx + sin(angle) * R; y = cy + cos(angle) * R;现在让我们回到Android和Canvas!
@H_301_4@@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.save(); float cx = getWidth() / 2f; float cy = getHeight() / 2f; float scaleMarkSize = getResources().getDisplayMetrics().density * 16; // 16dp float radius = Math.min(getWidth(),getHeight()) / 2; for (int i = 0; i < 360; i += 45) { float angle = (float) Math.toRadians(i); // Need to convert to radians first float startX = (float) (cx + radius * Math.sin(angle)); float startY = (float) (cy - radius * Math.cos(angle)); float stopX = (float) (cx + (radius - scaleMarkSize) * Math.sin(angle)); float stopY = (float) (cy - (radius - scaleMarkSize) * Math.cos(angle)); canvas.drawLine(startX,startY,stopX,stopY,scalePaint); } canvas.restore(); }