Create a progress drawable programmatically

后端 未结 1 1241
孤街浪徒
孤街浪徒 2021-02-04 21:55

I have a scenario where i need to have a large number of progress bar drawables. I cant create xml resources for all of them because i want the user to choose a color that will

1条回答
  •  不思量自难忘°
    2021-02-04 21:57

    From the links provided by Rajesh and g00dy, i was able to come up with a solution.

    public static Drawable createDrawable(Context context) {
    
    ShapeDrawable shape = new ShapeDrawable();
    shape.getPaint().setStyle(Style.FILL);
    shape.getPaint().setColor(
        context.getResources().getColor(R.color.transparent));
    
    shape.getPaint().setStyle(Style.STROKE);
    shape.getPaint().setStrokeWidth(4);
    shape.getPaint().setColor(
        context.getResources().getColor(R.color.category_green_stroke));
    
    ShapeDrawable shapeD = new ShapeDrawable();
    shapeD.getPaint().setStyle(Style.FILL);
    shapeD.getPaint().setColor(
        context.getResources().getColor(R.color.category_green));
    ClipDrawable clipDrawable = new ClipDrawable(shapeD, Gravity.LEFT,
        ClipDrawable.HORIZONTAL);
    
    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] {
        clipDrawable, shape });
    return layerDrawable;
    }
    

    This code will create a drawable that is visually similar to what the xml in my question creates.

    0 讨论(0)
提交回复
热议问题