有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Erase progressDrawable在循环进度条中

我已经创建了一个圆形进度条,随着任务的进行,它将填充圆形进度条。但是,我不想填充,而是想抹掉圆圈。我不知道该怎么做

这是我的密码:

可拉环。爪哇:

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, 安卓.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) 个答案

  1. # 1 楼答案

    我发现通过对圆弧的绘制调用(每次更新时更改角度)创建自己的可绘制图形并将其附加到ImageView更容易