/** * Created by Administrator on 2016/5/27. */ import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.LinearGradient; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.RectF; import android.graphics.Shader; import android.util.AttributeSet; import android.view.View; public class RoundProgress extends View { Paint paint,textpaint; RectF area; int value = 80; LinearGradient shader; public RoundProgress(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); // TODO Auto-generated constructor stub } public RoundProgress(Context context, AttributeSet attrs) { super(context, attrs); init(); // TODO Auto-generated constructor stub } public RoundProgress(Context context) { super(context); init(); // TODO Auto-generated constructor stub } public void setProgress(int value){ this.value = value; invalidate(); } public void init() { paint = new Paint(); paint.setStrokeWidth(40f); paint.setColor(Color.WHITE); paint.setStyle(Style.STROKE); paint.setAntiAlias(true); textpaint = new Paint(); textpaint.setTextSize(40f); textpaint.setColor(Color.WHITE); area = new RectF(100, 100, 500, 500); shader =new LinearGradient(0, 0, 400, 0, new int[] { Color.BLUE, Color.WHITE}, null, Shader.TileMode.CLAMP); paint.setShader(shader); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub canvas.drawColor(Color.GRAY); canvas.drawArc(area, 120, 288*value/100 , false, paint); canvas.drawText(value+"%", 270, 290, textpaint); } }
布局文件中引用该控件,通过调用setprogress()控制显示进度的多少