這只是一個提示。它只是一個視圖,它在同一矩形中繪製兩個弧線:第一個弧線從角度0到360.第二個(在第一個之上)從0跨越到一個取決於百分比的角度。
public class PercentView extends View {
public PercentView (Context context) {
super(context);
init();
}
public PercentView (Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PercentView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
paint = new Paint();
paint.setColor(getContext().getResources().getColor(R.color.lightblue));
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
bgpaint = new Paint();
bgpaint.setColor(getContext().getResources().getColor(R.color.darkblue));
bgpaint.setAntiAlias(true);
bgpaint.setStyle(Paint.Style.FILL);
rect = new RectF();
}
Paint paint;
Paint bgpaint;
RectF rect;
float percentage = 0;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//draw background circle anyway
int left = 0;
int width = getWidth();
int top = 0;
rect.set(left, top, left+width, top + width);
canvas.drawArc(rect, -90, 360, true, bgpaint);
if(percentage!=0) {
canvas.drawArc(rect, -90, (360*percentage), true, paint);
}
}
public void setPercentage(float percentage) {
this.percentage = percentage/100;
invalidate();
}
}
添加到您的佈局:
<bla.bla.PercentView
android:id="@+id/percentview"
android:layout_width="100dp"
android:layout_height="100dp" />
的
可能重複[畫出的Android餅圖?](http://stackoverflow.com/questions/4397192/draw-pie-chart-in- android) – SERPRO