在屏幕上绘图后,圆弧或圆圈不完美圆

时间:2014-05-01 13:55:39

标签: android draw geometry

我想通过填充弧来显示文件上传的进度,直到它达到100%(圆圈)。这很容易做到。我只需要它来旋转动画。当我这样做时,我得出的结论是,圆圈并不完美,因为它有点弹跳。

下面你会看到100%时的弧线。 the original image

这里添加了一个绿色覆盖图,用于显示圆圈的不完美之处。 the image with a perfect (photoshop) circle on top of it

为什么弧/圆不圆?这是抗锯齿吗?除此之外,当我只是以帧速率在不同的地方重绘弧时,它仍然显示出弹跳效果。

我使用的“调​​试”代码:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Path path = new Path();
    RectF drawBounds = new RectF(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom);

    if(mProgress != 100) {
        path.arcTo(drawBounds, mStartDegree, mProgress * 3.6f, true);
        canvas.drawPath(path, mLoaderPaint);
    }

    if (mStartDegree >= 359)
        mStartDegree = 0;
    else
        mStartDegree ++;

    invalidate();
}

1 个答案:

答案 0 :(得分:1)

似乎弧形绘制程序有点破碎,运行我的代码并等到角度为< 270 degs或更少:

class V extends View implements Runnable {

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private RectF mRect = new RectF();
    private float mStartAngle;
    private float mAngle;
    private Xfermode mClearMmode = new PorterDuffXfermode(Mode.CLEAR);

    public V(Context context) {
        super(context);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mRect.set(0, 0, w, w);
        getHandler().post(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        Log.d(TAG, "onDetachedFromWindow ");
        super.onDetachedFromWindow();
        getHandler().removeCallbacks(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(0xff00aa00);
        mPaint.setXfermode(null);
        canvas.drawOval(mRect, mPaint);

        mPaint.setXfermode(mClearMmode);
        canvas.drawArc(mRect, mStartAngle, mAngle, true, mPaint);
    }

    @Override
    public void run() {
        mStartAngle += 3.65;
        mAngle += 0.2f;
        invalidate();
        getHandler().postDelayed(this, 20);
    }
}

好消息是,您将很容易找到如何使用此代码来获得您想要的内容