Ank*_*kit 2 android android-canvas
我有一个自定义视图,我画一个弧.现在我还需要绘制一条连接中心和圆周的线,并且应该恰好位于弧的中间.
现在我知道android画布中绘制线的代码非常简单
但它不涉及角度,弧总是使用角度来绘制自己.
所以我可以在与弧相同的方向画线吗?
我清楚这里还是需要解释更多?
请帮忙.
fay*_*lon 10
计算起点和终点.
private class MView extends View {
private Paint mPaint;
private RectF mRect;
private int mCenterX = 150;
private int mCenterY = 150;
public MView(Context context) {
super(context);
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Style.STROKE);
mRect = new RectF(0, 0, mCenterX * 2, mCenterY * 2);
}
@Override
protected void onDraw(Canvas canvas) {
float startAngle = 30;
float sweepAngle = 60;
canvas.drawArc(mRect, startAngle, sweepAngle, true, mPaint);
float startX = mCenterX;
float startY = mCenterY;
float radius = mCenterX;
float angle = (float) ((startAngle + sweepAngle / 2) * Math.PI / 180);
float stopX = (float) (startX + radius * Math.cos(angle));
float stopY = (float) (startY + radius * Math.sin(angle));
canvas.drawLine(startX, startY, stopX, stopY, mPaint);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1905 次 |
| 最近记录: |