如何在Android中创建一个在其上旋转的圆形进度条? [英] How to Create a circular progressbar in Android which rotates on it?

查看:36
本文介绍了如何在Android中创建一个在其上旋转的圆形进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个圆形的进度条.这就是我想要实现的

I am trying to create a rounded progressbar. This is what I want to achieve

有一个灰色的背景环.在它的顶部,会出现一个蓝色进度条,它在 60 秒或任何秒数内以圆形路径从 0 移动到 360.

There is a grey color background ring. On top of it, a blue color progressbar appears which moves in a circular path from 0 to 360 in 60 or whatever amount of seconds.

这是我的示例代码.

<ProgressBar
            android:id="@+id/ProgressBar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            style="?android:attr/progressBarStyleLarge"
            android:indeterminateDrawable="@drawable/progressBarBG"
            android:progress="50"
            />

为此,在可绘制的progressBarBG"中,我创建了一个图层列表,并在该图层列表中提供了两个项目,如图所示.

To do this, in the drawable "progressBarBG", I am creating a layerlist and inside that layer list I am giving two items as shown.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape
            android:shape="ring"
            android:innerRadius="64dp"
            android:thickness="8dp"
            android:useLevel="false">

        <solid android:color="@color/grey" />
    </shape>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape
                android:shape="ring"
                android:innerRadius="64dp"
                android:thickness="8dp"
                android:useLevel="false">

            <solid android:color="@color/blue" />
        </shape>
    </clip>
</item>

现在,第一个灰色环生成良好.然而,蓝色环从 drawable 的左侧开始,然后像线性进度条一样向右移动.这就是它以 50% 的进度显示的方式,红色箭头表示方向.

Now, the first grey ring is generated fine. The blue ring however starts from the left of the drawable and goes to the right just like how a linear progressbar works. This is how it shows at 50% progress with the red color arrow showing direction.

我想按预期在圆形路径中移动蓝色进度条.

I want to move the blue progressbar in circular path as expected.

推荐答案

这是我的两个解决方案.

Here are my two solutions.

简答:

我没有创建 layer-list,而是将它分成两个文件.一种用于 ProgressBar,一种用于其背景.

Instead of creating a layer-list, I separated it into two files. One for ProgressBar and one for its background.

这是 ProgressDrawable 文件(@drawable 文件夹):circular_progress_bar.xml

This is the ProgressDrawable file (@drawable folder): circular_progress_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="270"
    android:toDegrees="270">
    <shape
        android:innerRadiusRatio="2.5"
        android:shape="ring"
        android:thickness="1dp"
        android:useLevel="true"><!-- this line fixes the issue for lollipop api 21 -->

        <gradient
            android:angle="0"
            android:endColor="#007DD6"
            android:startColor="#007DD6"
            android:type="sweep"
            android:useLevel="false" />
    </shape>
</rotate>

这是它的背景(@drawable文件夹):circle_shape.xml

And this is for its background(@drawable folder): circle_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadiusRatio="2.5"
    android:thickness="1dp"
    android:useLevel="false">

    <solid android:color="#CCC" />

</shape>

最后,在您正在工作的布局中:

And at the end, inside the layout that you're working:

<ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:indeterminate="false"
        android:progressDrawable="@drawable/circular_progress_bar"
        android:background="@drawable/circle_shape"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:progress="65" />

结果如下:

长答案:

使用继承 android.view.View 的自定义视图

Use a custom view which inherits the android.view.View

这里是完整的项目 在 github

这篇关于如何在Android中创建一个在其上旋转的圆形进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆