private void drawAxis(Canvas canvas) { mPaint.setAntiAlias(true); // draw y axis mPaint.setColor(yAxisColor); mPaint.setStrokeWidth(yAxisScale); canvas.drawLine(yAxisXPosition, yAxisStartYPosition, yAxisXPosition, yAxisEndYPosition, mPaint); // draw x axis mPaint.setColor(xAxisColor); mPaint.setStrokeWidth(xAxisScale); canvas.drawLine(xAxisStartXPosition, xAxisYPosition, xAxisEndXPosition, xAxisYPosition, mPaint); // draw average line int y = (int) (averageWithdrawal / maximumWithdrawal * (yAxisEndYPosition - yAxisStartYPosition)); y = yAxisEndYPosition - 1 - y; canvas.drawLine(yAxisXPosition, y, xAxisEndXPosition, y, mPaint); textPaint.setAntiAlias(true); textPaint.setTypeface(Typeface.SERIF); textPaint.setColor(Color.BLACK); // draw the second title textPaint.setTextAlign(Paint.Align.LEFT); textPaint.setTextSize(titleSecondSize); canvas.drawText(averageText, titleSecondSize, titleSecondY, textPaint); // draw the y axis valus textPaint.setTextAlign(Paint.Align.RIGHT); textPaint.setTextSize(yPaintScale); int x = yAxisXPosition - 2 * yAxisScale; y = yAxisEndYPosition; for (int i = 0, count = yValues.size(); i < count; i++) { canvas.drawText(yValues.get(i), x, y, textPaint); y -= yCellLength; } // draw the x axis values textPaint.setTextAlign(Paint.Align.LEFT); textPaint.setTextSize(xPaintScale); x = yAxisXPosition + xCellLength; for (int count = dataList.size(), i = count - 1; i >= 0; i--) { // draw the vertical line canvas.drawLine(x, yAxisStartYPosition, x, yAxisEndYPosition, mPaint); mpath.reset(); String temp = convertDateFormat(dataList.get(i).getLastUpdate()); String[] strs = temp.split(" "); mpath.moveTo(x - yPaintScale / 2, yAxisEndYPosition + 2 * yPaintScale); mpath.lineTo(x + 3 * yPaintScale, yAxisEndYPosition); canvas.drawTextOnPath(strs[0], mpath, 0, 0, textPaint); canvas.drawTextOnPath(strs[1], mpath, 0, yPaintScale, textPaint); x += xCellLength; } }
public void drawWithoutTransform(Canvas canvas) { if (mode != null) { textPaint.setXfermode(mode.xfermode); borderPaint.setXfermode(mode.xfermode); } if (stroke != null) { canvas.drawTextOnPath(text, stroke.getPath(), 0.0F, 0.0F, textPaint); if (hasBorder) { canvas.drawTextOnPath(text, stroke.getPath(), 0.0F, 0.0F, borderPaint); } } }
public void onDraw(Canvas canvas, int measuredWidth, int measuredHeight) { if (!visual || text == null) { return; } float actualDistance = distance + height / 2; calcOffset(actualDistance, measuredWidth, measuredHeight); rectPaint.setColor(backgroundColor); if (alpha != 0) { rectPaint.setAlpha(alpha); } rectPaint.setStrokeWidth(height); rectPath.reset(); rectPath.moveTo(startPosX, startPosY); rectPath.lineTo(endPosX, endPosY); canvas.drawPath(rectPath, rectPaint); textPaint.setTextSize(textSize); textPaint.setColor(textColor); textPaint.getTextBounds(text, 0, text.length(), textBound); canvas.drawTextOnPath( text, rectPath, (1.4142135f * actualDistance) / 2 - textBound.width() / 2, textBound.height() / 2, textPaint); }
/** * Method used to draw a line on map. * * @param advancedStyle * @param currentZoomLevel the current zoom level of the map view */ public void drawLines(AdvancedStyle advancedStyle, byte currentZoomLevel) { Paint stroke = StyleManager.getStrokePaint4Style(advancedStyle); Paint textStroke = StyleManager.getTextStrokePaint4Style(advancedStyle); ShapeWriter wr = new ShapeWriter(pointTransformer); wr.setRemoveDuplicatePoints(true); wr.setDecimation(style4Table.decimationFactor); if (stroke == null && textStroke == null) { // Can't draw return; } while (geometryIterator.hasNext()) { Geometry geom = geometryIterator.next(); DrawableShape shape = wr.toShape(geom); if (stroke != null) { shape.draw(canvas, stroke); } // draw labels if zoom level fits and data is available if (currentZoomLevel >= advancedStyle.label_minZoom && textStroke != null && shape instanceof PathShape && geom.getUserData() != null) { Object userData = geom.getUserData(); canvas.drawTextOnPath( userData.toString(), ((PathShape) shape).getPath(), LABEL_H_OFFSET, LABEL_V_OFFSET, textStroke); } } }
private void drawText(Canvas canvas) { mPaint.setColor(Color.BLACK); mPaint.setTextSize(60); mPaint.setStyle(Style.STROKE); mPaint.setStrokeWidth(2); mPaint.setTextSkewX(-0.5f); canvas.drawText(HELLO_ANDROID, 0, 60, mPaint); canvas.drawTextOnPath(HELLO_ANDROID, mBasePath, hOffset, 0, mPaint); hOffset += 5; if (hOffset > 200 * Math.PI) hOffset = 0; invalidate(); }
@Override public void onDraw(Canvas canvas) { int w = canvas.getWidth(); int h = canvas.getHeight(); Rect clip = canvas.getClipBounds(); int cw = clip.width(); int ch = clip.height(); if (cw > 0 && ch > 0) { w = cw; h = ch; } int inset = 20; float startX = 0 + inset; float startY = 0 + inset; float stopX = w - inset; float stopY = h - inset; float origoX = startX + MARGIN_X_AXIS; float origoY = stopY - MARGIN_Y_AXIS; float xmaxX = stopX - origoX - X_LINE_MARGIN; float maxProb = -origoY - Y_LINE_MARGIN + startY; // X and Y axisbug canvas.drawLine(origoX, origoY, origoX, startY, axisPaint); canvas.drawLine(origoX, origoY, stopX, origoY, axisPaint); // canvas.drawLine(startX, startY, stopX, stopY, axisPaint); canvas.drawText("Battery drain %/s", origoX, stopY - 20, textPaint); Path path = new Path(); path.addRect(new RectF(clip), Path.Direction.CW); canvas.drawTextOnPath("Probability", path, w * 2 + h + h / 2, +40, textPaint); if (xVals == null || yVals == null) { canvas.drawText("With", stopX, startY + 30, withTextPaint); canvas.drawText("Without", stopX, startY + 60, withoutTextPaint); } else { String anShort = appName; if (anShort == null) anShort = "Unknown"; int dot = anShort.lastIndexOf('.'); if (dot > 0) { int lastPart = anShort.length() - dot; if (lastPart > 7) anShort = anShort.substring(dot + 1); else { if (anShort.length() > 15 + 3) { anShort = "..." + anShort.substring(anShort.length() - 15); } } } String withString = anShort; String withoutString = anShort; switch (type) { case BUG: withString = anShort + " " + getContext().getString(R.string.runninghere); withoutString = anShort + " " + getContext().getString(R.string.runningelsewhere); break; case HOG: withString = anShort + " " + getContext().getString(R.string.running); withoutString = anShort + " " + getContext().getString(R.string.notrunning); break; case MODEL: withString = getContext().getString(R.string.onmodel) + " " + anShort; withoutString = getContext().getString(R.string.onothermodels); break; case OS: withString = getContext().getString(R.string.withos) + " " + anShort; withoutString = getContext().getString(R.string.withotheroses); break; case SIMILAR: withString = getContext().getString(R.string.withsimilarapps); withoutString = getContext().getString(R.string.withdifferentapps); break; default: } canvas.drawText(withString, stopX, startY + 30, withTextPaint); canvas.drawText(withoutString, stopX, startY + 60, withoutTextPaint); float xmax = 0.0f; float ymax = 0.0f; for (int i = 0; i < xVals.length && i < yVals.length; ++i) { float next = (float) xVals[i]; float nexty = (float) yVals[i]; if (next > xmax) xmax = next; if (nexty > ymax) ymax = nexty; } for (int i = 0; i < xValsWithout.length && i < yValsWithout.length; ++i) { float next = (float) xValsWithout[i]; float nexty = (float) yValsWithout[i]; if (next > xmax) xmax = next; if (nexty > ymax) ymax = nexty; } String xmaxS = xmax + ""; if (xmaxS.length() > 6) xmaxS = xmaxS.substring(0, 6); String ymaxS = ymax + ""; if (ymaxS.length() > 4) ymaxS = ymaxS.substring(0, 4); canvas.drawText(xmaxS + "", xmaxX, stopY - 20, textPaint); canvas.drawText(ymaxS + "", startX, startY + 30, textPaint); float lastX = 0.0f, lastY = 0.0f; for (int i = 0; i < xVals.length && i < yVals.length; ++i) { float x = (float) xVals[i]; float y = (float) yVals[i]; if (y == 0.0) continue; x /= xmax; // Now x is a fraction of max. All x values are from origoX to w // - origoX - offset: x = origoX + X_LINE_MARGIN + x * xmaxX; y /= ymax; y = origoY + Y_LINE_MARGIN + y * maxProb; if (lastX != 0 || lastY != 0) { canvas.drawLine(lastX, lastY, x, y, withPaint); } lastX = x; lastY = y; } lastX = 0.0f; lastY = 0.0f; for (int i = 0; i < xValsWithout.length && i < yValsWithout.length; ++i) { float x = (float) xValsWithout[i]; float y = (float) yValsWithout[i]; if (y == 0.0) continue; x /= xmax; x = origoX + X_LINE_MARGIN + x * xmaxX; y /= ymax; y = origoY + Y_LINE_MARGIN + y * maxProb; if (lastX != 0 || lastY != 0) { canvas.drawLine(lastX, lastY, x, y, withoutPaint); } lastX = x; lastY = y; } } }
@Override public void onLoadFinished(Loader<Cursor> ldr, Cursor data) { int l = data.getCount(); data.moveToFirst(); long maxCents = 0; long minCents = Long.MAX_VALUE; long maxTime = 0; long minTime = Long.MAX_VALUE; for (int i = 0; i != l; ++i) { long cents = data.getLong(0); if (cents > maxCents) maxCents = cents; if (cents < minCents) minCents = cents; long time = data.getLong(3); if (time > maxTime) maxTime = time; if (time < minTime) minTime = time; data.moveToNext(); } ImageView view = (ImageView) getView(); int cardSpacing = getActivity().getResources().getDimensionPixelSize(R.dimen.cardSpacing); int cardPadding = getActivity().getResources().getDimensionPixelSize(R.dimen.cardPadding); int textSize = cardPadding * 2; int width = getActivity().getWindow().getWindowManager().getDefaultDisplay().getWidth() - 2 * (cardSpacing) - 2 * (cardPadding); Log.d("Budget", "GraphFragment.onLoadFinished(): width=" + width); int height = getActivity().getResources().getDimensionPixelSize(R.dimen.graphHeight); Log.d("Budget", "GraphFragment.onLoadFinished(): height=" + height); Bitmap chart = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas chartCanvas = new Canvas(chart); chartCanvas.drawColor(Color.TRANSPARENT); Paint pen = new Paint(); pen.setColor(0xFF000000); pen.setTextAlign(Paint.Align.CENTER); pen.setTextSize(textSize); Paint brush = new Paint(); brush.setDither(true); brush.setHinting(Paint.HINTING_ON); brush.setStyle(Paint.Style.STROKE); float stroke = getActivity().getResources().getDimension(R.dimen.graphStroke); brush.setStrokeWidth(stroke); int currentEnvelope = -1; Path currentPath = null; float usableHeight = height - (2 * stroke) - textSize - cardPadding; float usableWidth = width - (2 * stroke) - textSize; data.moveToFirst(); for (int i = 0; i != l; ++i) { int envelope = data.getInt(1); long cents = data.getLong(0); long time = data.getLong(3); float pointHeight = usableHeight - (float) ((cents - minCents) * usableHeight / ((double) (maxCents - minCents))) + stroke; float pointPosition = (float) ((time - minTime) * usableWidth / ((double) (maxTime - minTime))) + stroke + textSize; Log.d("Budget", "GraphFragment.onLoadFinished(): envelope=" + envelope); Log.d("Budget", "GraphFragment.onLoadFinished(): envelope.name=" + data.getString(4)); Log.d("Budget", "GraphFragment.onLoadFinished(): cents=" + cents); Log.d("Budget", "GraphFragment.onLoadFinished(): pointHeight=" + pointHeight); Log.d("Budget", "GraphFragment.onLoadFinished(): time=" + time); Log.d("Budget", "GraphFragment.onLoadFinished(): pointPosition=" + pointPosition); if (envelope != currentEnvelope) { if (currentPath != null) { currentPath.rLineTo(usableWidth, 0); chartCanvas.drawPath(currentPath, brush); } int color = data.getInt(2); // brush = new Paint(brush); brush.setColor(color); currentEnvelope = envelope; currentPath = new Path(); currentPath.moveTo(pointPosition, pointHeight); } else { currentPath.lineTo(pointPosition, pointHeight); } data.moveToNext(); } if (currentPath != null) { currentPath.rLineTo(usableWidth, 0); chartCanvas.drawPath(currentPath, brush); } Path side = new Path(); side.moveTo(textSize, height); side.lineTo(textSize, 0); chartCanvas.drawTextOnPath( getActivity().getString(R.string.envelopeDetails_balance), side, 0, 0, pen); Path bottom = new Path(); bottom.moveTo(0, height - cardPadding); bottom.lineTo(width, height - cardPadding); chartCanvas.drawTextOnPath(getActivity().getString(R.string.graph_time), bottom, 0, 0, pen); if (maxTime != 0) { Date maxTimeD = new Date(maxTime); Date minTimeD = new Date(minTime); DateFormat timeFormat = android.text.format.DateFormat.getDateFormat(getActivity()); pen.setTextAlign(Paint.Align.LEFT); chartCanvas.drawTextOnPath(timeFormat.format(minTimeD), bottom, 0, 0, pen); pen.setTextAlign(Paint.Align.RIGHT); chartCanvas.drawTextOnPath(timeFormat.format(maxTimeD), bottom, 0, 0, pen); } view.setImageBitmap(chart); }
/** * 自定义的方法,简单绘制一些基本图形 * * @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); }
public void drawTextOverCanvas(RenderingContext rc, Canvas cv, boolean useEnglishNames) { int size = rc.textToDraw.size(); // 1. Sort text using text order Collections.sort( rc.textToDraw, new Comparator<TextDrawInfo>() { @Override public int compare(TextDrawInfo object1, TextDrawInfo object2) { return object1.textOrder - object2.textOrder; } }); RectF r = new RectF(0, 0, rc.width, rc.height); r.inset(-100, -100); QuadTree<TextDrawInfo> nonIntersectedBounds = new QuadTree<TextDrawInfo>(r, 4, 0.6f); for (int i = 0; i < size; i++) { TextDrawInfo text = rc.textToDraw.get(i); if (text.text != null && text.text.length() > 0) { if (useEnglishNames) { text.text = Junidecode.unidecode(text.text); } // sest text size before finding intersection (it is used there) float textSize = rc.getDensityValue(text.textSize); paintText.setTextSize(textSize); paintText.setFakeBoldText(text.bold); paintText.setColor(text.textColor); // align center y text.centerY += (-paintText.ascent()); // calculate if there is intersection boolean intersects = findTextIntersection(cv, rc, nonIntersectedBounds, text); if (!intersects) { if (text.drawOnPath != null) { if (text.textShadow > 0) { paintText.setColor(Color.WHITE); paintText.setStyle(Style.STROKE); paintText.setStrokeWidth(2 + text.textShadow); cv.drawTextOnPath(text.text, text.drawOnPath, 0, text.vOffset, paintText); // reset paintText.setStyle(Style.FILL); paintText.setStrokeWidth(2); paintText.setColor(text.textColor); } cv.drawTextOnPath(text.text, text.drawOnPath, 0, text.vOffset, paintText); } else { if (text.shieldRes != null) { Bitmap ico = RenderingIcons.getIcon(context, text.shieldRes); if (ico != null) { if (rc.highResMode) { float left = text.centerX - rc.getDensityValue(ico.getWidth() / 2) - 0.5f; float top = text.centerY - rc.getDensityValue(ico.getHeight() / 2) - rc.getDensityValue(4.5f); Rect rec = new Rect(0, 0, ico.getWidth(), ico.getHeight()); cv.drawBitmap( ico, rec, new RectF( left, top, left + rc.getDensityValue(ico.getWidth()), top + rc.getDensityValue(ico.getHeight())), paintIcon); } else { float left = text.centerX - ico.getWidth() / 2 - 0.5f; float top = text.centerY - ico.getHeight() / 2 - rc.getDensityValue(4.5f); cv.drawBitmap(ico, left, top, paintIcon); } } } drawWrappedText(cv, text, textSize); } } } } }