Android开发画图(贝塞尔曲线)

目的

通过代码实现画取贝塞尔曲线图

相关技术、及其使用

1、创建WaveV诶我继承于View
调用onDraw方法

 @Override
    protected void onDraw(Canvas canvas) {
        //创建一个Path
        Path mPath = new Path();
        mPath.moveTo(50,500);
        //画贝塞尔曲线
//        mPath.cubicTo(50,500,200,300,350,500);
//
//        mPath.cubicTo(350,500,500,700,650,500);
        mPath.quadTo(200,300,350,500);
        mPath.quadTo(500,700,650,500);

//        mPath.lineTo(400,500);
        //画笔
        Paint mPaint = new Paint();
        mPaint.setStrokeWidth(10);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);

        //设置路径的起始点
        canvas.drawPath(mPath,mPaint);
    }
}

2、创建MeterView继承于View
创建init方法进行画笔的相关初始化:

   private void init(){
        //画笔
         bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
         bgPaint.setColor(Color.BLACK);
         //STROKE 经过的地方才会画
         bgPaint.setStyle(Paint.Style.STROKE);
         bgPaint.setStrokeWidth(40);
         bgPaint.setStrokeCap(Paint.Cap.ROUND);//设置两端的类型

        progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        progressPaint.setColor(Color.MAGENTA);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeWidth(40);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);

        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(100);
    }

3、调用onDraw方法进行画图(圆圈进度图)

protected void onDraw(Canvas canvas) {
        //画初始圆
        //确定矩形区域
        RectF frame = new RectF(50,100,getWidth()-50,getWidth()-50);

        //画一个弧
        canvas.drawArc(frame,120,300,false,bgPaint);

        //计算进度对应的角度
        int angle = (int)(progress*300);
        canvas.drawArc(frame,120,angle,false,progressPaint);

        //计算文字的宽度
        int width = (int) textPaint.measureText("100%");
        //计算文字的矩阵 FontMetrics
        Paint.FontMetrics fo = textPaint.getFontMetrics();

        //计算文字的高度
        int height = (int)(fo.bottom - fo.top);

        //计算向下移动的距离 Ascent /2
        int sapce = (int) -(fo.ascent/2);

        //画文字
        canvas.drawText("100%",getWidth()/2-width/2,getWidth()/2+sapce,textPaint);
    }

4、调用setProgress方法进行刷新和重绘:

  public void setProgress(float progress) {
        this.progress = progress;
        //刷新,重绘
        if(progress <= 1.0001){
            invalidate();
        }
    }

你可能感兴趣的:(Android开发画图(贝塞尔曲线))