使用自定义图层列表drawable填充progressBar

时间:2018-01-25 10:22:55

标签: android android-layout progress-bar android-progressbar layer-list

我想在没有任何外部库的情况下填充循环进度条的内部。我已经创建了一个自定义循环进度来处理background / progress / secondaryProgress,但现在我想用椭圆填充环的内部。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/backgroundCenter">
        <shape android:shape="oval">
            <solid android:color="@color/orange_dark" />
        </shape>
    </item>

    <item android:id="@android:id/background">
        <!--Background progress-->
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="15"
            android:useLevel="false">
            <solid android:color="@color/light_grey" />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="15"
            android:useLevel="true">

            <gradient
                android:centerColor="#999999"
                android:endColor="#999999"
                android:startColor="#999999"
                android:type="sweep" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <rotate
            android:fromDegrees="270"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="270">

            <shape
                android:innerRadiusRatio="3"
                android:shape="ring"
                android:thicknessRatio="15"
                android:useLevel="true">

                <rotate
                    android:fromDegrees="0"
                    android:pivotX="50%"
                    android:pivotY="50%"
                    android:toDegrees="360" />

                <solid android:color="@color/cyan_light" />

            </shape>
        </rotate>
    </item>
</layer-list>

布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ProgressBar
        android:id="@+id/statusProgressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:backgroundTint="#CCC"
        android:max="100"
        android:progress="50"
        android:progressDrawable="@drawable/circular_progress"
        android:progressTint="@color/red"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

但结果是(椭圆比圆环大):

PorgressBar fill 我不能添加任何白色笔划来隐藏椭圆形,因为我需要在戒指周围透明。

1 个答案:

答案 0 :(得分:1)

如果有人遇到同样的问题,我会解决问题,我会用比率来制作戒指,只是在卵形的边缘:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingBottom="20dp"
    android:paddingEnd="20dp"
    android:paddingStart="20dp"
    android:paddingTop="20dp">

    <item
        android:id="@+id/backgroundCenter">
        <shape android:shape="oval">
            <solid android:color="@color/orange_dark" />
        </shape>
    </item>

    <item android:id="@android:id/background">
        <!--Background-->
        <shape
            android:innerRadiusRatio="2.31"
            android:shape="ring"
            android:thicknessRatio="15"
            android:useLevel="false">
            <solid android:color="#CCC" />
        </shape>
    </item>


    <item android:id="@android:id/secondaryProgress">
        <shape
            android:innerRadiusRatio="2.31"
            android:shape="ring"
            android:thicknessRatio="15"
            android:useLevel="true">

            <gradient
                android:centerColor="#999999"
                android:endColor="#999999"
                android:startColor="#999999"
                android:type="sweep" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <rotate
            android:fromDegrees="270"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="270">

            <shape
                android:innerRadiusRatio="2.31"
                android:shape="ring"
                android:thicknessRatio="15"
                android:useLevel="true">

                <rotate
                    android:fromDegrees="0"
                    android:pivotX="50%"
                    android:pivotY="50%"
                    android:toDegrees="360" />

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

            </shape>
        </rotate>
    </item>
</layer-list>