【问题标题】:Create activity indicator like shape创建活动指示器,如形状
【发布时间】:2018-02-14 11:37:06
【问题描述】:

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

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

<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>

【问题讨论】:

  • 可能不是您需要的解决方案,但请使用矢量图像......它们采用 xml 格式且重量轻。您可以在线将此 jpeg/png 转换为矢量..
  • 您也可以将此视图创建为画布上的自定义视图,它可以让您根据需要管理动画,但这种方式当然更复杂
  • 您可以创建一个自定义视图来处理参考此链接以获取一些参考stackoverflow.com/questions/37741872/…

标签: android xml user-interface shape


【解决方案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);
        }
    }
}

【讨论】:

【解决方案2】:

如果您更喜欢使用 XML,我认为您可以通过 animation-list 实现您的目标。但我建议你实现你自己的自定义View,它在Canvas 中绘制了你需要的所有内容。如果您需要更改某些内容,它将很灵活。

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多