2015-12-05 72 views
0

我想要自定義視圖有圓和我必須在運行時給部分如下圖所示。我在onDraw方法中嘗試了很多東西,但沒有得到任何好運。即使我提到這個https://github.com/donvigo/CustomProgressControls。基本上我想給部分,然後每個部分我可以根據我的需要給顏色。自定義圓與之間的空格

enter image description here

我找進度應該有間隙/空間顯示在圈之間的圖像說,如果我給了第5,那麼它應該有5個部分,然後說,如果我只填寫3部分則將成爲所有紅色1-3部分,如果它傳遞給4section那麼所有的1-4變成綠色同爲5還

要畫我做這樣的:

private void initExternalCirclePainter() { 
    internalCirclePaint = new Paint(); 
    internalCirclePaint.setAntiAlias(true);  
    internalCirclePaint.setStrokeWidth(internalStrokeWidth); 
    internalCirclePaint.setColor(color); 
    internalCirclePaint.setStyle(Paint.Style.STROKE); 
    internalCirclePaint.setPathEffect(new DashPathEffect(new float[]{dashWith, dashSpace}, dashSpace)); 
} 
+0

這意味着你需要的進度條5段,但在部分的differernt顏色我得到的權利?? –

+0

請參閱'帆布#drawArc' – pskink

+0

帆布#drawArc我累了,但我沒有得到兩個部分之間的完美空間 – nick

回答

0

我覺得更容易做數學爲drawArc(操作角度VA根據部分的數量計算)而不是計算弧長。 下面是一個簡單的想法,有很多硬編碼性質的,但你應該能夠得到的想法:

public class MyStrokeCircleView extends View { 
    private Paint mPaint; 
    private RectF mRect; 
    private int mPadding; 

    private int mSections; 
    private int mFullArcSliceLength; 
    private int mColorArcLineLength; 
    private int mArcSectionGap; 

    public MyStrokeCircleView(Context context) { 
     super(context); 
     init(null, 0); 
    } 

    public MyStrokeCircleView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(attrs, 0); 
    } 

    public MyStrokeCircleView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(attrs, defStyle); 
    } 

    private void init(AttributeSet attrs, int defStyle) { 
     mPaint = new Paint(); 
     mPaint.setFlags(Paint.ANTI_ALIAS_FLAG); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeWidth(10); 
     mPaint.setColor(ContextCompat.getColor(getContext(), android.R.color.darker_gray)); 

     mPadding = 5; 
     mRect = new RectF(mPadding, mPadding, mPadding, mPadding); 

     mSections = 4; 
     mFullArcSliceLength = 360/mSections; 
     mArcSectionGap = mFullArcSliceLength/10; 
     mColorArcLineLength = mFullArcSliceLength - 2 * mArcSectionGap; 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     mRect.right = getWidth() - mPadding; 
     mRect.bottom = getHeight() - mPadding; 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     for (int i = 0; i < mSections; i++) { 
      canvas.drawArc(mRect, i * mFullArcSliceLength + mArcSectionGap, mColorArcLineLength, false, mPaint); 
     } 
    } 
}