我想在我的Android应用程序中绘制一个椭圆弧。 我用了这段代码:
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
angle=45; //angle of ellipse major axis with X-axis.
startAngle=0; //start angle of arc
sweepAngle=90; //sweep angle of arc
a = 200; //major axis of ellipse
b = 100; //minor axis of ellipse
canvas.rotate(angle, center.x, center.y);
//draw the arc
canvas.drawArc(rect, startAngle - angle, sweepAngle, true, paintLine);
paintLine.setPathEffect(new DashPathEffect(new float[] { 5, 5 }, 0));
canvas.drawOval(rect, paintLine);
canvas.restore();
paintLine.setPathEffect(null);
}
我收到这个形状:
我需要的弧应该在此图像的红点处开始和结束:
请告诉我我犯了什么错误。
感谢。
答案 0 :(得分:0)
当android绘制椭圆弧时,就好像是先在提供的扫掠角上绘制一个圆弧一样。然后缩放该圆弧,使其适合指定的椭圆。椭圆的起始角和后掠角将不同于指定的起始角和后掠角。要获得所需角度,必须将实际角度指定为tan(actual-angle)=(a / b)* tan(wanted-angle)
public class Main3Activity extends Activity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = new View(this) {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
float angle=45; //angle of ellipse major axis with X-axis.
// These are the wanted angles
float startAngle_ = (float)Math.toRadians(315); //start angle of arc
float endAngle_ = (float)Math.toRadians(45); //end angle of arc
float a = 200; //major axis of ellipse
float b = 100; //minor axis of ellipse
float xc = getWidth()/2f;
float yc = getHeight()/2f;
// These are the angles to use
float startAngle = (float)Math.toDegrees(
Math.atan2(a*Math.sin(startAngle_),b*Math.cos(startAngle_)));
float endAngle = (float)Math.toDegrees(
Math.atan2(a*Math.sin(endAngle_),b*Math.cos(endAngle_)));
float sweepAngle = endAngle - startAngle;
RectF rect = new RectF(xc-a,yc-b,xc+a,yc+b);
Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setStyle(Paint.Style.STROKE);
canvas.rotate(angle,xc,yc);
//draw the arc
canvas.drawArc(rect, startAngle, sweepAngle, true, paintLine);
paintLine.setPathEffect(new DashPathEffect(new float[] { 5, 5 }, 0));
canvas.drawOval(rect, paintLine);
canvas.restore();
paintLine.setPathEffect(null);
}
};
FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
addContentView(view,layout);
}
这个问题快七年了!大约需要更新文档吗?