【问题标题】:Android drawArc irregular arcAndroid drawArc 不规则圆弧
【发布时间】:2015-02-27 12:46:25
【问题描述】:

Illustration

我正在使用相同的 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 方法会创建一个部分路径,因此圆弧的插值总是不同于整圆的圆弧。

我们将不胜感激朝着正确的方向前进。

【问题讨论】:

    标签: android canvas draw


    【解决方案1】:

    所以我设法通过使用 Path 而不是画布的 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() 非常相似,但它会在每次调用时重置路径。

    【讨论】:

      猜你喜欢
      • 2012-12-24
      • 1970-01-01
      • 2013-07-11
      • 2011-09-12
      • 2017-12-19
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多