android - 带有 2 个指标的进度条

标签 android progress-bar greenfoot

我想要带有 2 个指示器的进度条。

第一个指示器以绿色显示任务 A 的进度,第二个指示器以红色显示任务 B 的进度,全部在一个进度条中。 rest显示任务A和B的剩余部分。

是否有(简单的)解决方案来实现这一点?我阅读了文档但没有找到帮助。

最佳答案

这可以通过将两个指标编码为同一进度条的主要进度和次要进度来完成。

为进度条创建一个子类。

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 条目。

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

现在在资源目录中创建drawable

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

可以在此绘图中更改主要和次要指示器的颜色。

像这样在您的代码中使用它们:

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

关于android - 带有 2 个指标的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10387435/

相关文章:

android - Facebook Android SDK 4.1+ 中 Android refreshCurrentAccessTokenAsync 的回调

java - Gradle 构建重复条目

android - 将位图转换为 WebRTC VideoFrame

c++ - QprogressBar 在一秒内从 0 跳到 100,如何使其平滑

jquery $ajax 进展

jquery - 进度条未单独加载

android - 在 Android 上使用 Volley 获取 SSLHandshakeException

Java删除对象

java - 找不到符号 - 方法scroll()