半圓弧形進度條


/**
 * 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()控制顯示進度的多少

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章