2015-05-15 95 views
1

我創建了一個圓形進度欄,隨着任務的進展填充圓。但是,我不想填滿圓圈。我無法弄清楚如何解決這個問題。在圓形進度條中清除progressDrawable

這裏是我的代碼:

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(); 
} 

回答

0

我發現它更容易地創建我自己的drawable與一個繪製調用弧(更改角度每個更新),並將其附加到一個ImageView。