Android 不规则弧
我正在使用同一个RectF绘制一个圆弧的多个线段,但是这些圆弧没有正确对齐。我尝试了所有的帽子选项,但它看起来从来都不是对称的Android 不规则弧,android,canvas,draw,Android,Canvas,Draw,我正在使用同一个RectF绘制一个圆弧的多个线段,但是这些圆弧没有正确对齐。我尝试了所有的帽子选项,但它看起来从来都不是对称的 private void initPaint(TypedArray a){ segmentInactivePaint = new Paint(Paint.ANTI_ALIAS_FLAG); segmentInactivePaint.setColor(a.getColor(R.styleable.SegmentsCustomView_segmentBg
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];
}
}
我的猜测是,Canvas.drawArc方法会创建一条部分路径,因此圆弧的插值始终不同于整圆的圆弧
如果能朝着正确的方向努力,我们将不胜感激 因此,我通过使用路径而不是画布的drawArc方法解决了这个问题。每当我绘制新线段时,通过倒带路径
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
position = 0;
for(int i=0; i < segmentSizes.length; i++){
if(i == segmentActive){
tmpPaint = segmentActivePaint;
}else if(i > segmentActive){
tmpPaint = segmentsBGPaint;
}else{
tmpPaint = segmentInactivePaint;
}
path.rewind();
path.addArc(rectF,position - 180, segmentSizes[i] - segmentGap);
canvas.drawPath(path, tmpPaint);
position+=segmentSizes[i];
}
path.reset();
}
倒带似乎能够画出一个几乎完美的圆,而不考虑子弧的数量
drawArc方法的工作方式似乎与addArc非常相似,但是它会在每次调用时重置路径
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
position = 0;
for(int i=0; i < segmentSizes.length; i++){
if(i == segmentActive){
tmpPaint = segmentActivePaint;
}else if(i > segmentActive){
tmpPaint = segmentsBGPaint;
}else{
tmpPaint = segmentInactivePaint;
}
path.rewind();
path.addArc(rectF,position - 180, segmentSizes[i] - segmentGap);
canvas.drawPath(path, tmpPaint);
position+=segmentSizes[i];
}
path.reset();
}