下面列出了android.graphics.Canvas#drawPath ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
float canvasRotate;
if (mVerticalMirror) {
canvasRotate = MathUtils.lerp(270, 135f, mProgress);
} else {
canvasRotate = MathUtils.lerp(0f, 135f, mProgress);
}
canvas.save();
canvas.translate(bounds.centerX(), bounds.centerY());
canvas.rotate(canvasRotate);
canvas.drawPath(mPath, mPaint);
canvas.restore();
}
private void drawUnselected(Canvas canvas) {
combinedUnselectedPath.rewind();
// draw any settled, revealing or joining dots
for (int page = 0; page < pageCount; page++) {
int nextXIndex = page == pageCount - 1 ? page : page + 1;
Path unselectedPath = getUnselectedPath(page,
dotCenterX[page],
dotCenterX[nextXIndex],
page == pageCount - 1 ? INVALID_FRACTION : joiningFractions[page],
dotRevealFractions[page]);
unselectedPath.addPath(combinedUnselectedPath);
combinedUnselectedPath.addPath(unselectedPath);
}
// draw any retreating joins
if (retreatingJoinX1 != INVALID_FRACTION) {
Path retreatingJoinPath = getRetreatingJoinPath();
combinedUnselectedPath.addPath(retreatingJoinPath);
}
canvas.drawPath(combinedUnselectedPath, unselectedPaint);
}
public void renderLimitLineLine(Canvas c, LimitLine limitLine, float[] position)
{
mLimitLineSegmentsBuffer[0] = position[0];
mLimitLineSegmentsBuffer[1] = mViewPortHandler.contentTop();
mLimitLineSegmentsBuffer[2] = position[0];
mLimitLineSegmentsBuffer[3] = mViewPortHandler.contentBottom();
mLimitLinePath.reset();
mLimitLinePath.moveTo(mLimitLineSegmentsBuffer[0], mLimitLineSegmentsBuffer[1]);
mLimitLinePath.lineTo(mLimitLineSegmentsBuffer[2], mLimitLineSegmentsBuffer[3]);
mLimitLinePaint.setStyle(Paint.Style.STROKE);
mLimitLinePaint.setColor(limitLine.getLineColor());
mLimitLinePaint.setStrokeWidth(limitLine.getLineWidth());
mLimitLinePaint.setPathEffect(limitLine.getDashPathEffect());
c.drawPath(mLimitLinePath, mLimitLinePaint);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Canvas canvas2 = new Canvas();
//画黄色的圆 满屏幕那种 bitmap
Bitmap bt = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_4444);
canvas2.setBitmap(bt);
//1:绘制红色的矩形
paint.setColor(Color.RED);
canvas2.drawRect(getWidth() / 2 - 200, getHeight() / 2 - 200, getWidth() / 2 + 200, getHeight() / 2 + 300, paint);
//2:saveLayer
canvas2.saveLayer(0, 0, getWidth(), getHeight(), paint,
Canvas.ALL_SAVE_FLAG);
//3:绘制黄色的圆
paint.setColor(Color.YELLOW);
canvas2.drawCircle(getWidth() / 2, getHeight() / 2, 200, paint);
//4:用我的工具类进行 matte
Matrix mar = new Matrix();
mar.postTranslate(-100, 0);
paint.setColor(Color.BLUE);
MatteUtils.setMatte(canvas2, new Circle(getWidth() / 2 + 200, getHeight() / 2 + 200, 200F).getPath()
, mar, paint, PorterDuff.Mode.SRC_OUT);
//5:绘制个浅蓝色的圆看看 上面的工具类对此有影响不
Circle topCircle = new Circle(getWidth() / 2 + 200, getHeight() / 2 + 200, 100F);
paint.setColor(Color.CYAN);
canvas2.drawPath(topCircle.getPath(), paint);
canvas2.restore();//画布提交 对应上面的 保存图层,不然会显示不出来 saveLayer的那层画面;
canvas.drawBitmap(bt, 0, 0, paint);
}
private void drawLeftBottomElevation(Canvas canvas) {
canvas.save();
canvas.translate(mBound.left + mCornerRadius, mBound.bottom - mCornerRadius);
canvas.rotate(90);
mPaint.setShader(mElevationCornerShader);
canvas.drawPath(mElevationCorner, mPaint);
canvas.restore();
}
/**
* 绘制右边的按钮
* @param canvas
*/
private void drawRightIcon(Canvas canvas){
path.reset();
path.moveTo(rightButtonCenter.x- buttonIconSize, rightButtonCenter.y);
path.lineTo(rightButtonCenter.x+ buttonIconSize, rightButtonCenter.y);
canvas.drawPath(path, buttonIconPaint);//划横线
canvas.save();
canvas.rotate(buttonIconDegrees, rightButtonCenter.x, rightButtonCenter.y);//旋转画布,让竖线可以随角度旋转
path.reset();
path.moveTo(rightButtonCenter.x, rightButtonCenter.y- buttonIconSize);
path.lineTo(rightButtonCenter.x, rightButtonCenter.y+ buttonIconSize);
canvas.drawPath(path, buttonIconPaint);//画竖线
canvas.restore();
}
private void clipRightUp(Canvas canvas) {
int width = getWidth();
Path path = new Path();
path.moveTo(width, roundHeight);
path.lineTo(width, 0);
path.lineTo(width-roundWidth, 0);
RectF arc = new RectF(width - roundWidth * 2, 0, width, roundHeight * 2);
float startAngle=-90;
float sweepAngle=90;
path.arcTo(arc, startAngle, sweepAngle);
path.close();
canvas.drawPath(path, paint);
}
@Override
public void draw(Canvas canvas) {
if(mHeight == 0)
return;
Rect bounds = getBounds();
float y = bounds.bottom - mHeight / 2;
if(!isRunning()){
mPath.reset();
mPath.moveTo(bounds.left + mPaddingLeft, y);
mPath.lineTo(bounds.right - mPaddingRight, y);
mPaint.setPathEffect(mEnable ? null : getPathEffect());
mPaint.setColor(mCurColor);
canvas.drawPath(mPath, mPaint);
}
else{
float centerX = (bounds.right + bounds.left - mPaddingRight + mPaddingLeft) / 2f;
float start = centerX * (1f - mAnimProgress) + (bounds.left + mPaddingLeft) * mAnimProgress;
float end = centerX * (1f - mAnimProgress) + (bounds.right + mPaddingRight) * mAnimProgress;
mPaint.setPathEffect(null);
if(mAnimProgress < 1f){
mPaint.setColor(mPrevColor);
mPath.reset();
mPath.moveTo(bounds.left + mPaddingLeft, y);
mPath.lineTo(start, y);
mPath.moveTo(bounds.right - mPaddingRight, y);
mPath.lineTo(end, y);
canvas.drawPath(mPath, mPaint);
}
mPaint.setColor(mCurColor);
mPath.reset();
mPath.moveTo(start, y);
mPath.lineTo(end, y);
canvas.drawPath(mPath, mPaint);
}
}
@Override
public void draw(Canvas canvas) {
if (!ensureValidRect()) {
// nothing to draw
return;
}
// remember the alpha values, in case we temporarily overwrite them
// when we modulate them with mAlpha
final int prevFillAlpha = mFillPaint.getAlpha();
// compute the modulate alpha values
final int currFillAlpha = modulateAlpha(prevFillAlpha);
final CircularProgressBarState st = mCircularProgressBarState;
/* Drawing with a layer is slower than direct drawing, but it
allows us to apply paint effects like alpha and colorfilter to
the result of multiple separate draws. In our case, if the user
asks for a non-opaque alpha value (via setAlpha), and we're
stroking, then we need to apply the alpha AFTER we've drawn
both the fill and the stroke.
*/
/* since we're not using a layer, apply the dither/filter to our
individual paints
*/
mFillPaint.setAlpha(currFillAlpha);
mFillPaint.setColorFilter(mColorFilter);
if (mColorFilter != null && !mCircularProgressBarState.mHasSolidColor) {
mFillPaint.setColor(mAlpha << 24);
}
Path path = buildRing(st);
canvas.drawPath(path, mFillPaint);
mFillPaint.setAlpha(prevFillAlpha);
}
@Override
protected void onDraw(Canvas canvas) {
int x = (getWidth() - width) / 2;
int y = AndroidUtilities.dp(2);
Drawable shadowDrawable = Theme.chat_msgInMediaDrawable.getShadowDrawable();
if (shadowDrawable != null) {
shadowDrawable.setBounds(x, y, width + x, height + y);
shadowDrawable.draw(canvas);
}
int h = AndroidUtilities.displaySize.y;
if (getParent() instanceof View) {
View view = (View) getParent();
h = view.getMeasuredHeight();
}
Theme.chat_msgInMediaDrawable.setTop((int) getY(), h, false, false);
Theme.chat_msgInMediaDrawable.setBounds(x, y, width + x, height + y);
Theme.chat_msgInMediaDrawable.draw(canvas);
Theme.chat_msgTextPaint.setColor(Theme.getColor(Theme.key_chat_messageTextIn));
Theme.chat_msgTextPaint.linkColor = Theme.getColor(Theme.key_chat_messageLinkIn);
canvas.save();
canvas.translate(textX = AndroidUtilities.dp(2 + 9) + x, textY = AndroidUtilities.dp(2 + 9) + y);
if (pressedLink != null) {
canvas.drawPath(urlPath, Theme.chat_urlPaint);
}
if (textLayout != null) {
textLayout.draw(canvas);
}
canvas.restore();
wasDraw = true;
}
private void drawPathBContentBitmap(Bitmap bitmap, Paint pathPaint) {
Canvas mCanvas = new Canvas(bitmap);
//下面开始绘制区域内的内容...
mCanvas.drawPath(getPathDefault(), pathPaint);
mCanvas.drawText("这是在B区域的内容...BBBB", viewWidth - 260, viewHeight - 100, textPaint);
//结束绘制区域内的内容...
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mBlowWavePath, mBlowWavePaint);
canvas.drawPath(mAboveWavePath, mAboveWavePaint);
}
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
mPath.reset();
mPath.moveTo(mWidth, mHeight); //开始起点
mPath.quadTo(mWidth / 2 + 600, mHeight / 2 - 300, mWidth / 2, mHeight / 2); // 控制点、终点
canvas.drawPath(mPath, mPaint);
}
private void drawStroke(Canvas canvas) {
strokePaint.setStrokeWidth(strokeWidth * 2);
strokePaint.setColor(stroke.getColorForState(getDrawableState(), stroke.getDefaultColor()));
cornersMask.setFillType(Path.FillType.WINDING);
canvas.drawPath(cornersMask, strokePaint);
}
@Override
public void renderWarnLine(Canvas canvas) {
super.renderWarnLine(canvas);
List<WarnLine> warnLines = _Axis.getListWarnLins();
if (warnLines == null) {
return;
}
canvas.save();
canvas.clipRect(_rectMain);
for (WarnLine warnLine : warnLines) {
if (warnLine.isEnable()) {
double value = warnLine.getValue();
SingleF_XY xy = _MappingManager.getPxByValue(0, value);
float y = xy.getY();
if (y < _rectMain.top || y > _rectMain.bottom) {
continue;
}
_PaintWarnText.setColor(warnLine.getWarnColor());
_PaintWarnText.setStrokeWidth(warnLine.getWarnLineWidth());
_PaintWarnText.setTextSize(warnLine.getTxtSize());
_PaintWarnPath.setColor(warnLine.getWarnColor());
_PaintWarnPath.setStrokeWidth(warnLine.getWarnLineWidth());
_PathWarn.reset();
_PathWarn.moveTo(_rectMain.left, y);
_PathWarn.lineTo(_rectMain.right, y);
canvas.drawPath(_PathWarn, _PaintWarnPath);
float txtHeight = Utils.textHeight(_PaintWarnText);
canvas.drawText(value + "", _rectMain.left + 10, y - txtHeight, _PaintWarnText);
}
}
canvas.restore();
}
/**
* 横向3D柱形图
* @param barLeft 左边X坐标
* @param barTop 顶部Y坐标
* @param barRight 右边X坐标
* @param barBottom 底部Y坐标
* @param color 柱形颜色
* @param canvas 画布
*/
public void renderHorizontal3DBar(float barLeft,float barTop,
float barRight,float barBottom,
int color,
Canvas canvas)
{
if(Float.compare(barTop, barBottom) == 0)return;
//浅色
int lightColor = DrawHelper.getInstance().getLightColor(color,mAlpha);
getBarPaint().setColor(color);
mPaint3D.setColor(lightColor);
//水平偏移量
float offsetX = (float) getOffsetX();
//垂直偏移量
float offsetY= (float) getOffsetY();
//Shadow
float barLeft2 = MathHelper.getInstance().sub(barLeft , offsetX);
float barTop2 = MathHelper.getInstance().add(barTop , offsetY);
float barRight2 = MathHelper.getInstance().sub(barRight , offsetX) ;
float barBottom2 = MathHelper.getInstance().add(barBottom, offsetY) ;
//右侧边 浅色
mPathRectangle2D.reset();
mPathRectangle2D.moveTo(barRight, barTop);
mPathRectangle2D.lineTo(barRight, barBottom);
mPathRectangle2D.lineTo(barRight2, barBottom2);
mPathRectangle2D.lineTo(barRight2, barTop2);
mPathRectangle2D.close();
canvas.drawPath(mPathRectangle2D,mPaint3D);
//正面
canvas.drawRect(barLeft2, barTop2, barRight2, barBottom2, mPaint3D);
//顶
mPathRectangle2D.reset();
mPathRectangle2D.moveTo(barLeft, barTop);
mPathRectangle2D.lineTo(barLeft2, barTop2);
mPathRectangle2D.lineTo(barRight2, barTop2 );
mPathRectangle2D.lineTo(barRight, barTop );
mPathRectangle2D.close();
canvas.drawPath(mPathRectangle2D,getBarPaint());
//轮廓线
mPaintLine.reset();
mPaintLine.setColor(Color.WHITE);
mPaintLine.setStyle(Style.STROKE);
canvas.drawLine( barLeft2, barTop2, barRight2, barTop2, mPaintLine);
canvas.drawLine( barRight2, barTop2, barRight2, barBottom2,mPaintLine);
canvas.drawLine( barRight, barTop, barRight2,barTop2,mPaintLine);
}
public void draw(Canvas canvas) {
canvas.drawPath(S0, P0);
canvas.drawPath(S1, P1);
canvas.drawPath(S2, P2);
}
@Override
public void draw(Canvas canvas) {
canvas.drawPath(mPath, mPaint);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// set canvas background color
canvas.drawColor(bgColor);
if (count > 360) {
count = count % 360;
} else {
count += 15;
}
dRain += 5;
dRain %= 100;
dy = centerY + 20;
if (drop1) {
dx = centerX - 20;
if (dRain == 95) {
drop2 = true;
drop1 = false;
}
} else if (drop2) {
dx = centerX;
if (dRain == 95) {
drop3 = true;
drop2 = false;
}
} else if (drop3) {
dx = centerX + 20;
if (dRain == 95) {
drop1 = true;
drop3 = false;
}
}
mRainPath.reset();
mRainPath.moveTo(dx, dy);
mRainPath.addArc(new RectF(dx - 5, dy - 5 + dRain, dx + 5, dy + 5 + dRain), 180, -180);
mRainPath.lineTo(dx, dy - 10 + dRain);
mRainPath.close();
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawPath(mRainPath, mPaint);
// drawing cloud with fill
mPaint.setColor(bgColor);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawPath(cloud.getCloud(centerX, centerY, (int) width, count), mPaint);
// mPaint cloud with stroke
mPaint.setColor(strokeColor);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(cloud.getCloud(centerX, centerY, (int) width, count), mPaint);
if (!isStatic) {
postInvalidateDelayed(33);
}
}
public void draw(Canvas canvas) {
if(drawing){
canvas.drawPath(S0, P0);
canvas.drawPath(S1, P1);
canvas.drawPath(S2, P2);
canvas.drawPath(S3, P3);
canvas.drawPath(S4, P4);
}
}