创建形状等活动指标

时间:2018-02-14 11:37:06

标签: android xml user-interface shape

我想在Android中使用像这样的xml创建一个形状。

enter image description here

我设法创建内部白色圆圈,下面是我的代码,但我怎样才能使外部活动指示器像圆圈一样。我也想为外圈制作动画,但这不是现在的优先考虑,但如果有人能帮助我,那就太棒了。

<item android:height="63dp" android:width="63dp" android:gravity="center">
    <shape
        android:shape="oval">

        <solid
            android:color="@color/white"/>
        <stroke
            android:width="2dp"
            android:color="#000000"/>
    </shape>
</item>

2 个答案:

答案 0 :(得分:1)

创建自己的画布视图。请查看此库,例如https://github.com/dinuscxj/CircleProgressBar/blob/master/circleprogressbar/src/main/java/com/dinuscxj/progressbar/CircleProgressBar.java

使用下一个方法绘制线条

private void drawLineProgress(Canvas canvas) {
    float unitDegrees = (float) (2.0f * Math.PI / mLineCount);
    float outerCircleRadius = mRadius;
    float interCircleRadius = mRadius - mLineWidth;

    int progressLineCount = (int) ((float) getProgress() / (float) getMax() * mLineCount);

    for (int i = 0; i < mLineCount; i++) {
        float rotateDegrees = i * unitDegrees;

        float startX = mCenterX + (float) Math.sin(rotateDegrees) * interCircleRadius;
        float startY = mCenterX - (float) Math.cos(rotateDegrees) * interCircleRadius;

        float stopX = mCenterX + (float) Math.sin(rotateDegrees) * outerCircleRadius;
        float stopY = mCenterX - (float) Math.cos(rotateDegrees) * outerCircleRadius;

        if (i < progressLineCount) {
            canvas.drawLine(startX, startY, stopX, stopY, mProgressPaint);
        } else {
            canvas.drawLine(startX, startY, stopX, stopY, mProgressBackgroundPaint);
        }
    }
}

答案 1 :(得分:0)

如果您更喜欢使用XML,我认为您可以通过animation-list实现目标。但我建议您实施自己的自定义View,它会在Canvas中提取您所需的全部内容。如果您需要更改某些内容,它将非常灵活。

祝你好运!