有2个指标进度条进度条、指标

2023-09-06 03:02:19 作者:没有印记

我想有进度条与2指标。

I want to have progress bar with 2 indicators.

一个指标显示任务A的颜色绿色的进度,第二指示灯显示为红色任务B的进步,都在同一个进度条。其余的显示任务A和B的剩余。

One indicator shows progress of task A in color green, the second indicator shows progress of task B in red, all in one progress bar. The rest shows the remaining of tasks A and B.

有一个(简单的)解决方案来实现这一目标?我读的文件,但没有找到帮助。

Is there a (simple) solution to achieve this? I read the documentation but did not find help.

推荐答案

这可以由作为主进步和相同的进度条二级进步编码两个指标完成的。

This can be done by coding the two indicators as the Primary progress and secondary progress of the same progress bar.

创建一个子类进度条。

create a sub class for the progress bar.

public class TextProgressBar extends ProgressBar {
    private Paint textPaint;

    public TextProgressBar(Context context) {
        super(context);
        textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
    }

    public TextProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        setMax(30);
        setProgress(12);
        setSecondaryProgress(20);

    }

}

进度条中的XML项已被称为使用这个子类。

The XML entry for the progress bar has to be referred to using this sub class.

<com.darsh.doubleProgressBar.TextProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="15sp"
    android:layout_marginLeft="1sp"
    android:layout_marginRight="1sp"
    android:layout_marginTop="10sp"
    android:progressDrawable="@drawable/progress" />

现在建立在资源目录的绘制

now create the drawable in the resources directory

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

<item android:id="@android:id/background">
    <shape>
        <corners android:radius="5dip" />

        <gradient
            android:angle="270"
            android:centerColor="#ff5a5d5a"
            android:centerY="0.75"
            android:endColor="#ff747674"
            android:startColor="#ff5a5d5a" />
    </shape>
</item>
<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />


            <gradient
                android:angle="270"
                android:centerColor="#32cd32"
                android:centerY="0.75"
                android:endColor="#32cd32"
                android:startColor="#32cd32" />
        </shape>
    </clip>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners android:radius="5dip" />

            <gradient
                android:angle="270"
                android:endColor="#33B5E5"
                android:startColor="#33B5E5" />
        </shape>
    </clip>
</item>
</layer-list>

的色彩为一级和二级指标可以在此绘制改变。

The colors for the primary and secondary indicators can be changed in this drawable.

请使用它们在你的code是这样的:

Make use of them in your code like this:

TextProgressBar textProgress;
textProgress = (TextProgressBar)findViewById(R.id.progressBar1);
textProgress.setMax(100);
textProgress.setProgress(10); //
textProgress.setSecondaryProgress(50); //green