Android自定义水平进度条与进度分隔线

时间:2014-10-20 10:52:05

标签: android progress-bar android-custom-view android-progressbar divider

我需要在Android中制作自定义水平进度条,但它不必是连续的。每个进度值必须有DIVIDERS。例如:它必须有绿色进度和白色分隔线。请参阅下一张图片,了解我正在寻找的内容。谢谢。

enter image description here

现在我正在尝试制作一个自定义Drawable并将其设置为进度条。

public class CustomProgressDrawable extends Drawable {

    private static final int DEFAULT_RECT_WIDHT = 20;
    private static int DEFAULT_RECT_COLOR;

    private int rectWidth;
    private int rectColor;

    Paint paint = new Paint();

    public CustomProgressDrawable(Context context){
        init(context);
    }

    private void init(Context context){
        DEFAULT_RECT_COLOR = context.getResources().getColor(android.R.color.black);
        rectColor = DEFAULT_RECT_COLOR;
        rectWidth = DEFAULT_RECT_WIDHT; // Default rect width
    }


    @Override
    public void draw(Canvas canvas) {

        float parentWidth = 300;
        float parentHeight = 40;
        float childTop = 0;
        float childBottom = parentHeight;


        for (int i = rectWidth; i < parentWidth; i  =+ rectWidth){
            paint.setColor(rectColor);

            canvas.drawRect(i - rectWidth + 10, childTop, i + 10, childBottom, paint);

        }   
    }

    @Override
    protected boolean onLevelChange(int level) {

        invalidateSelf();

        return super.onLevelChange(level);

    } 
}

在我的活动中,我有:

CustomProgressDrawable customProgressDrawable = new CustomProgressDrawable(this);

ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setProgressDrawable(customProgressDrawable);
progressBar.setProgress(50);

0 个答案:

没有答案