代码之家  ›  专栏  ›  技术社区  ›  Piku

如何设计半圆进度条

  •  0
  • Piku  · 技术社区  · 10 年前

    嗨,我有 实现了循环进度条 现在问题是我需要 设计半圆进度条 .

    1 回复  |  直到 10 年前
        1
  •  0
  •   Community Mohan Dere    9 年前

    这可以通过以一定角度剪切包含图像的画布来实现(通过绘制圆弧)。

    然后通过画一条弧线来剪辑图像。

    下面是你可以做到的。

    //Convert the progress in range of 0 to 100 to angle in range of 0 180. Easy math.
    float angle = (progress * 180) / 100;
    mClippingPath.reset();
    //Define a rectangle containing the image
    RectF oval = new RectF(mPivotX, mPivotY, mPivotX + mBitmap.getWidth(), mPivotY + mBitmap.getHeight());
    //Move the current position to center of rect
    mClippingPath.moveTo(oval.centerX(), oval.centerY());
    //Draw an arc from center to given angle
    mClippingPath.addArc(oval, 180, angle);
    //Draw a line from end of arc to center
    mClippingPath.lineTo(oval.centerX(), oval.centerY());
    

    一旦找到路径,就可以使用 clipPath function 以在该路径中剪切画布。

    canvas.clipPath(mClippingPath);
    

    看看 Semi Circle Progress Bar 获取更简单有用的细节。


    请注意,如果硬件加速已打开,clipPath函数将不起作用。您只能为该视图关闭硬件加速。

    //Turn off hardware accleration
      semiCircleProgressBarView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    

    当进度发生变化时,您可以通过调用函数设置进度条,

    semiCircleProgressBarView.setClipping(progress);