【发布时间】:2015-02-27 12:46:25
【问题描述】:
我正在使用相同的 RectF 绘制弧的多个段,但是弧没有正确对齐。我尝试了所有 CAP 选项,但它看起来从来都不是对称的。
private void initPaint(TypedArray a){
segmentInactivePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
segmentInactivePaint.setColor(a.getColor(R.styleable.SegmentsCustomView_segmentBgColor, segmentBgColor));
segmentInactivePaint.setStrokeWidth(30f);
segmentInactivePaint.setStyle(Paint.Style.STROKE);
segmentsBGPaint = new Paint(segmentInactivePaint);
segmentsBGPaint.setAlpha(64);
segmentActivePaint = new Paint(segmentInactivePaint);
segmentActivePaint.setColor(a.getColor(R.styleable.SegmentsCustomView_segmentActiveColor,segmentActiveColor));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if(getWidth()<getHeight()) {
rectF.set(getLeft(), getTop(), getWidth() - getLeft(), getWidth() - getLeft());
}else{
rectF.set(getLeft(), getTop(), getHeight() - getTop(), getHeight() - getTop());
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
position = 0;
for(int i=0; i < segmentSizes.length; i++){
canvas.drawArc(rectF,position-180,segmentSizes[i]-segmentGap,false,
i != segmentActive ?
i < segmentActive ? segmentInactivePaint : segmentsBGPaint
: segmentActivePaint);
position+=segmentSizes[i];
}
}
我也尝试使用静态 RectF,它不会因任何调整大小事件而改变,所以这不是问题。
RectF (0):﹕ [75.0,75.0][759.0,759.0]
RectF (1):﹕ [75.0,75.0][759.0,759.0]
RectF (2):﹕ [75.0,75.0][759.0,759.0]
RectF (3):﹕ [75.0,75.0][759.0,759.0]
RectF (4):﹕ [75.0,75.0][759.0,759.0]
我的猜测是,Canvas.drawArc 方法会创建一个部分路径,因此圆弧的插值总是不同于整圆的圆弧。
我们将不胜感激朝着正确的方向前进。
【问题讨论】: