下面列出了android.graphics.Path#quadTo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected Path buildClippingPath() {
Path roundedPath = new Path();
float widthDiff = width * 1f / (waveCount * 2);
float startingHeight = height;
roundedPath.moveTo(0, startingHeight);
float nextCPX = widthDiff;
float nextCPY = startingHeight + roundedEdgeHeight;
float nextX = nextCPX + widthDiff;
for (int i = 0; i < waveCount; i++) {
roundedPath.quadTo(nextCPX, nextCPY, nextX, startingHeight);
nextCPX = nextX + widthDiff;
nextX = nextCPX + widthDiff;
}
roundedPath.lineTo(height + 100, startingHeight);
roundedPath.lineTo(height + 100, 0);
roundedPath.lineTo(0, 0);
roundedPath.close();
return roundedPath;
}
protected static Path composeRoundedRectPath(RectF rect, float tl, float tr, float bl, float br) {
Path path = new Path();
tl = tl < 0 ? 0 : tl;
tr = tr < 0 ? 0 : tr;
bl = bl < 0 ? 0 : bl;
br = br < 0 ? 0 : br;
path.moveTo(rect.left + tl, rect.top);
path.lineTo(rect.right - tr, rect.top);
path.quadTo(rect.right, rect.top, rect.right, rect.top + tr);
path.lineTo(rect.right, rect.bottom - br);
path.quadTo(rect.right, rect.bottom, rect.right - br, rect.bottom);
path.lineTo(rect.left + bl, rect.bottom);
path.quadTo(rect.left, rect.bottom, rect.left, rect.bottom - bl);
path.lineTo(rect.left, rect.top + tl);
path.quadTo(rect.left, rect.top, rect.left + tl, rect.top);
path.close();
return path;
}
private void buildPath(Path path) {
//Important!
path.reset();
path.moveTo(adjustedPoints[0].x, adjustedPoints[0].y);
int pointSize = adjustedPoints.length;
for (int i = 0; i < adjustedPoints.length - 1; i++) {
float pointX = (adjustedPoints[i].x + adjustedPoints[i + 1].x) / 2;
float pointY = (adjustedPoints[i].y + adjustedPoints[i + 1].y) / 2;
float controlX = adjustedPoints[i].x;
float controlY = adjustedPoints[i].y;
path.quadTo(controlX, controlY, pointX, pointY);
}
path.quadTo(adjustedPoints[pointSize - 1].x, adjustedPoints[pointSize - 1].y, adjustedPoints[pointSize - 1].x,
adjustedPoints[pointSize - 1].y);
}
private void drawBubblePath(Canvas canvas, float triangleCenterX, float height, float width) {
final Path path = new Path();
int padding = 3;
final Rect rect = new Rect(padding, padding, (int) width - padding, (int) (height - dpToPx(BUBBLE_ARROW_HEIGHT)) - padding);
final float roundRectHeight = (height - dpToPx(BUBBLE_ARROW_HEIGHT)) / 2;
path.moveTo(rect.left + roundRectHeight, rect.top);
path.lineTo(rect.right - roundRectHeight, rect.top);
path.quadTo(rect.right, rect.top, rect.right, rect.top + roundRectHeight);
path.lineTo(rect.right, rect.bottom - roundRectHeight);
path.quadTo(rect.right, rect.bottom, rect.right - roundRectHeight, rect.bottom);
path.lineTo(triangleCenterX + dpToPx(BUBBLE_ARROW_WIDTH) / 2f, height - dpToPx(BUBBLE_ARROW_HEIGHT) - padding);
path.lineTo(triangleCenterX, height - padding);
path.lineTo(triangleCenterX - dpToPx(BUBBLE_ARROW_WIDTH) / 2f, height - dpToPx(BUBBLE_ARROW_HEIGHT) - padding);
path.lineTo(rect.left + roundRectHeight, rect.bottom);
path.quadTo(rect.left, rect.bottom, rect.left, rect.bottom - roundRectHeight);
path.lineTo(rect.left, rect.top + roundRectHeight);
path.quadTo(rect.left, rect.top, rect.left + roundRectHeight, rect.top);
path.close();
canvas.drawPath(path, settings.paintBubble);
}
private void buildClippingPath() {
roundedPath = new Path();
float widthDiff = width * 1f / (waveCount * 2);
float startingHeight = height;
roundedPath.moveTo(0, startingHeight);
float nextCPX = widthDiff;
float nextCPY = startingHeight + roundedEdgeHeight;
float nextX = nextCPX + widthDiff;
float nextY = startingHeight;
for (int i = 0; i < waveCount; i++) {
roundedPath.quadTo(nextCPX, nextCPY, nextX, nextY);
nextCPX = nextX + widthDiff;
nextCPY =
(i % 2 != 0) ? startingHeight + roundedEdgeHeight : startingHeight - roundedEdgeHeight;
nextX = nextCPX + widthDiff;
}
roundedPath.lineTo(width + 100, startingHeight);
roundedPath.lineTo(width + 100, 0);
roundedPath.lineTo(0, 0);
roundedPath.close();
}
/**
* 绘制五角星
*
* @param num 角数量
* @param startAngle 初始角度
* @return
*/
private Path createStarPath(int num, int startAngle)
{
final Path path = new Path();
int angle = 360 / num;
int roundSize = 5;//圆角弧度
int offsetAngle = angle / 2;
path.moveTo(getViewCenterX() + mStarOutMidR * cos(startAngle - roundSize), getViewCenterY() + mStarOutMidR * sin(startAngle - roundSize));
for (int i = 0; i < num; i++)
{
int value = angle * i + startAngle;
path.lineTo(getViewCenterX() + mStarOutMidR * cos(value - roundSize), getViewCenterY() + mStarOutMidR * sin(value - roundSize));
//圆角
path.quadTo(getViewCenterX() + mStarOutR * cos(value), getViewCenterY() + mStarOutR * sin(value), getViewCenterX() + mStarOutMidR * cos(value + roundSize), getViewCenterY() + mStarOutMidR * sin(value + roundSize));
path.lineTo(getViewCenterX() + mStarInR * cos(value + offsetAngle - roundSize), getViewCenterY() + mStarInR * sin(value + offsetAngle - roundSize));
//内圆角
path.quadTo(getViewCenterX() + mStarInMidR * cos(value + offsetAngle), getViewCenterY() + mStarInMidR * sin(value + offsetAngle), getViewCenterX() + mStarInR * cos(value + offsetAngle + roundSize), getViewCenterY() + mStarInR * sin(value + offsetAngle + roundSize));
}
path.close();
return path;
}
private void updatePath() {
final Path path = mPath;
path.rewind();
final ArrayList<PointF> points = mPoints;
final int count = points.size();
if (count <= 1)
return;
float preX = 0;
float preY = 0;
for (int i = 0; i < count; i++) {
final float x = points.get(i).x;
final float y = points.get(i).y;
if (i == 0) {
path.moveTo(x, y);
preX = x;
preY = y;
} else {
path.quadTo(preX, preY, (x + preX) / 2, (y + preY) / 2);
preX = x;
preY = y;
}
}
}
protected static Path composeRoundedRectPath(RectF rect, float tl, float tr, float bl, float br) {
Path path = new Path();
tl = tl < 0 ? 0 : tl;
tr = tr < 0 ? 0 : tr;
bl = bl < 0 ? 0 : bl;
br = br < 0 ? 0 : br;
path.moveTo(rect.left + tl, rect.top);
path.lineTo(rect.right - tr, rect.top);
path.quadTo(rect.right, rect.top, rect.right, rect.top + tr);
path.lineTo(rect.right, rect.bottom - br);
path.quadTo(rect.right, rect.bottom, rect.right - br, rect.bottom);
path.lineTo(rect.left + bl, rect.bottom);
path.quadTo(rect.left, rect.bottom, rect.left, rect.bottom - bl);
path.lineTo(rect.left, rect.top + tl);
path.quadTo(rect.left, rect.top, rect.left + tl, rect.top);
path.close();
return path;
}
/**
* 绘制五叶草
*
* @param path 路径
* @param num 角数量
* @param startAngle 初始角度
* @return
*/
private void createStarPath(Path path, int num, int startAngle)
{
path.reset();
int angle = 360 / num;
int roundSize = 5;//圆角弧度
int offsetAngle = angle / 2;
path.moveTo(getViewCenterX() + mStarOutMidR * cos(startAngle - roundSize), getViewCenterY() + mStarOutMidR * sin(startAngle - roundSize));
for (int i = 0; i < num; i++)
{
int value = angle * i + startAngle;
path.lineTo(getViewCenterX() + mStarOutMidR * cos(value - roundSize), getViewCenterY() + mStarOutMidR * sin(value - roundSize));
//圆角
path.quadTo(getViewCenterX() + mStarOutR * cos(value), getViewCenterY() + mStarOutR * sin(value), getViewCenterX() + mStarOutMidR * cos(value + roundSize), getViewCenterY() + mStarOutMidR * sin(value + roundSize));
path.lineTo(getViewCenterX() + mStarInR * cos(value + offsetAngle - roundSize), getViewCenterY() + mStarInR * sin(value + offsetAngle - roundSize));
//内圆角
path.quadTo(getViewCenterX() + mStarInMidR * cos(value + offsetAngle), getViewCenterY() + mStarInMidR * sin(value + offsetAngle), getViewCenterX() + mStarInR * cos(value + offsetAngle + roundSize), getViewCenterY() + mStarInR * sin(value + offsetAngle + roundSize));
}
path.close();
}
public static Path composeRoundedRectPath(float left, float top, float right, float bottom, float radius) {
Log.d(TAG, "composeRoundedRectPath() called with: left = [" + left + "], top = [" + top + "], right = [" + right + "], bottom = [" + bottom + "], radius = [" + radius + "]");
Path path = new Path();
path.moveTo(left + radius, top);
path.lineTo(right - radius, top);
path.quadTo(right - radius / 2, top - radius / 2, right, top + radius);
path.lineTo(right, bottom - radius);
path.quadTo(right, bottom, right - radius, bottom);
path.lineTo(left + radius, bottom);
path.quadTo(left, bottom, left, bottom - radius);
path.lineTo(left, top + radius);
path.quadTo(left, top, left + radius, top);
path.close();
return path;
}
protected static Path composeRoundedRectPath(RectF rect, float tl, float tr, float bl, float br) {
Path path = new Path();
tl = tl < 0 ? 0 : tl;
tr = tr < 0 ? 0 : tr;
bl = bl < 0 ? 0 : bl;
br = br < 0 ? 0 : br;
path.moveTo(rect.left + tl, rect.top);
path.lineTo(rect.right - tr, rect.top);
path.quadTo(rect.right, rect.top, rect.right, rect.top + tr);
path.lineTo(rect.right, rect.bottom - br);
path.quadTo(rect.right, rect.bottom, rect.right - br, rect.bottom);
path.lineTo(rect.left + bl, rect.bottom);
path.quadTo(rect.left, rect.bottom, rect.left, rect.bottom - bl);
path.lineTo(rect.left, rect.top + tl);
path.quadTo(rect.left, rect.top, rect.left + tl, rect.top);
path.close();
return path;
}
@Override
protected void onDraw(Canvas canvas) {
//这里做下判断,只有图片的宽高大于设置的圆角距离的时候才进行裁剪
int maxLeft = Math.max(leftTopRadius, leftBottomRadius);
int maxRight = Math.max(rightTopRadius, rightBottomRadius);
int minWidth = maxLeft + maxRight;
int maxTop = Math.max(leftTopRadius, rightTopRadius);
int maxBottom = Math.max(leftBottomRadius, rightBottomRadius);
int minHeight = maxTop + maxBottom;
if (width >= minWidth && height > minHeight) {
Path path = new Path();
//四个角:右上,右下,左下,左上
path.moveTo(leftTopRadius, 0);
path.lineTo(width - rightTopRadius, 0);
path.quadTo(width, 0, width, rightTopRadius);
path.lineTo(width, height - rightBottomRadius);
path.quadTo(width, height, width - rightBottomRadius, height);
path.lineTo(leftBottomRadius, height);
path.quadTo(0, height, 0, height - leftBottomRadius);
path.lineTo(0, leftTopRadius);
path.quadTo(0, 0, leftTopRadius, 0);
canvas.clipPath(path);
}
super.onDraw(canvas);
}
public void onPath(View view) {
//see http://blog.csdn.net/tianjian4592/article/details/47067161
Path path = new Path();
path.moveTo(START_POINT[0], START_POINT[1]);
path.quadTo(RIGHT_CONTROL_POINT[0], RIGHT_CONTROL_POINT[1], BOTTOM_POINT[0], BOTTOM_POINT[1]);
path.quadTo(LEFT_CONTROL_POINT[0], LEFT_CONTROL_POINT[1], START_POINT[0], START_POINT[1]);
path.close();
ViewAnimator.animate(imageView4path)
.repeatMode(ViewAnimator.RESTART)
.repeatCount(ViewAnimator.INFINITE)
.path(path)
.start();
}
Path getPath() {
Path path = new Path();
path.moveTo(LEFT_CURVE[0].x, LEFT_CURVE[0].y);
path.quadTo(LEFT_CURVE[1].x, LEFT_CURVE[1].y, LEFT_CURVE[2].x, LEFT_CURVE[2].y);
path.cubicTo(CURVE_CTR[3].x, CURVE_CTR[3].y, CURVE_CTR[2].x, CURVE_CTR[2].y, RIGHT_CURVE[2].x, RIGHT_CURVE[2].y);
path.quadTo(RIGHT_CURVE[1].x, RIGHT_CURVE[1].y, RIGHT_CURVE[0].x, RIGHT_CURVE[0].y);
path.cubicTo(CURVE_CTR[0].x, CURVE_CTR[0].y, CURVE_CTR[1].x, CURVE_CTR[1].y, LEFT_CURVE[0].x, LEFT_CURVE[0].y);
return path;
}
private void straightLineBefore(Path path) {
path.moveTo(0, yCalculator.height / 2);
float controlY = yCalculator.y(HorizontalStraightLineLength * 3);
path.quadTo(HorizontalStraightLineLength, yCalculator.height / 2,
HorizontalStraightLineLength * 2, (yCalculator.height / 2 + controlY) / 2);
path.quadTo(HorizontalStraightLineLength * 3, controlY, HorizontalStraightLineLength
* 4, yCalculator.y(HorizontalStraightLineLength * 4));
}
@Override
protected Path curveToPath() {
Path path = new Path();
path.moveTo(mStartX, mStartY);
path.quadTo(mControlX, mControlY, mEndX, mEndY);
return path;
}
private static Path createQuad(float controlX, float controlY) {
final Path path = new Path();
path.moveTo(0.0f, 0.0f);
path.quadTo(controlX, controlY, 1.0f, 1.0f);
return path;
}
@Override
protected void onDraw(@NonNull final Canvas canvas) {
final Rect frame = mFrameRect;
if (frame == null) {
return;
}
final int width = getWidth();
final int height = getHeight();
final float top = frame.getTop();
final float left = frame.getLeft();
final float right = frame.getRight();
final float bottom = frame.getBottom();
final float frameCornersSize = mFrameCornersSize;
final float frameCornersRadius = mFrameCornersRadius;
final Path path = mPath;
if (frameCornersRadius > 0) {
final float normalizedRadius =
Math.min(frameCornersRadius, Math.max(frameCornersSize - 1, 0));
path.reset();
path.moveTo(left, top + normalizedRadius);
path.quadTo(left, top, left + normalizedRadius, top);
path.lineTo(right - normalizedRadius, top);
path.quadTo(right, top, right, top + normalizedRadius);
path.lineTo(right, bottom - normalizedRadius);
path.quadTo(right, bottom, right - normalizedRadius, bottom);
path.lineTo(left + normalizedRadius, bottom);
path.quadTo(left, bottom, left, bottom - normalizedRadius);
path.lineTo(left, top + normalizedRadius);
path.moveTo(0, 0);
path.lineTo(width, 0);
path.lineTo(width, height);
path.lineTo(0, height);
path.lineTo(0, 0);
canvas.drawPath(path, mMaskPaint);
path.reset();
path.moveTo(left, top + frameCornersSize);
path.lineTo(left, top + normalizedRadius);
path.quadTo(left, top, left + normalizedRadius, top);
path.lineTo(left + frameCornersSize, top);
path.moveTo(right - frameCornersSize, top);
path.lineTo(right - normalizedRadius, top);
path.quadTo(right, top, right, top + normalizedRadius);
path.lineTo(right, top + frameCornersSize);
path.moveTo(right, bottom - frameCornersSize);
path.lineTo(right, bottom - normalizedRadius);
path.quadTo(right, bottom, right - normalizedRadius, bottom);
path.lineTo(right - frameCornersSize, bottom);
path.moveTo(left + frameCornersSize, bottom);
path.lineTo(left + normalizedRadius, bottom);
path.quadTo(left, bottom, left, bottom - normalizedRadius);
path.lineTo(left, bottom - frameCornersSize);
canvas.drawPath(path, mFramePaint);
} else {
path.reset();
path.moveTo(left, top);
path.lineTo(right, top);
path.lineTo(right, bottom);
path.lineTo(left, bottom);
path.lineTo(left, top);
path.moveTo(0, 0);
path.lineTo(width, 0);
path.lineTo(width, height);
path.lineTo(0, height);
path.lineTo(0, 0);
canvas.drawPath(path, mMaskPaint);
path.reset();
path.moveTo(left, top + frameCornersSize);
path.lineTo(left, top);
path.lineTo(left + frameCornersSize, top);
path.moveTo(right - frameCornersSize, top);
path.lineTo(right, top);
path.lineTo(right, top + frameCornersSize);
path.moveTo(right, bottom - frameCornersSize);
path.lineTo(right, bottom);
path.lineTo(right - frameCornersSize, bottom);
path.moveTo(left + frameCornersSize, bottom);
path.lineTo(left, bottom);
path.lineTo(left, bottom - frameCornersSize);
canvas.drawPath(path, mFramePaint);
}
}
private void operate(Path finalPath, String method, StringTokenizer args) {
float x0 = 0.0f;
float y0 = 0.0f;
float x1 = 0.0f;
float y1 = 0.0f;
float x2 = 0.0f;
float y2 = 0.0f;
try {
int index = 0;
while (args.hasMoreTokens()) {
String s = args.nextToken();
if (TextUtils.isEmpty(s)) {
continue;
}
if (index == 0) {
x0 = Float.valueOf(s);
}
if (index == 1) {
y0 = Float.valueOf(s);
}
if (index == 2) {
x1 = Float.valueOf(s);
}
if (index == 3) {
y1 = Float.valueOf(s);
}
if (index == 4) {
x2 = Float.valueOf(s);
}
if (index == 5) {
y2 = Float.valueOf(s);
}
index++;
}
} catch (Exception e) {
}
SVGAPoint currentPoint = new SVGAPoint(0.0f, 0.0f, 0.0f);
if ("M".equals(method)) {
finalPath.moveTo(x0, y0);
currentPoint = new SVGAPoint(x0, y0, 0.0f);
} else if ("m".equals(method)) {
finalPath.rMoveTo(x0, y0);
currentPoint = new SVGAPoint(currentPoint.x + x0, currentPoint.y + y0, 0.0f);
}
if ("L".equals(method)) {
finalPath.lineTo(x0, y0);
} else if ("l".equals(method)) {
finalPath.rLineTo(x0, y0);
}
if ("C".equals(method)) {
finalPath.cubicTo(x0, y0, x1, y1, x2, y2);
} else if ("c".equals(method)) {
finalPath.rCubicTo(x0, y0, x1, y1, x2, y2);
}
if ("Q".equals(method)) {
finalPath.quadTo(x0, y0, x1, y1);
} else if ("q".equals(method)) {
finalPath.rQuadTo(x0, y0, x1, y1);
}
if ("H".equals(method)) {
finalPath.lineTo(x0, currentPoint.y);
} else if ("h".equals(method)) {
finalPath.rLineTo(x0, 0f);
}
if ("V".equals(method)) {
finalPath.lineTo(currentPoint.x, x0);
} else if ("v".equals(method)) {
finalPath.rLineTo(0f, x0);
}
if ("Z".equals(method)) {
finalPath.close();
} else if ("z".equals(method)) {
finalPath.close();
}
}
private Path drawBubble(RectF myRect, float topLeftDiameter, float topRightDiameter, float bottomRightDiameter, float bottomLeftDiameter) {
final Path path = new Path();
topLeftDiameter = topLeftDiameter < 0 ? 0 : topLeftDiameter;
topRightDiameter = topRightDiameter < 0 ? 0 : topRightDiameter;
bottomLeftDiameter = bottomLeftDiameter < 0 ? 0 : bottomLeftDiameter;
bottomRightDiameter = bottomRightDiameter < 0 ? 0 : bottomRightDiameter;
final float spacingLeft = this.position == POSITION_LEFT ? arrowHeightPx : 0;
final float spacingTop = this.position == POSITION_TOP ? arrowHeightPx : 0;
final float spacingRight = this.position == POSITION_RIGHT ? arrowHeightPx : 0;
final float spacingBottom = this.position == POSITION_BOTTOM ? arrowHeightPx : 0;
final float left = spacingLeft + myRect.left;
final float top = spacingTop + myRect.top;
final float right = myRect.right - spacingRight;
final float bottom = myRect.bottom - spacingBottom;
final float centerX = (myRect.left + myRect.right) * positionPer;
path.moveTo(left + topLeftDiameter / 2f, top);
//LEFT, TOP
if (position == POSITION_TOP) {
path.lineTo(centerX - arrowWidthPx, top);
path.lineTo(centerX, myRect.top);
path.lineTo(centerX + arrowWidthPx, top);
}
path.lineTo(right - topRightDiameter / 2f, top);
path.quadTo(right, top, right, top + topRightDiameter / 2);
//RIGHT, TOP
if (position == POSITION_RIGHT) {
path.lineTo(right, bottom-(bottom*(1-positionPer))- arrowWidthPx);
path.lineTo(myRect.right, bottom-(bottom*(1-positionPer)));
path.lineTo(right, bottom-(bottom*(1-positionPer)) + arrowWidthPx);
}
path.lineTo(right, bottom - bottomRightDiameter / 2);
path.quadTo(right, bottom, right - bottomRightDiameter / 2, bottom);
//RIGHT, BOTTOM
if (position == POSITION_BOTTOM) {
path.lineTo(centerX + arrowWidthPx, bottom);
path.lineTo(centerX, myRect.bottom);
path.lineTo(centerX - arrowWidthPx, bottom);
}
path.lineTo(left + bottomLeftDiameter / 2, bottom);
path.quadTo(left, bottom, left, bottom - bottomLeftDiameter / 2);
//LEFT, BOTTOM
if (position == POSITION_LEFT) {
path.lineTo(left, bottom-(bottom*(1-positionPer))+ arrowWidthPx);
path.lineTo(myRect.left, bottom-(bottom*(1-positionPer)));
path.lineTo(left, bottom-(bottom*(1-positionPer)) - arrowWidthPx);
}
path.lineTo(left, top + topLeftDiameter / 2);
path.quadTo(left, top, left + topLeftDiameter / 2, top);
path.close();
return path;
}