@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /** 画最外层的大圆环 */ int centre = getWidth() / 2; // 获取圆心的x坐标 int radius = (int) (centre - roundWidth / 2); // 圆环的半径 paint.setColor(roundColor); // 设置圆环的颜色 paint.setStyle(Paint.Style.STROKE); // 设置空心 paint.setStrokeWidth(roundWidth); // 设置圆环的宽度 paint.setAntiAlias(true); // 消除锯齿 canvas.drawCircle(centre, centre, radius, paint); // 画出圆环 Log.e("log", centre + ""); /** 画进度百分比 */ paint.setStrokeWidth(0); paint.setColor(textColor); paint.setTextSize(textSize); paint.setTypeface(Typeface.DEFAULT_BOLD); // 设置字体 int percent = (int) (((float) progress / (float) max) * 100); // 中间的进度百分比,先转换成float在进行除法运算,不然都为0 float textWidth = paint.measureText(percent + "%"); // 测量字体宽度,我们需要根据字体的宽度设置在圆环中间 if (textIsDisplayable && percent != 0 && style == STROKE) { canvas.drawText( percent + "%", centre - textWidth / 2, centre + textSize / 2, paint); // 画出进度百分比 } /** 画圆弧 ,画圆环的进度 */ // 设置进度是实心还是空心 paint.setStrokeWidth(roundWidth); // 设置圆环的宽度 paint.setColor(roundProgressColor); // 设置进度的颜色 RectF oval = new RectF( centre - radius, centre - radius, centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限 switch (style) { case STROKE: { paint.setStyle(Paint.Style.STROKE); canvas.drawArc(oval, 0, 360 * progress / max, false, paint); // 根据进度画圆弧 break; } case FILL: { paint.setStyle(Paint.Style.FILL_AND_STROKE); if (progress != 0) canvas.drawArc(oval, 0, 360 * progress / max, true, paint); // 根据进度画圆弧 break; } } }
@Override public void draw(Canvas canvas) { float startAngle = mCurrentGlobalAngle - mCurrentGlobalAngleOffset; float sweepAngle = mCurrentSweepAngle; if (!mModeAppearing) { startAngle = startAngle + sweepAngle; sweepAngle = 360 - sweepAngle - MIN_SWEEP_ANGLE; } else { sweepAngle += MIN_SWEEP_ANGLE; } canvas.drawArc(fBounds, startAngle, sweepAngle, false, mPaint); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (Build.VERSION.SDK_INT < 11) { mTransform.set(canvas.getMatrix()); mTransform.preRotate(mRotation, mPivot.x, mPivot.y); canvas.setMatrix(mTransform); } for (Item it : mData) { mPiePaint.setShader(it.mItemShader); cSlicePaint.setShader(it.cSliceShader); // Log.d(TAG, "mBounds: " +mBounds); canvas.drawArc(mBounds, 360 - it.mEndAngle, it.mEndAngle - it.mStartAngle, true, mPiePaint); Log.d(TAG, "Item Being Built: " + it.mLabel); Log.d(TAG, "RectF: " + it.cSliceBounds); Log.d(TAG, "StartAngle> " + it.mStartAngle + " EndAngle> " + it.mEndAngle); canvas.drawArc( it.cSliceBounds, 360 - it.mEndAngle, it.mEndAngle - it.mStartAngle, true, cSlicePaint); // Log.d(TAG, "StartAngle> "+it.mStartAngle+" EndAngle> "+it.mEndAngle); } }
public void draw() { Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); RectF rect = new RectF(8, 8, 192, 192); Paint p = new Paint(); p.setAntiAlias(true); p.setStyle(Paint.Style.STROKE); p.setColor(ColS); p.setStrokeWidth(16); int S = 162; int H = 300; // S canvas.drawArc(rect, -90, S, false, p); p.setStyle(Paint.Style.FILL); p.setColor(ColM); H -= 90; canvas.drawCircle(100, 100, 72, p); p.setColor(ColH); p.setStrokeWidth(2); float sx, sy; sx = (float) (72 * Math.cos(Math.PI * H / 180)); sy = (float) (72 * Math.sin(Math.PI * H / 180)); canvas.drawLine(100, 100, sx + 100, sy + 100, p); ImageView clokImg; clokImg = (ImageView) findViewById(R.id.clokImage); clokImg.setImageBitmap(bitmap); // Hour picker Bitmap ColPickBmp = Bitmap.createBitmap(80, 50, Bitmap.Config.ARGB_8888); Canvas ColPickCanvas = new Canvas(ColPickBmp); ImageView colPickImg = (ImageView) findViewById(R.id.colorHou); rect = new RectF(0, 0, 80, 50); p = new Paint(); p.setAntiAlias(true); p.setStyle(Paint.Style.FILL); p.setColor(ColH); ColPickCanvas.drawRect(rect, p); colPickImg.setImageBitmap(ColPickBmp); // Minute picker ColPickBmp = Bitmap.createBitmap(80, 50, Bitmap.Config.ARGB_8888); ColPickCanvas = new Canvas(ColPickBmp); colPickImg = (ImageView) findViewById(R.id.colorMin); rect = new RectF(0, 0, 80, 50); p = new Paint(); p.setAntiAlias(true); p.setStyle(Paint.Style.FILL); p.setColor(ColM); ColPickCanvas.drawRect(rect, p); colPickImg.setImageBitmap(ColPickBmp); // Second picker ColPickBmp = Bitmap.createBitmap(80, 50, Bitmap.Config.ARGB_8888); ColPickCanvas = new Canvas(ColPickBmp); colPickImg = (ImageView) findViewById(R.id.colorSec); rect = new RectF(0, 0, 80, 50); p = new Paint(); p.setAntiAlias(true); p.setStyle(Paint.Style.FILL); p.setColor(ColS); ColPickCanvas.drawRect(rect, p); colPickImg.setImageBitmap(ColPickBmp); }
/** * 自定义的方法,简单绘制一些基本图形 * * @param mCanvas 把图形画在mCanvas上 */ public void canvasMethod(Canvas mCanvas) { // 创建对应坐标的矩形区域 RectF mArc = new RectF(mX, mY - 70, mX + 50, mY - 20); // 画填充弧,在矩形区域内,从弧的最右边开始,画270度,然后再通过连接圆心来填充 mCanvas.drawArc(mArc, 0, 270, true, mPaint); // 获得icon的Bitmap对象 Bitmap mBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); // 画图片 mCanvas.drawBitmap(mBitmap, mX, mY, mPaint); // 画圆,(x轴,y轴,半径,画笔) mCanvas.drawCircle(mX + 10, mY + 60, 10, mPaint); // 画一条线,(起点横坐标,起点纵坐标,终点横坐标,终点纵坐标,画笔) mCanvas.drawLine(mX, mY + 75, mX + 20, mY + 75, mPaint); // 画多条线,(坐标数组,画笔)坐标数组里每四个值构成一条线 mCanvas.drawLines( new float[] {mX + 50, mY + 45, mX + 50, mY + 75, mX + 60, mY + 45, mX + 60, mY + 75}, mPaint); // 创建对应矩形区域 RectF mOval = new RectF(mX, mY + 80, mX + 60, mY + 110); // 画椭圆 mCanvas.drawOval(mOval, mPaint); /* * Paint qPaint = new Paint(); qPaint.setColor(Color.RED); * mCanvas.drawPaint(qPaint); */ // 重置Path里的所有路径 mPath.reset(); // 设置Path的起点 mPath.moveTo(mX, mY + 120); // 第二个点 mPath.lineTo(screenW - 10, mY + 120); // 第三个点 mPath.lineTo(screenW - 10, mY + 150); // 画出路径,这里画的是三角形 mCanvas.drawPath(mPath, mPaint); // 重置Path里的所有路径 qPath.reset(); // 设置Path的起点 qPath.moveTo(qStartX, qStartY); // 设置贝塞尔曲线的控制点坐标和终点坐标 qPath.quadTo(qControlX, qCOntrolY, qEndX, qEndY); // 画出贝塞尔曲线 mCanvas.drawPath(qPath, qPaint); // 画点 mCanvas.drawPoint(mX, mY + 155, qPaint); // 画多个点,坐标数组每两个值代表一个点的坐标 mCanvas.drawPoints(new float[] {mX, mY + 160, mX + 5, mY + 160, mX + 5, mY + 160}, qPaint); // 画矩形 mCanvas.drawRect(mX, mY + 170, mX + 100, mY + 220, mPaint); // 设置矩形区域 RectF mRect = new RectF(mX, mY + 230, mX + 100, mY + 260); // 画圆角矩形,这个方法的第二第三个参数在后面有图讲解 mCanvas.drawRoundRect(mRect, 10, 10, mPaint); // 画文本 mCanvas.drawText("drawText", mX, mY + 290, mPaint); // 画文本,数组里每两个值代表文本的一个字符的坐标,数组的坐标可以比字符串里的字符多,但不可以少 mCanvas.drawPosText( "哈哈你好", new float[] {mX, mY + 310, mX + 20, mY + 310, mX + 40, mY + 310, mX + 60, mY + 310}, mPaint); // 重置Path tPath.reset(); // 添加一个圆形路径,坐标,半径,方向(顺时针还是逆时针) tPath.addCircle(mX + 10, mY + 340, 10, Path.Direction.CW); // 画出路径 mCanvas.drawPath(tPath, qPaint); // 把文本画在路径上,但不会画出路径 mCanvas.drawTextOnPath("draw", tPath, 30, 0, mPaint); }
private void drawArcs(Canvas canvas, RectF oval, boolean useCenter, Paint paint) { canvas.drawRect(oval, mFramePaint); canvas.drawArc(oval, mStart, mSweep, useCenter, paint); }