Seekbar或多种颜色的进度条

San*_*hav 13 android seekbar progress-bar

在此输入图像描述

我想创建一个像这样的条,当进度为零时,它将是淡入淡出的颜色,但随着进展,它将变得明亮在那一部分(这是我能解释的最好)主要的是我想要酒吧显示所有颜色在同一时间.

Eri*_*wer 19

剪辑你的"on"drawable:在此输入图像描述
在你的"关闭"drawable:在此输入图像描述

通过使用 res/drawable/custom_progress_drawable.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- Background -->
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/custom_progress_bar_off"/>

    <!-- Secondary progress - this is optional -->
    <item android:id="@android:id/secondaryProgress">
        <clip android:drawable="@drawable/custom_progress_bar_secondary" />
    </item>

    <!-- Progress -->
    <item android:id="@android:id/progress">
        <clip android:drawable="@drawable/custom_progress_bar_on" />
    </item>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

Activity,使用

Drawable progressDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.custom_progress_drawable, getTheme());
myProgressBar.setProgressDrawable(progressDrawable);
Run Code Online (Sandbox Code Playgroud)

或者在xml中,使用

android:progressDrawable="@drawable/custom_progress_drawable"
Run Code Online (Sandbox Code Playgroud)

这是android:max="10"在xml中使用时的结果:

在此输入图像描述

它有点偏,但你可以使用setMax()更喜欢的东西10000,并在调用时做一些抵消计算setProgress(),使其更清洁.


dbe*_*m22 7

最后!哈哈,我的任务是为你解决这个问题,如果这就足够了,请随时给我赏金,哈哈.


尝试在布局中使用它:

    <View android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight=".20"/>

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:orientation="horizontal"
                android:gravity="center"
                android:layout_weight="0.62">
                <View android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight=".03"/>
                <ProgressBar style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="0.94"
                    android:progressDrawable="@drawable/progressmask"
                    android:progress="0"
                    android:max="10"
                    android:rotation="180"/>
                <View android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight=".03"/>
            </LinearLayout>
    <View
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight=".18"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

它引用了这个drawable(progressmask.xml):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        <corners android:radius="50dip" />
        <gradient android:startColor="#00000000" android:endColor="#00000000" android:angle="270" />
        <stroke android:width="1dp" android:color="#00000000" />
    </shape>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners android:radius="50dip" />
            <gradient android:startColor="#aa000000" android:endColor="#aa000000"
                android:angle="270" />
            <stroke android:width="1dp" android:color="#00000000" />
        </shape>
    </clip>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

和这个图像(colorprogress.png)

在此输入图像描述

它的作用是将图像设置为线性布局的背景,其中包含一个进度条.进度条为图像添加了一个半透明的黑色遮罩,使其看起来是关闭的.

注意:为了获得这种效果,我不得不使用进度条(即将其翻转,并将其设置为仅10个间隔).您将需要进行一些数学计算以获得与图像对齐的进度.即setprogress ((100-trueprogress)/ 10).抱歉,我没有为你做这部分.

这就是50%的进度(小x和三角形将在设备上消失)

在此输入图像描述

我希望这回答了你的问题!