@Override public Surface drawLine(float x0, float y0, float x1, float y1, float width) { canvas.save(); canvas.setStrokeWidth(width); canvas.drawLine(x0, y0, x1, y1); canvas.restore(); return this; }
/** * 画X,Y轴和边框 * * @param canvas */ private void drawAxesLine(Canvas canvas) { XYpaint = new Paint(); XYpaint.setColor(axesLineColor); XYpaint.setAntiAlias(true); XYpaint.setStyle(Style.FILL_AND_STROKE); XYpaint.setStrokeWidth(chartFrameLineSize); // 左侧Y轴 canvas.drawLine( leftPadding + yTextWidth, topPadding + topTextHeight, leftPadding + yTextWidth, height - bottomPadding - xTextHeight, XYpaint); // 三角箭头 Path yTriangle = new Path(); yTriangle.moveTo(leftPadding + yTextWidth - 10, topPadding + topTextHeight); yTriangle.lineTo(leftPadding + yTextWidth, topPadding + topTextHeight - 20); yTriangle.lineTo(leftPadding + yTextWidth + 10, topPadding + topTextHeight); yTriangle.close(); canvas.drawPath(yTriangle, XYpaint); // 下方X轴 canvas.drawLine( leftPadding + yTextWidth, height - bottomPadding - xTextHeight, width - rightPadding, height - bottomPadding - xTextHeight, XYpaint); // 三角箭头 Path xTriangle = new Path(); xTriangle.moveTo(width - rightPadding, height - bottomPadding - xTextHeight + 10); xTriangle.lineTo(width - rightPadding + 15, height - bottomPadding - xTextHeight); xTriangle.lineTo(width - rightPadding, height - bottomPadding - xTextHeight - 10); xTriangle.close(); canvas.drawPath(xTriangle, XYpaint); // 上方封顶 // canvas.drawLine(leftPadding + xTextHeight, topPadding + topTextHeight, width - rightPadding, // topPadding + topTextHeight, XYpaint); // 右侧封边 // canvas.drawLine(width - rightPadding, topPadding + topTextHeight, width - rightPadding, // height - bottomPadding - xTextHeight, XYpaint); }
private void drawBackGroudImageOnGivenCanvas(Canvas canvas) { canvas.drawBitmap(dataHolder.getBackgroundImage(), 0, 0, null); dataHolder.getLightPaint().setColor(Color.rgb(0x83, 0x83, 0x83)); dataHolder.getDarkPaint().setColor(Color.rgb(0x3b, 0x3b, 0x3b)); final int cellSize = dataHolder.getCellSize(); final int sizeY = dataHolder.getGameEngine().getGameField().getSizeY(); final int sizeX = dataHolder.getGameEngine().getGameField().getSizeX(); for (int i = 1; i < sizeX; i++) { canvas.drawLine( i * cellSize - 1, 0, i * cellSize - 1, sizeY * cellSize, dataHolder.getLightPaint()); canvas.drawLine(i * cellSize, 0, i * cellSize, sizeY * cellSize, dataHolder.getDarkPaint()); } for (int i = 1; i < sizeY; i++) { canvas.drawLine(0, i * cellSize, sizeX * cellSize, i * cellSize, dataHolder.getDarkPaint()); canvas.drawLine( 0, i * cellSize + 1, sizeX * cellSize, i * cellSize + 1, dataHolder.getLightPaint()); } }
public void draw(Canvas canvas, MapView mapview, boolean flag) { Projection projection = mapview.getProjection(); Paint paint = new Paint(); Point point = new Point(); projection.toPixels(gp1, point); paint.setColor(color); Point point1 = new Point(); projection.toPixels(gp2, point1); paint.setStrokeWidth(5F); paint.setAlpha(120); canvas.drawLine(point.x, point.y, point1.x, point1.y, paint); super.draw(canvas, mapview, flag); }
/** * Draw the vertical line of the stem * * @param ytop The y location (in pixels) where the top of the staff starts. * @param topstaff The note at the top of the staff. */ private void DrawVerticalLine(Canvas canvas, Paint paint, int ytop, WhiteNote topstaff) { int xstart; if (side == LeftSide) xstart = SheetMusic.LineSpace / 4 + 1; else xstart = SheetMusic.LineSpace / 4 + SheetMusic.NoteWidth; if (direction == Up) { int y1 = ytop + topstaff.Dist(bottom) * SheetMusic.NoteHeight / 2 + SheetMusic.NoteHeight / 4; int ystem = ytop + topstaff.Dist(end) * SheetMusic.NoteHeight / 2; canvas.drawLine(xstart, y1, xstart, ystem, paint); } else if (direction == Down) { int y1 = ytop + topstaff.Dist(top) * SheetMusic.NoteHeight / 2 + SheetMusic.NoteHeight; if (side == LeftSide) y1 = y1 - SheetMusic.NoteHeight / 4; else y1 = y1 - SheetMusic.NoteHeight / 2; int ystem = ytop + topstaff.Dist(end) * SheetMusic.NoteHeight / 2 + SheetMusic.NoteHeight; canvas.drawLine(xstart, y1, xstart, ystem, paint); } }
private void draw() { Canvas canvas = view.getBackground(); canvas.setColor(Color.BLUE); canvas.setAntiAliasing(true); canvas.fill(); canvas.setColor(Color.WHITE); canvas.drawCircle(0, 0, 1); canvas.drawEllipse(0, 0, 3, 2); canvas.setTextSize(20); canvas.drawText(3, 3, "Scale=" + view.getScale()); canvas.drawRectangle(2, 2, 4, 4); canvas.drawImage(2, 2, new Image("res:drawable/spain.jpg")); canvas.drawImage(2, 2, Image.JEDA_LOGO_64x64); canvas.drawLine(1, 1, 3, 1); }
/** * 背景画网格的横线 * * @param canvas */ private void drawGridLine(Canvas canvas) { Paint paint = new Paint(); paint.setColor(gridLineColor); paint.setAntiAlias(true); paint.setStyle(Style.STROKE); paint.setStrokeWidth(gridLineSize); paint.setPathEffect(new DashPathEffect(new float[] {10, 20}, 0)); for (int i = 1; i <= yNum; i++) { // 画网格的横线 canvas.drawLine( leftPadding + yTextWidth, height - bottomPadding - xTextHeight - (spaceYLength * i), width - rightPadding, height - bottomPadding - xTextHeight - (spaceYLength * i), paint); } }
@Override public void onDraw(Canvas canvas) { if (layoutInfo == null) layoutInfo = layout(); float width = containerView.getWidth(); float margin = 10f; canvas.save(); canvas.translate(width - layoutInfo.frame.width() - margin, margin); // draw background canvas.drawRect(layoutInfo.frame, backgroundPaint); canvas.drawRect(layoutInfo.frame, borderPaint); // draw markers float x = layoutInfo.getMarkerX(); float lineLeft = layoutInfo.getMarkerLineLeft(); float lineRight = layoutInfo.getMarkerLineRight(); for (int i = 0; i < entryList.size(); i++) { Entry entry = entryList.get(i); float y = layoutInfo.margin + (layoutInfo.labelHeight + layoutInfo.labelSpacing) * (0.5f + i); canvas.drawLine(lineLeft, y, lineRight, y, entry.getPainter().getDrawConfig().getLinePaint()); tempPoint.set(x, y); IPointRenderer pointRenderer = entry.painter.getPointRenderer(); pointRenderer.drawPoint(canvas, tempPoint, entry.painter.getDrawConfig()); } // draw labels x = layoutInfo.getLabelStartX(); for (int i = 0; i < entryList.size(); i++) { Entry entry = entryList.get(i); float y = layoutInfo.margin + (layoutInfo.labelHeight + layoutInfo.labelSpacing) * i + layoutInfo.labelHeight; canvas.drawText(entry.getLabel(), x, y, labelPaint); } canvas.restore(); }
@Override protected void onDraw(Canvas canvas) { canvas.drawLine(mTextX, mPointerY, mPointerX, mPointerY, mTextPaint); canvas.drawCircle(mPointerX, mPointerY, mPointerRadius, mTextPaint); }
@Override public void drawLine(int x, int y, int x2, int y2, int color) { paint.setColor(color); canvas.drawLine(x, y, x2, y2, paint); }
/* Draw a horizontal beam stem, connecting this stem with the Stem pair. * @param ytop The y location (in pixels) where the top of the staff starts. * @param topstaff The note at the top of the staff. */ private void DrawHorizBarStem(Canvas canvas, Paint paint, int ytop, WhiteNote topstaff) { paint.setStrokeWidth(SheetMusic.NoteHeight / 2); paint.setStrokeCap(Paint.Cap.BUTT); int xstart = 0; int xstart2 = 0; if (side == LeftSide) xstart = SheetMusic.LineSpace / 4 + 1; else if (side == RightSide) xstart = SheetMusic.LineSpace / 4 + SheetMusic.NoteWidth; if (pair.side == LeftSide) xstart2 = SheetMusic.LineSpace / 4 + 1; else if (pair.side == RightSide) xstart2 = SheetMusic.LineSpace / 4 + SheetMusic.NoteWidth; if (direction == Up) { int xend = width_to_pair + xstart2; int ystart = ytop + topstaff.Dist(end) * SheetMusic.NoteHeight / 2; int yend = ytop + topstaff.Dist(pair.end) * SheetMusic.NoteHeight / 2; if (duration == NoteDuration.Eighth || duration == NoteDuration.DottedEighth || duration == NoteDuration.Triplet || duration == NoteDuration.Sixteenth || duration == NoteDuration.ThirtySecond) { canvas.drawLine(xstart, ystart, xend, yend, paint); } ystart += SheetMusic.NoteHeight; yend += SheetMusic.NoteHeight; /* A dotted eighth will connect to a 16th note. */ if (duration == NoteDuration.DottedEighth) { int x = xend - SheetMusic.NoteHeight; double slope = (yend - ystart) * 1.0 / (xend - xstart); int y = (int) (slope * (x - xend) + yend); canvas.drawLine(x, y, xend, yend, paint); } if (duration == NoteDuration.Sixteenth || duration == NoteDuration.ThirtySecond) { canvas.drawLine(xstart, ystart, xend, yend, paint); } ystart += SheetMusic.NoteHeight; yend += SheetMusic.NoteHeight; if (duration == NoteDuration.ThirtySecond) { canvas.drawLine(xstart, ystart, xend, yend, paint); } } else { int xend = width_to_pair + xstart2; int ystart = ytop + topstaff.Dist(end) * SheetMusic.NoteHeight / 2 + SheetMusic.NoteHeight; int yend = ytop + topstaff.Dist(pair.end) * SheetMusic.NoteHeight / 2 + SheetMusic.NoteHeight; if (duration == NoteDuration.Eighth || duration == NoteDuration.DottedEighth || duration == NoteDuration.Triplet || duration == NoteDuration.Sixteenth || duration == NoteDuration.ThirtySecond) { canvas.drawLine(xstart, ystart, xend, yend, paint); } ystart -= SheetMusic.NoteHeight; yend -= SheetMusic.NoteHeight; /* A dotted eighth will connect to a 16th note. */ if (duration == NoteDuration.DottedEighth) { int x = xend - SheetMusic.NoteHeight; double slope = (yend - ystart) * 1.0 / (xend - xstart); int y = (int) (slope * (x - xend) + yend); canvas.drawLine(x, y, xend, yend, paint); } if (duration == NoteDuration.Sixteenth || duration == NoteDuration.ThirtySecond) { canvas.drawLine(xstart, ystart, xend, yend, paint); } ystart -= SheetMusic.NoteHeight; yend -= SheetMusic.NoteHeight; if (duration == NoteDuration.ThirtySecond) { canvas.drawLine(xstart, ystart, xend, yend, paint); } } paint.setStrokeWidth(1); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // draw border mPlotPaint.setStyle(Paint.Style.STROKE); mPlotPaint.setColor(Color.BLACK); canvas.drawRect(mPlotBounds, mPlotPaint); // draw grid mPlotPaint.setColor(gridColor); // x grid if (xTicks != null) { for (float tick : xTicks) { int x = calcX(tick); canvas.drawLine(x, mPlotBounds.bottom, x, mPlotBounds.top, mPlotPaint); } } if (yTicks != null) { for (float tick : yTicks) { int y = calcY(tick); canvas.drawLine(mPlotBounds.left, y, mPlotBounds.right, y, mPlotPaint); } } /* ************************* * draw the data ***************************/ for (Seria s : mData) { stroke(s, canvas); } // draw rectangles that hide out of bounds lines mPlotPaint.setStyle(Style.FILL); mPlotPaint.setColor(Color.WHITE); // left canvas.drawRect(0, 0, mPlotBounds.left, this.getBottom(), mPlotPaint); // bottom canvas.drawRect(0, mPlotBounds.bottom + 1, this.getRight(), this.getBottom(), mPlotPaint); // right canvas.drawRect(mPlotBounds.right + 1, 0, this.getRight(), this.getBottom(), mPlotPaint); // top canvas.drawRect(0, 0, this.getRight(), mPlotBounds.top, mPlotPaint); // x ticks mPlotPaint.setStyle(Paint.Style.STROKE); mPlotPaint.setTextAlign(Align.CENTER); mPlotPaint.setTextSize(xTickTextHeight); mPlotPaint.setColor(xTickColor); if (xTicks != null) { for (float tick : xTicks) { textFigure( xTickFormat.format(tick).toString(), calcX(tick), mPlotBounds.bottom + xTickTextHeight, canvas); } } mPlotPaint.setTextSize(labelTextHeight); mPlotPaint.setTextAlign(Align.RIGHT); if (labelX != null) { textFigure(labelX, mPlotBounds.right, mPlotBounds.bottom + axisXSpacing, canvas); } // y ticks mPlotPaint.setTextAlign(Align.CENTER); mPlotPaint.setTextSize(yTickTextHeight); mPlotPaint.setColor(yTickColor); if (yTicks != null) { for (float tick : yTicks) { textFigure( yTickFormat.format(tick).toString(), mPlotBounds.left - yTickTextHeight, calcY(tick), 90, canvas); } } mPlotPaint.setTextSize(labelTextHeight); mPlotPaint.setTextAlign(Align.RIGHT); if (labelX != null) { textFigure(labelX, mPlotBounds.right, mPlotBounds.bottom + axisXSpacing, canvas); } mPlotPaint.setTextAlign(Align.LEFT); if (labelY != null) { textFigure(labelY, mPlotBounds.left - axisYSpacing, mPlotBounds.top, 90, canvas); } }
@Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { // Reset paint opacity paint.setAlpha(255); // Ignore everything but rectangles in bounds mode if (boundsMode) { if (localName.equals("rect")) { Float x = getFloatAttr("x", atts); if (x == null) { x = 0f; } Float y = getFloatAttr("y", atts); if (y == null) { y = 0f; } Float width = getFloatAttr("width", atts); Float height = getFloatAttr("height", atts); bounds = new RectF(x, y, x + width, y + height); } return; } if (localName.equals("svg")) { int width = (int) Math.ceil(getFloatAttr("width", atts)); int height = (int) Math.ceil(getFloatAttr("height", atts)); canvas = picture.beginRecording(width, height); } else if (localName.equals("defs")) { // Ignore } else if (localName.equals("linearGradient")) { gradient = doGradient(true, atts); } else if (localName.equals("radialGradient")) { gradient = doGradient(false, atts); } else if (localName.equals("stop")) { if (gradient != null) { float offset = getFloatAttr("offset", atts); String styles = getStringAttr("style", atts); StyleSet styleSet = new StyleSet(styles); String colorStyle = styleSet.getStyle("stop-color"); int color = Color.BLACK; if (colorStyle != null) { if (colorStyle.startsWith("#")) { color = Integer.parseInt(colorStyle.substring(1), 16); } else { color = Integer.parseInt(colorStyle, 16); } } String opacityStyle = styleSet.getStyle("stop-opacity"); if (opacityStyle != null) { float alpha = Float.parseFloat(opacityStyle); int alphaInt = Math.round(255 * alpha); color |= (alphaInt << 24); } else { color |= 0xFF000000; } gradient.positions.add(offset); gradient.colors.add(color); } } else if (localName.equals("g")) { // Check to see if this is the "bounds" layer if ("bounds".equalsIgnoreCase(getStringAttr("id", atts))) { boundsMode = true; } if (hidden) { hiddenLevel++; // Util.debug("Hidden up: " + hiddenLevel); } // Go in to hidden mode if display is "none" if ("none".equals(getStringAttr("display", atts))) { if (!hidden) { hidden = true; hiddenLevel = 1; // Util.debug("Hidden up: " + hiddenLevel); } } pushTransform(atts); } else if (!hidden && localName.equals("rect")) { Float x = getFloatAttr("x", atts); if (x == null) { x = 0f; } Float y = getFloatAttr("y", atts); if (y == null) { y = 0f; } Float width = getFloatAttr("width", atts); Float height = getFloatAttr("height", atts); Float rx = getFloatAttr("rx", atts); Float ry = getFloatAttr("ry", atts); if (rx == null) { if (ry == null) rx = ry = 0f; else rx = ry; } else { if (ry == null) ry = rx; } if (rx > width / 2) rx = width * 0.5f; if (ry > height / 2) ry = height * 0.5f; pushTransform(atts); Properties props = new Properties(atts); if (doFill(props, gradientMap)) { doLimits(x, y, width, height); if (rx > 0 && ry > 0) canvas.drawRoundRect(new RectF(x, y, x + width, y + height), rx, ry, paint); else canvas.drawRect(x, y, x + width, y + height, paint); } if (doStroke(props, gradientMap)) { if (rx > 0 && ry > 0) canvas.drawRoundRect(new RectF(x, y, x + width, y + height), rx, ry, paint); else canvas.drawRect(x, y, x + width, y + height, paint); } popTransform(); } else if (!hidden && localName.equals("line")) { Float x1 = getFloatAttr("x1", atts); Float x2 = getFloatAttr("x2", atts); Float y1 = getFloatAttr("y1", atts); Float y2 = getFloatAttr("y2", atts); Properties props = new Properties(atts); if (doStroke(props, gradientMap)) { pushTransform(atts); doLimits(x1, y1); doLimits(x2, y2); canvas.drawLine(x1, y1, x2, y2, paint); popTransform(); } } else if (!hidden && localName.equals("circle")) { Float centerX = getFloatAttr("cx", atts); Float centerY = getFloatAttr("cy", atts); Float radius = getFloatAttr("r", atts); if (centerX != null && centerY != null && radius != null) { pushTransform(atts); Properties props = new Properties(atts); if (doFill(props, gradientMap)) { doLimits(centerX - radius, centerY - radius); doLimits(centerX + radius, centerY + radius); canvas.drawCircle(centerX, centerY, radius, paint); } if (doStroke(props, gradientMap)) { canvas.drawCircle(centerX, centerY, radius, paint); } popTransform(); } } else if (!hidden && localName.equals("ellipse")) { Float centerX = getFloatAttr("cx", atts); Float centerY = getFloatAttr("cy", atts); Float radiusX = getFloatAttr("rx", atts); Float radiusY = getFloatAttr("ry", atts); if (centerX != null && centerY != null && radiusX != null && radiusY != null) { pushTransform(atts); Properties props = new Properties(atts); rect.set(centerX - radiusX, centerY - radiusY, centerX + radiusX, centerY + radiusY); if (doFill(props, gradientMap)) { doLimits(centerX - radiusX, centerY - radiusY); doLimits(centerX + radiusX, centerY + radiusY); canvas.drawOval(rect, paint); } if (doStroke(props, gradientMap)) { canvas.drawOval(rect, paint); } popTransform(); } } else if (!hidden && (localName.equals("polygon") || localName.equals("polyline"))) { NumberParse numbers = getNumberParseAttr("points", atts); if (numbers != null) { Path p = new Path(); ArrayList<Float> points = numbers.numbers; if (points.size() > 1) { pushTransform(atts); Properties props = new Properties(atts); p.moveTo(points.get(0), points.get(1)); for (int i = 2; i < points.size(); i += 2) { float x = points.get(i); float y = points.get(i + 1); p.lineTo(x, y); } // Don't close a polyline if (localName.equals("polygon")) { p.close(); } if (doFill(props, gradientMap)) { doLimits(p); canvas.drawPath(p, paint); } if (doStroke(props, gradientMap)) { canvas.drawPath(p, paint); } popTransform(); } } } else if (!hidden && localName.equals("path")) { Path p = doPath(getStringAttr("d", atts)); pushTransform(atts); Properties props = new Properties(atts); if (doFill(props, gradientMap)) { doLimits(p); canvas.drawPath(p, paint); } if (doStroke(props, gradientMap)) { canvas.drawPath(p, paint); } popTransform(); } else if (!hidden) { // Log.d(TAG, "UNRECOGNIZED SVG COMMAND: " + localName); } }
/** * 自定义的方法,简单绘制一些基本图形 * * @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); }
/** * 画折线图,网格竖线,X坐标值 * * @param canvas */ private void drawChartLine(Canvas canvas) { Paint paintChart = new Paint(); paintChart.setColor(chartLineColor); paintChart.setAntiAlias(true); paintChart.setStyle(Style.STROKE); paintChart.setStrokeWidth(chartLineSize); Paint paintGrid = new Paint(); paintGrid.setColor(gridLineColor); paintGrid.setAntiAlias(true); paintGrid.setStyle(Style.STROKE); paintGrid.setStrokeWidth(gridLineSize); Paint paintTopText = new Paint(); paintTopText.setColor(axesLineColor); paintTopText.setAntiAlias(true); paintTopText.setSubpixelText(true); paintTopText.setTypeface(Typeface.MONOSPACE); paintTopText.setTextSize(topTextSize); paintTopText.setTextAlign(Align.CENTER); Paint paintBottomText = new Paint(); paintBottomText.setColor(axesLineColor); paintBottomText.setAntiAlias(true); paintBottomText.setSubpixelText(true); paintBottomText.setTypeface(Typeface.MONOSPACE); paintBottomText.setTextSize(bottomTextSize); paintBottomText.setTextAlign(Align.CENTER); Paint paintPoint = new Paint(); paintPoint.setColor(pointColor); paintPoint.setAntiAlias(true); paintPoint.setStyle(Style.FILL); pressedPaint = new Paint(); pressedPaint.setColor(pressedColor); pressedPaint.setAntiAlias(true); pressedPaint.setStyle(Style.FILL); Paint paintShape = new Paint(); paintShape.setStyle(Style.FILL); paintShape.setColor(shapeColor); paintShape.setAlpha(shapeAlpha); if (axesData.size() > 0) { for (int i = 0; i < axesData.size(); i++) { if (i < axesData.size() - 1) { // 画折线 canvas.drawLine( axesData.get(i).X, axesData.get(i).Y, axesData.get(i + 1).X, axesData.get(i + 1).Y, paintChart); if (isShapeShow) { // 画阴影 Path path = new Path(); path.moveTo(axesData.get(i).X, height - bottomPadding - xTextHeight); path.lineTo(axesData.get(i).X, axesData.get(i).Y + chartLineSize / 2); path.lineTo(axesData.get(i + 1).X, axesData.get(i + 1).Y + chartLineSize / 2); path.lineTo(axesData.get(i + 1).X, height - bottomPadding - xTextHeight); canvas.drawPath(path, paintShape); } } // 画网格竖线 // canvas.drawLine(axesData.get(i).X, height - bottomPadding - xTextHeight, // axesData.get(i).X, topPadding + topTextHeight, paintGrid); // 写X轴坐标的刻度值 if (!TextUtils.isEmpty(axesData.get(i).getxText())) { setTextSizeForWidth( paintBottomText, spaceXLength - 10, axesData.get(i).getxText()); // 10为相邻日期文字间隔 canvas.drawText( axesData.get(i).getxText(), axesData.get(i).X, height - bottomPadding - xTextHeight / 2, paintBottomText); } // 写顶部的刻度值 if (!TextUtils.isEmpty(axesData.get(i).getTopText())) { // 取消了顶部隔年显示的数据 // canvas.drawText(axesData.get(i).getTopText(), axesData.get(i).X, topPadding, // paintTopText); } // 画数据点 if (pointBitmap == null) { canvas.drawCircle(axesData.get(i).X, axesData.get(i).Y, pointSize + 1, paintChart); canvas.drawCircle(axesData.get(i).X, axesData.get(i).Y, pointSize, paintPoint); } else { Matrix matrix = new Matrix(); canvas.drawBitmap(pointBitmap, matrix, paintPoint); } } // 画最后一个数据的网格竖线 // canvas.drawLine(axesData.get(axesData.size() - 1).X, height - bottomPadding - xTextHeight, // axesData.get(axesData.size() - 1).X, // topPadding + topTextHeight, paintGrid); // 写X轴坐标的最后一个值的刻度值 /* * canvas.drawText(axesData.get(axesData.size() - 1).X + "", * axesData.get(axesData.size() - 1).X, height - bottomPadding - * xTextHeight / 2, paintBottomText); */ // 写顶部的最后一个刻度值 // canvas.drawText("2014", axesData.get(axesData.size() - 1).X, // topPadding, paintTopText); // 画数最后一个据点 if (pointBitmap == null) { // canvas.drawCircle(axesData.get(axesData.size() - 1).X, // axesData.get(axesData.size() - 1).Y, pointSize + 1, // paintChart); // canvas.drawCircle(axesData.get(axesData.size() - 1).X, // axesData.get(axesData.size() - 1).Y, pointSize, paintPoint); } else { Matrix matrix = new Matrix(); canvas.drawBitmap(pointBitmap, matrix, paintPoint); } } if (currentPressedPoint != null) { // canvas.drawCircle(currentPressedPoint.X, currentPressedPoint.Y, pointSize, pressedPaint); } }
public void drawLine(int a, int b, int c, int d) { canvas.drawLine(a, b, c, d, paint); }
@Override public void render(MainGamePanel game, Canvas canvas, GameState gameState) { setzIndex(5); if (gameState.isStateDefeat()) { canvas.clipRect(0, 0, canvas.getWidth(), canvas.getHeight(), Region.Op.REPLACE); int border = canvas.getWidth() / 15; paint.setTextSize(TextSizeCalculator.getDefaultTextSize(canvas)); paint.setTextAlign(Paint.Align.CENTER); paint.setAntiAlias(true); paint.setStrokeWidth(2f); paint.setColorFilter(MyColors.getFilterGui()); paint.setColor(MyColors.getGuiElementColor()); canvas.drawLine(border, border, canvas.getWidth() - border, border, paint); canvas.drawLine(border, border, border, canvas.getHeight() - border, paint); canvas.drawLine( border, canvas.getHeight() - border, canvas.getWidth() - border, canvas.getHeight() - border, paint); canvas.drawLine( canvas.getWidth() - border, border, canvas.getWidth() - border, canvas.getHeight() - border, paint); int headingHeight = TextSizeCalculator.getHeightFromTextSize(paint.getTextSize()); int highScoreTextSize = (int) (TextSizeCalculator.getDefaultTextSize(canvas) * 0.7); ExtStorage ext = (ExtStorage) game.getElements().getComponent(Elements.EXTERNAL_STORAGE_PROVIDER); String heading = ""; String secondaryScoreName = ""; String secondaryScoreShortName = ""; String secondaryScoreValue = ""; int secondaryScoreIntValue = 0; List<String> highScoreList = null; // Normal if (gameState.getPreviousState() == GameState.STATE_GAME) { heading = "LOL NOOB"; secondaryScoreName = "level"; secondaryScoreShortName = ". level"; // Level value secondaryScoreIntValue = LevelList.getLevelId() + 1; secondaryScoreValue = Integer.toString(secondaryScoreIntValue); if (ext != null) { highScoreList = ext.getHighScore(ExtStorage.HIGH_SCORE_NORMAL_FILE); } // Arcade } else if (gameState.getPreviousState() == GameState.STATE_ARCADE) { heading = "NICE TRY!"; secondaryScoreName = "seconds"; secondaryScoreShortName = " sec"; // Seconds value secondaryScoreIntValue = (int) ((gameState.getArcadeTimeEnd() - gameState.getArcadeTimeStart()) / 1000.0f); secondaryScoreValue = Integer.toString(secondaryScoreIntValue); if (ext != null) { highScoreList = ext.getHighScore(ExtStorage.HIGH_SCORE_ARCADE_FILE); } } // Heading paint.setTextAlign(Paint.Align.LEFT); canvas.drawText(heading, border * 1.5f, border * 2.5f + headingHeight * 0.5f, paint); paint.setTextAlign(Paint.Align.CENTER); paint.setTextSize(paint.getTextSize() * 0.9f); canvas.drawText(secondaryScoreValue, canvas.getWidth() - border * 2.5f, border * 2.7f, paint); paint.setTextSize(paint.getTextSize() * 0.4f); canvas.drawText(secondaryScoreName, canvas.getWidth() - border * 2.5f, border * 3.5f, paint); canvas.drawLine( canvas.getWidth() - border * 4f, border, canvas.getWidth() - border * 4f, border * 4f, paint); // Restore defaults paint.setTextSize(TextSizeCalculator.getDefaultTextSize(canvas)); paint.setTextAlign(Paint.Align.CENTER); if (highScoreList != null) { int lineHeight = TextSizeCalculator.getHeightFromTextSize(highScoreTextSize) * 2; int left = border; int right = canvas.getWidth() - border; int baseTop = border * 4; int counter = 0; paint.setColor(MyColors.getGuiElementColor()); paint.setTextSize(highScoreTextSize); canvas.drawLine( left, baseTop + (counter * lineHeight), right, baseTop + (counter * lineHeight), paint); counter++; canvas.drawText( "HIGH SCORE", canvas.getWidth() / 2, baseTop + (counter * lineHeight) - lineHeight / 4, paint); canvas.drawLine( left, baseTop + (counter * lineHeight), right, baseTop + (counter * lineHeight), paint); counter++; for (String score : highScoreList) { if (score != null && !score.equals("") && score.split("\\|").length == 2) { canvas.drawLine( left, baseTop + (counter * lineHeight), right, baseTop + (counter * lineHeight), paint); String highScoreValue = score.split("\\|")[0]; String secondaryHighScoreValue = score.split("\\|")[1]; // Arcade if (gameState.getPreviousState() == GameState.STATE_ARCADE) { Score scoreComponent = (Score) game.getElements().getComponent(Elements.SCORE); if (Integer.parseInt(highScoreValue) == scoreComponent.getScore() && Integer.parseInt(secondaryHighScoreValue) == secondaryScoreIntValue) { paint.setColor(MyColors.getGuiNewHighScoreColor()); } } // Score values paint.setTextAlign(Paint.Align.RIGHT); canvas.drawText( highScoreValue, canvas.getWidth() / 2 - border / 2, baseTop + (counter * lineHeight) - lineHeight / 4, paint); canvas.drawText( secondaryHighScoreValue + secondaryScoreShortName, canvas.getWidth() - border * 1.5f, baseTop + (counter * lineHeight) - lineHeight / 4, paint); paint.setColor(MyColors.getGuiElementColor()); // Line canvas.drawLine( canvas.getWidth() / 2, baseTop + (counter * lineHeight), canvas.getWidth() / 2, baseTop + ((counter - 1) * lineHeight), paint); counter++; } } } paint.setTextAlign(Paint.Align.CENTER); buttonLeft = 2 * border; buttonRight = canvas.getWidth() - 2 * border; buttonRetryTop = canvas.getHeight() - 7 * border; buttonRetryBottom = canvas.getHeight() - 5 * border; buttonMainMenuTop = canvas.getHeight() - 4 * border; buttonMainMenuBottom = canvas.getHeight() - 2 * border; // Draw buttons canvas.drawRect(buttonLeft, buttonRetryTop, buttonRight, buttonRetryBottom, paint); paint.setStyle(Paint.Style.STROKE); canvas.drawRect(buttonLeft, buttonMainMenuTop, buttonRight, buttonMainMenuBottom, paint); // Draw button texts paint.setStyle(Paint.Style.FILL); paint.setColor(MyColors.getGuiElementTextColor()); int textHeight = TextSizeCalculator.getHeightFromTextSize(highScoreTextSize); canvas.drawText( "RETRY", canvas.getWidth() / 2, getTextY(buttonRetryTop, buttonRetryBottom, textHeight), paint); paint.setColor(MyColors.getGuiElementColor()); canvas.drawText( "QUIT TO MENU", canvas.getWidth() / 2, getTextY(buttonMainMenuTop, buttonMainMenuBottom, textHeight), paint); } }
@Override protected boolean drawBubble(Canvas canvas) { if (messageLayout == null) { requestLayout(); return false; } boolean isAnimated = false; if (messageLayout.isForwarded) { canvas.drawText("Forwarded message", 0, getPx(16), messageLayout.forwardingPaint); canvas.drawText("From", 0, getPx(35), messageLayout.forwardingPaint); canvas.drawText( messageLayout.forwarderNameMeasured, messageLayout.forwardOffset, getPx(35), messageLayout.senderPaint); canvas.save(); canvas.translate(0, getPx(19) * 2); messageLayout.layout.draw(canvas); canvas.restore(); } else { if (!messageLayout.isOut & messageLayout.isGroup) { canvas.drawText(messageLayout.senderNameMeasured, 0, getPx(16), messageLayout.senderPaint); canvas.save(); canvas.translate(0, getPx(19)); messageLayout.layout.draw(canvas); canvas.restore(); } else { messageLayout.layout.draw(canvas); } } if (messageLayout.showState) { if (state == MessageState.PENDING) { canvas.save(); canvas.translate( messageLayout.layoutRealWidth - getPx(12), messageLayout.layoutHeight - getPx(12) - getPx(3)); canvas.drawCircle(getPx(6), getPx(6), getPx(6), clockIconPaint); double time = (System.currentTimeMillis() / 15.0) % (12 * 60); double angle = (time / (6 * 60)) * Math.PI; int x = (int) (Math.sin(-angle) * getPx(4)); int y = (int) (Math.cos(-angle) * getPx(4)); canvas.drawLine(getPx(6), getPx(6), getPx(6) + x, getPx(6) + y, clockIconPaint); x = (int) (Math.sin(-angle * 12) * getPx(5)); y = (int) (Math.cos(-angle * 12) * getPx(5)); canvas.drawLine(getPx(6), getPx(6), getPx(6) + x, getPx(6) + y, clockIconPaint); canvas.restore(); clockOutPaint.setColor(COLOR_NORMAL); isAnimated = true; } else if (state == MessageState.READED && prevState == MessageState.SENT && (SystemClock.uptimeMillis() - stateChangeTime < STATE_ANIMATION_TIME)) { long animationTime = SystemClock.uptimeMillis() - stateChangeTime; float progress = easeStateFade(animationTime / (float) STATE_ANIMATION_TIME); int offset = (int) (getPx(5) * progress); int alphaNew = (int) (progress * 255); bounds( stateSent, messageLayout.layoutRealWidth - stateSent.getIntrinsicWidth() - offset, messageLayout.layoutHeight - stateSent.getIntrinsicHeight() - getPx(3)); stateSent.setAlpha(255); stateSent.draw(canvas); bounds( stateHalfCheck, messageLayout.layoutRealWidth - stateHalfCheck.getIntrinsicWidth() + getPx(5) - offset, messageLayout.layoutHeight - stateHalfCheck.getIntrinsicHeight() - getPx(3)); stateHalfCheck.setAlpha(alphaNew); stateHalfCheck.draw(canvas); clockOutPaint.setColor(COLOR_NORMAL); isAnimated = true; } else { Drawable stateDrawable = getStateDrawable(state); bounds( stateDrawable, messageLayout.layoutRealWidth - stateDrawable.getIntrinsicWidth(), messageLayout.layoutHeight - stateDrawable.getIntrinsicHeight() - getPx(3)); stateDrawable.setAlpha(255); stateDrawable.draw(canvas); if (state == MessageState.READED) { bounds( stateSent, messageLayout.layoutRealWidth - stateSent.getIntrinsicWidth() - getPx(5), messageLayout.layoutHeight - stateDrawable.getIntrinsicHeight() - getPx(3)); stateSent.setAlpha(255); stateSent.draw(canvas); } if (state == MessageState.FAILURE) { clockOutPaint.setColor(COLOR_ERROR); } else { clockOutPaint.setColor(COLOR_NORMAL); } } } else { clockOutPaint.setColor(COLOR_IN); } canvas.drawText( wireframe.date, messageLayout.layoutRealWidth - messageLayout.timeWidth + getPx(6), messageLayout.layoutHeight - getPx(4), clockOutPaint); return isAnimated; }
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); }
@Override protected void onDraw(Canvas canvas) { float ringWidth = textHeight + 4; int height = getMeasuredHeight(); int width = getMeasuredWidth(); int px = width / 2; int py = height / 2; Point center = new Point(px, py); int radius = Math.min(px, py) - 2; RectF boundingBox = new RectF(center.x - radius, center.y - radius, center.x + radius, center.y + radius); RectF innerBoundingBox = new RectF( center.x - radius + ringWidth, center.y - radius + ringWidth, center.x + radius - ringWidth, center.y + radius - ringWidth); float innerRadius = innerBoundingBox.height() / 2; RadialGradient borderGradient = new RadialGradient( px, py, radius, borderGradientColors, borderGradientPositions, TileMode.CLAMP); Paint pgb = new Paint(); pgb.setShader(borderGradient); Path outerRingPath = new Path(); outerRingPath.addOval(boundingBox, Direction.CW); canvas.drawPath(outerRingPath, pgb); LinearGradient skyShader = new LinearGradient( center.x, innerBoundingBox.top, center.x, innerBoundingBox.bottom, skyHorizonColorFrom, skyHorizonColorTo, TileMode.CLAMP); Paint skyPaint = new Paint(); skyPaint.setShader(skyShader); LinearGradient groundShader = new LinearGradient( center.x, innerBoundingBox.top, center.x, innerBoundingBox.bottom, groundHorizonColorFrom, groundHorizonColorTo, TileMode.CLAMP); Paint groundPaint = new Paint(); groundPaint.setShader(groundShader); float tiltDegree = pitch; while (tiltDegree > 90 || tiltDegree < -90) { if (tiltDegree > 90) tiltDegree = -90 + (tiltDegree - 90); if (tiltDegree < -90) tiltDegree = 90 - (tiltDegree + 90); } float rollDegree = roll; while (rollDegree > 180 || rollDegree < -180) { if (rollDegree > 180) rollDegree = -180 + (rollDegree - 180); if (rollDegree < -180) rollDegree = 180 - (rollDegree + 180); } Path skyPath = new Path(); skyPath.addArc(innerBoundingBox, -rollDegree, (180 + (2 * rollDegree))); canvas.rotate(-tiltDegree, px, py); canvas.drawOval(innerBoundingBox, groundPaint); canvas.drawPath(skyPath, skyPaint); canvas.drawPath(skyPath, markerPaint); int markWidth = radius / 3; int startX = center.x - markWidth; int endX = center.x + markWidth; Log.d("PAARV ", "Roll " + String.valueOf(rollDegree)); Log.d("PAARV ", "Pitch " + String.valueOf(tiltDegree)); double h = innerRadius * Math.cos(Math.toRadians(90 - tiltDegree)); double justTiltX = center.x - h; float pxPerDegree = (innerBoundingBox.height() / 2) / 45f; for (int i = 90; i >= -90; i -= 10) { double ypos = justTiltX + i * pxPerDegree; if ((ypos < (innerBoundingBox.top + textHeight)) || (ypos > innerBoundingBox.bottom - textHeight)) continue; canvas.drawLine(startX, (float) ypos, endX, (float) ypos, markerPaint); int displayPos = (int) (tiltDegree - i); String displayString = String.valueOf(displayPos); float stringSizeWidth = textPaint.measureText(displayString); canvas.drawText( displayString, (int) (center.x - stringSizeWidth / 2), (int) (ypos) + 1, textPaint); } markerPaint.setStrokeWidth(2); canvas.drawLine( center.x - radius / 2, (float) justTiltX, center.x + radius / 2, (float) justTiltX, markerPaint); markerPaint.setStrokeWidth(1); Path rollArrow = new Path(); rollArrow.moveTo(center.x - 3, (int) innerBoundingBox.top + 14); rollArrow.lineTo(center.x, (int) innerBoundingBox.top + 10); rollArrow.moveTo(center.x + 3, innerBoundingBox.top + 14); rollArrow.lineTo(center.x, innerBoundingBox.top + 10); canvas.drawPath(rollArrow, markerPaint); String rollText = String.valueOf(rollDegree); double rollTextWidth = textPaint.measureText(rollText); canvas.drawText( rollText, (float) (center.x - rollTextWidth / 2), innerBoundingBox.top + textHeight + 2, textPaint); canvas.restore(); canvas.save(); canvas.rotate(180, center.x, center.y); for (int i = -180; i < 180; i += 10) { if (i % 30 == 0) { String rollString = String.valueOf(i * -1); float rollStringWidth = textPaint.measureText(rollString); PointF rollStringCenter = new PointF(center.x - rollStringWidth / 2, innerBoundingBox.top + 1 + textHeight); canvas.drawText(rollString, rollStringCenter.x, rollStringCenter.y, textPaint); } else { canvas.drawLine( center.x, (int) innerBoundingBox.top, center.x, (int) innerBoundingBox.top + 5, markerPaint); } canvas.rotate(10, center.x, center.y); } canvas.restore(); canvas.save(); canvas.rotate(-1 * (bearing), px, py); double increment = 22.5; for (double i = 0; i < 360; i += increment) { CompassDirection cd = CompassDirection.values()[(int) (i / 22.5)]; String headString = cd.toString(); float headStringWidth = textPaint.measureText(headString); PointF headStringCenter = new PointF(center.x - headStringWidth / 2, boundingBox.top + 1 + textHeight); if (i % increment == 0) canvas.drawText(headString, headStringCenter.x, headStringCenter.y, textPaint); else canvas.drawLine( center.x, (int) boundingBox.top, center.x, (int) boundingBox.top + 3, markerPaint); canvas.rotate((int) increment, center.x, center.y); } canvas.restore(); RadialGradient glassShader = new RadialGradient( px, py, (int) innerRadius, glassGradientColors, glassGradientPositions, TileMode.CLAMP); Paint glassPaint = new Paint(); glassPaint.setShader(glassShader); canvas.drawOval(innerBoundingBox, glassPaint); canvas.drawOval(boundingBox, circlePaint); circlePaint.setStrokeWidth(2); canvas.drawOval(innerBoundingBox, circlePaint); canvas.restore(); }