Android 画图详解 圆形进度条CircleProgressBar

来源:互联网 发布:淘宝商城男装t恤特卖网 编辑:程序博客网 时间:2024/05/29 05:02

先来介绍一下画几何图形要用到的,画布(Canvas)、画笔(Paint)。

1. 画一个圆使用的是drawCircle:canvas.drawCircle(cx, cy, radius, paint);x、y代表坐标、radius是半径、paint是画笔,就是画图的颜色;

2. 在画图的时候还要有注意,你所画的矩形是实心(paint.setStyle(Paint.Style.FILL))还是空心(paint.setStyle(Paint.Style.STROKE);

画图的时候还有一点,那就是消除锯齿:paint.setAntiAlias(true);

3. 还有就是设置一种渐变颜色的矩形:

     Shader mShader = new LinearGradient(0,0,100,100, new int[]{Color.RED,Color.GREEn,Color.BLUE,Color.YELLO},null,Shader.TileMode.REPEAT);

ShapeDrawable sd;

//画一个实心正方形

sd = new ShapeDrawable(new RectShape());

sd.setBounds(20,20,100,100);

sd.draw(canvas);

//一个渐变色的正方形就完成了

 

4. 正方形:drawRect:canvas.drawRect(left, top, right, bottom, paint)

这里的left、top、right、bottom的值是:

left:是矩形距离左边的X轴

top:是矩形距离上边的Y轴

right:是矩形距离右边的X轴

bottom:是矩形距离下边的Y轴

5. 长方形:他和正方形是一个原理,这个就不用说了

6. 椭圆形:记住,这里的Rectf是float类型的

RectF re = new Rect(left, top, right, bottom);

canvas.drawOval(re,paint);

以上是转载他人的


public class CircleProgressBar extends View {
    private int maxProgress = 10;//最大进度
    private int progress = 0;//当前进度
    private int progressStrokeWidth = 6;//线宽
    // 画圆所在的矩形区域
    RectF oval;
    Paint paint;

    public CircleProgressBar(Context context) {
        super(context);
        // TODO 自动生成的构造函数存根
    }

    public CircleProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO 自动生成的构造函数存根
        oval = new RectF();
        paint = new Paint();
    }

    public CircleProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO 自动生成的构造函数存根
    }

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

        /**
         * 画最外层的大圆环
         */
        int centre = getWidth()/2; //获取圆心的x坐标
        int radius = (int) (centre - progressStrokeWidth/2); //圆环的半径
        paint.setColor(Color.WHITE);//(roundColor); //设置圆环的颜色
        paint.setStyle(Paint.Style.STROKE); //设置空心
        paint.setStrokeWidth(progressStrokeWidth); //设置圆环的宽度
        paint.setAntiAlias(true);  //消除锯齿
        canvas.drawCircle(centre, centre, radius, paint); //画出圆环

        /**
         * 画圆弧 ,画圆环的进度
         */

        //设置进度是实心还是空心
        paint.setStrokeWidth(progressStrokeWidth); //设置圆环的宽度
        paint.setColor(Color.rgb(0x57, 0x87, 0xb6));  //设置进度的颜色
        RectF oval = new RectF(centre - radius, centre - radius, centre
                + radius, centre + radius);  //用于定义的圆弧的形状和大小的界限

        paint.setStyle(Paint.Style.STROKE);
        canvas.drawArc(oval, -90, 360 * progress / maxProgress, false, paint);  //根据进度画圆弧  绘制白色圆圈,即进度条背景
    }

    public int getMaxProgress(){
        return maxProgress;
    }

    public void setMaxProgress(int maxProgress){
        this.maxProgress = maxProgress;
    }

    public void setProgress(int progress){
        this.progress = progress;
        this.invalidate();
    }

    public void setProgressNotInUiThread(int progress){
        this.progress = progress;
        this.postInvalidate();
    }

}


0 0
原创粉丝点击