擦除循环进度条中的progressDrawable

时间:2015-05-15 16:35:53

标签: java android

我创建了一个循环进度条,随着任务的进行填充圆圈。但是,而不是填充我想要擦​​除圆圈。我无法弄清楚如何处理这个问题。

这是我的代码:

RingDrawable.java:

public class RingDrawable extends GradientDrawable {

private Class<?> mGradientState;

public RingDrawable() {
    this(Orientation.TOP_BOTTOM, null);
}

public RingDrawable(int innerRadius, int thickness, float innerRadiusRatio, float thicknessRatio) {
    this(Orientation.TOP_BOTTOM, null, innerRadius, thickness, innerRadiusRatio, thicknessRatio);
}

public RingDrawable(GradientDrawable.Orientation orientation, int[] colors) {
    super(orientation, colors);
    setShape(RING);
}

public RingDrawable(GradientDrawable.Orientation orientation, int[] colors, int innerRadius, int thickness, float innerRadiusRatio, float thicknessRatio) {
    this(orientation, colors);
    try {
        setInnerRadius(innerRadius);
        setThickness(thickness);
        setInnerRadiusRatio(innerRadiusRatio);
        setThicknessRatio(thicknessRatio);
    } catch (Exception e) {
        // fail silently - change to your own liking
        e.printStackTrace();
    }
}

public void setInnerRadius(int radius) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    if (mGradientState == null) mGradientState = resolveGradientState();
    Field innerRadius = resolveField(mGradientState, "mInnerRadius");
    innerRadius.setInt(getConstantState(), radius);
}

public void setThickness(int thicknessValue) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    if (mGradientState == null) mGradientState = resolveGradientState();
    Field thickness = resolveField(mGradientState, "mThickness");
    thickness.setInt(getConstantState(), thicknessValue);
}

public void setInnerRadiusRatio(float ratio) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    if (mGradientState == null) mGradientState = resolveGradientState();
    Field innerRadiusRatio = resolveField(mGradientState, "mInnerRadiusRatio");
    innerRadiusRatio.setFloat(getConstantState(), ratio);
}

public void setThicknessRatio(float ratio) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    if (mGradientState == null) mGradientState = resolveGradientState();
    Field thicknessRatio = resolveField(mGradientState, "mThicknessRatio");
    thicknessRatio.setFloat(getConstantState(), ratio);
}

private Class<?> resolveGradientState() {
    Class<?>[] classes = GradientDrawable.class.getDeclaredClasses();
    for (Class<?> singleClass : classes) {
        if (singleClass.getSimpleName().equals("GradientState")) return singleClass;
    }
    throw new RuntimeException("GradientState could not be found in current GradientDrawable implementation");
}

private Field resolveField(Class<?> source, String fieldName) throws SecurityException, NoSuchFieldException {
    Field field = source.getDeclaredField(fieldName);
    field.setAccessible(true);
    return field;
}

}

创建进度条:

    mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
    mProgressBar.setMax(duration);
    mProgressBar.setProgress(0);
    mProgressBar.setIndeterminate(false);
    RingDrawable ring = new RingDrawable(25, 5, 0, 0);
    ring.setColor(Color.WHITE);
    mProgressBar.setProgressDrawable(ring);

更新方法:

public void update(long elapsedTime) {
    //update the buttons states
    mProgressBar.setProgress((int)(elapsedTime/1000));
    mProgressBar.invalidate();
}

1 个答案:

答案 0 :(得分:0)

我发现通过对弧的绘制调用(更改每个更新的角度)创建自己的drawable更容易,并将其附加到ImageView。