Java - Android 自定义控件之圆形进度条

Author: YugeCse
Date: 2014.06.12

Java - Android 自定义控件之圆形进度条_第1张图片
psb (1).jpg
package com.kaiju.kaiju.usercontrols;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class CircleProgressBar extends View {

    private Context context;
    private Paint paint;
    private float radius = 40f;
    private float ringRadius = 10f;
    private int progress = 0;
    private int max = 100;
    private int circleColor = Color.GRAY;
    private int progressColor = Color.parseColor("#feb239");
    
    public CircleProgressBar(Context context){
        super(context);
        init();
    }
    
    public CircleProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public void init(){
        paint = new Paint();
        paint.setAntiAlias(true);
    }
    
        //设置默认圆环颜色
    public void setCircleColor(int color){
        this.circleColor = color;
        this.invalidate();
    }
    
    public int getCircleColor(){
        return this.circleColor;
    }
    
        //设置进度条颜色
    public void setProgressColor(int color){
        this.progressColor = color;
        this.invalidate();
    }
    
    public int getProgressColor(){
        return this.progressColor;
    }
    
       //设置圆环半径
    public void setRingRadius(float radius){
        this.ringRadius = radius;
        this.invalidate();
    }
    
    public float getRingRadius(){
        return this.ringRadius;
    }
    
    public void setRadius(float radius){
        this.radius = radius;
        this.invalidate();
    }
    
    public float getRadius(){
        return this.radius;
    }
    
        //设置最大值
    public void setMax(int value){
        this.max = value;
    }
    
    public int getMax(){
        return this.max;
    }
    
        //设置当前进度值
    public void setProgress(int progress){
        this.progress = progress;
        this.invalidate();
    }
    
    public int getProgress(){
        return this.progress;
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
                //获取控件测量的宽度高度
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        super.onDraw(canvas);
               //设置画笔参数
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10f);
        paint.setColor(circleColor);
                //绘制最大圆
        canvas.drawArc(new RectF((width-radius*2)/2+2,(height-radius*2)/2,radius*2,radius*2), 270, 360, false, paint);
        paint.setColor(progressColor);
                //绘制内圆
        canvas.drawArc(new RectF((width-radius*2)/2+2,(height-radius*2)/2,radius*2,radius*2), 270, (float)progress/max*360, false, paint);
        paint.setColor(Color.BLUE);
        String value = (int)(((float)progress/max)*100)+"%";
        float widthText = 0f;
        float[] widths = new float[value.length()];
        for(int i=0;i

掌握这方面的自定义控件,主要的就是掌握Canvas的绘图函数,已经如何通过使用变量来控制绘图。这种进度条的绘制应该也算是最基本的了吧,哈哈。

你可能感兴趣的:(Java - Android 自定义控件之圆形进度条)