Java Code Examples for android.graphics.Canvas.drawText()

The following are Jave code examples for showing how to use drawText() of the android.graphics.Canvas class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: blockvote   File: HistoryView.java   View Source Code Vote up 7 votes
@Override
protected void onDraw(Canvas canvas) {
	canvas.drawPath(marksPath, marksPaint);
	canvas.drawPath(axisPath, axisPaint);

	if (data != null) {
		canvas.drawPath(sw1dPath, sw1dPaint);
		canvas.drawPath(sw7dPath, sw7dPaint);
		canvas.drawPath(sw30dPath, sw30dPaint);
		canvas.drawPath(bu1dPath, bu1dPaint);
		canvas.drawPath(bu7dPath, bu7dPaint);
		canvas.drawPath(bu30dPath, bu30dPaint);
	}

	// y-labels
	for (int i = 4; i >= 0; i--) {
		canvas.drawText(String.format(Locale.getDefault(), "%d%%", i * 25), labelWidth, labelHeight + (1.5f * labelHeight + chartHeight) - (i + 1) * (1.5f * labelHeight + chartHeight) / 5, yLabelTextPaint);
	}
}
 
Example 2
Project: BooheeRuler   File: RightHeadRuler.java   View Source Code Vote up 6 votes
private void drawScale(Canvas canvas) {
    int width = canvas.getWidth();
    int height = canvas.getHeight();
    float start = (getScrollY() - mDrawOffset) / mParent.getInterval() + mParent.getMinScale();
    float end = (getScrollY() + height + mDrawOffset) / mParent.getInterval() + mParent.getMinScale();
    for (float i = start; i <= end; i++){
        float locationY = (i - mParent.getMinScale()) * mParent.getInterval();

        if (i >= mParent.getMinScale() && i <= mParent.getMaxScale()) {
            if (i % mCount == 0) {
                canvas.drawLine(width - mParent.getBigScaleLength(), locationY, width, locationY, mBigScalePaint);
                canvas.drawText(RulerStringUtil.resultValueOf(i,mParent.getFactor()),width - mParent.getTextMarginHead(), locationY + mParent.getTextSize() / 2, mTextPaint);
            } else {
                canvas.drawLine(width - mParent.getSmallScaleLength(), locationY, width, locationY, mSmallScalePaint);
            }
        }
    }
    //画轮廓线
    canvas.drawLine(canvas.getWidth(), getScrollY(), canvas.getWidth(), getScrollY() + height, mOutLinePaint);
}
 
Example 3
Project: Barcode-Reader   File: BarcodeGraphic.java   View Source Code Vote up 6 votes
/**
 * Draws the barcode annotations for position, size, and raw value on the supplied canvas.
 */
@Override
public void draw(Canvas canvas) {
    Barcode barcode = mBarcode;
    if (barcode == null) {
        return;
    }

    // Draws the bounding box around the barcode.
    RectF rect = new RectF(barcode.getBoundingBox());
    rect.left = translateX(rect.left);
    rect.top = translateY(rect.top);
    rect.right = translateX(rect.right);
    rect.bottom = translateY(rect.bottom);
    canvas.drawRect(rect, mRectPaint);

    // Draws a label at the bottom of the barcode indicate the barcode value that was detected.
    canvas.drawText(barcode.rawValue, rect.left, rect.bottom, mTextPaint);
}
 
Example 4
Project: BezierView   File: BezierQuadView.java   View Source Code Vote up 6 votes
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mPath.reset();
    //贝塞尔曲线
    mPath.moveTo(mStartXPoint, mStartYPoint);
    mPath.quadTo(mConXPoint, mConYPoint, mEndXPoint, mEndYPoint);
    canvas.drawPath(mPath, mPaint);

    //辅助线
    canvas.drawLine(mStartXPoint, mStartYPoint, mConXPoint, mConYPoint, mLinePaint);
    canvas.drawLine(mConXPoint, mConYPoint, mEndXPoint, mEndYPoint, mLinePaint);

    //文字
    canvas.drawPoint(mStartXPoint, mStartYPoint, mPaint);
    canvas.drawText("起始点", mStartXPoint, mStartYPoint + 30, mTextPaint);
    canvas.drawPoint(mEndXPoint, mEndYPoint, mPaint);
    canvas.drawText("结束点", mEndXPoint, mEndYPoint + 30, mTextPaint);
    canvas.drawPoint(mConXPoint, mConYPoint, mPaint);
    canvas.drawText("控制点", mConXPoint, mConYPoint - 30, mTextPaint);

}
 
Example 5
Project: android_ui   File: MonthView.java   View Source Code Vote up 6 votes
/**
 * Invoked to draw first letters of days in week from the specified <var>top</var> position.
 * <p>
 * Default implementation draws day letters in one row on the specified <var>canvas</var>.
 *
 * @param canvas The canvas on which to draw day letters.
 * @param top    Top position from which to start the drawing. This is the position returned from
 *               {@link #onDrawTitle(Canvas, float, Paint)} method {@code +} {@link #getDayLettersOffsetVertical()}.
 * @param paint  Paint for day letters ready to be used for drawing.
 * @return Updated top position from which following graphic components (day numbers) of this view
 * will be drawn.
 * @see #onDrawDayNumbers(Canvas, float, Paint)
 */
protected float onDrawWeekDayLetters(@NonNull Canvas canvas, float top, @NonNull Paint paint) {
	/**
	 * We will draw 7 columns for each day in week.
	 */
	int dayIndex = mFirstDayOfWeek - 1;
	paint.getTextBounds(Integer.toString(MAX_DAY_IN_MONTH), 0, 2, TEXT_BOUNDS);
	float left = getPaddingLeft() + TEXT_BOUNDS.width() / 2f;
	top += (int) paint.getTextSize();
	for (int i = 0; i < DAYS_IN_WEEK; i++) {
		final String dayLetter = DAY_LETTERS[dayIndex];
		canvas.drawText(dayLetter, 0, 1, left, top, paint);
		left += mSpacingHorizontal;
		if (++dayIndex >= DAYS_IN_WEEK) {
			dayIndex = 0;
		}
	}
	return top;
}
 
Example 6
Project: Widgets   File: PickerView.java   View Source Code Vote up 6 votes
private void drawData(Canvas canvas) {
    // 先绘制选中的text再往上往下绘制其余的text
    float scale = parabola(mViewHeight / 4.0f, mMoveLen);
    float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize;
    mPaint.setTextSize(size);
    mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha));
    // text居中绘制,注意baseline的计算才能达到居中,y值是text中心坐标
    float x = (float) (mViewWidth / 2.0);
    float y = (float) (mViewHeight / 2.0 + mMoveLen);
    FontMetricsInt fmi = mPaint.getFontMetricsInt();
    float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0));

    if (null != mItemProvider) {
        String itemData = mItemProvider.getItem(mDataList.get(mCurrentSelected), mCurrentSelected);
        canvas.drawText(itemData, x, baseline, mPaint);
    }
    // 绘制上方data
    for (int i = 1; (mCurrentSelected - i) >= 0; i++) {
        drawOtherText(canvas, i, -1);
    }
    // 绘制下方data
    for (int i = 1; (mCurrentSelected + i) < mDataList.size(); i++) {
        drawOtherText(canvas, i, 1);
    }
}
 
Example 7
Project: CalendarView_master   File: SimpleCalendarCardView.java   View Source Code Vote up 6 votes
@Override
protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
    float baselineY = mTextBaseLine + y;
    int cx = x + mItemWidth / 2;
    if (hasScheme) {
        canvas.drawText(String.valueOf(calendar.getDay()),
                cx,
                baselineY,
                calendar.isCurrentDay() ? mCurDayTextPaint :
                        calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);

    } else {
        canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY,
                calendar.isCurrentDay() ? mCurDayTextPaint :
                        calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
    }
}
 
Example 8
Project: buildAPKsApps   File: AbstractChart.java   View Source Code Vote up 5 votes
/**
 * Draw a multiple lines string.
 * 
 * @param canvas the canvas to paint to
 * @param text the text to be painted
 * @param x the x value of the area to draw to
 * @param y the y value of the area to draw to
 * @param paint the paint to be used for drawing
 */
protected void drawString(Canvas canvas, String text, float x, float y, Paint paint) {
  if (text != null) {
    String[] lines = text.split("\n");
    Rect rect = new Rect();
    int yOff = 0;
    for (int i = 0; i < lines.length; ++i) {
      canvas.drawText(lines[i], x, y + yOff, paint);
      paint.getTextBounds(lines[i], 0, lines[i].length(), rect);
      yOff = yOff + rect.height() + 5; // space between lines is 5
    }
  }
}
 
Example 9
Project: Weather-Android   File: AbstractWidgetProvider.java   View Source Code Vote up 5 votes
protected Bitmap getWeatherIcon(String text, Context context) {
    Bitmap myBitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_4444);
    Canvas myCanvas = new Canvas(myBitmap);
    Paint paint = new Paint();
    Typeface clock = Typeface.createFromAsset(context.getAssets(), "fonts/weather.ttf");
    paint.setAntiAlias(true);
    paint.setSubpixelText(true);
    paint.setTypeface(clock);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTextSize(150);
    paint.setTextAlign(Paint.Align.CENTER);
    myCanvas.drawText(text, 128, 180, paint);
    return myBitmap;
}
 
Example 10
Project: redpacketui-open   File: RPSideBar.java   View Source Code Vote up 5 votes
/**
 * 重写这个方法
 */
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // 获取焦点改变背景颜色.
    int height = getHeight();// 获取对应高度
    int width = getWidth(); // 获取对应宽度
    int singleHeight = height / b.length;// 获取每一个字母的高度

    for (int i = 0; i < b.length; i++) {
        paint.setColor(getResources().getColor(R.color.rp_text_grey));
        paint.setColor(Color.BLACK);
        paint.setTypeface(Typeface.DEFAULT);
        paint.setAntiAlias(true);
        paint.setTextSize(dip2px(mContext, 12));
        // 选中的状态
        if (i == choose) {
            paint.setColor(getResources().getColor(R.color.rp_primary_dark));
            paint.setFakeBoldText(true);
        }
        // x坐标等于中间-字符串宽度的一半.
        float xPos = width / 2 - paint.measureText(b[i]) / 2;
        float yPos = singleHeight * i + singleHeight;
        canvas.drawText(b[i], xPos, yPos, paint);
        paint.reset();// 重置画笔
    }

}
 
Example 11
Project: timelineview   File: TimeLineView.java   View Source Code Vote up 5 votes
private void drawCircle(Canvas canvas, int drawStep, String text, int dx) {
    this.mPaint.setColor(this.mStep == drawStep ? mUnderwayCircleColor : this.mStep > drawStep ? mStartedCircleColor : mPreCircleColor);
    this.mPaint.setStyle(Paint.Style.STROKE);
    this.mPaint.setStrokeWidth(2.0F);
    canvas.drawCircle(dx, getHeight() - this.mRadius - 1, this.mRadius, this.mPaint);
    this.mPaint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(dx, getHeight() - this.mRadius - 1, this.mRadius - 5, this.mPaint);
    this.mPaint.setColor(this.mStep == drawStep ? mUnderwayStringColor : this.mStep > drawStep ? mStartedStringColor : mPreStringColor);
    this.mPaint.setTextSize(this.mTextSize);
    canvas.drawText(text, dx - getWordCount(text) * this.mTextSize, getHeight() - this.mRadius * 2 - 15, this.mPaint);
}
 
Example 12
Project: GitHub   File: WaterBottleLoadingRenderer.java   View Source Code Vote up 5 votes
@Override
protected void draw(Canvas canvas, Rect bounds) {
    int saveCount = canvas.save();

    RectF arcBounds = mCurrentBounds;
    arcBounds.set(bounds);
    //draw bottle
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setColor(mBottleColor);
    canvas.drawPath(createBottlePath(mBottleBounds), mPaint);

    //draw water
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaint.setColor(mWaterColor);
    canvas.drawPath(createWaterPath(mWaterBounds, mProgress), mPaint);

    //draw water drop
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(mWaterColor);
    for (WaterDropHolder waterDropHolder : mWaterDropHolders) {
        if (waterDropHolder.mNeedDraw) {
            canvas.drawCircle(waterDropHolder.mInitX, waterDropHolder.mCurrentY, waterDropHolder.mRadius, mPaint);
        }
    }

    //draw loading text
    mPaint.setColor(mBottleColor);
    canvas.drawText(LOADING_TEXT, mBottleBounds.centerX() - mLoadingBounds.width() / 2.0f,
            mBottleBounds.bottom + mBottleBounds.height() * 0.2f, mPaint);
    canvas.restoreToCount(saveCount);
}
 
Example 13
Project: ScrollableChart   File: DemoDrawing.java   View Source Code Vote up 5 votes
protected void drawIndex(Canvas canvas, int height, float x, int index) {
  String text = index + "";
  int textHeight = getTextHeight(text, mTextPaint);
  float textWidth = getTextWidth(text, mTextPaint);
  x -= textWidth / 2.0f;
  float y = height - textHeight;
  canvas.drawText(text, x, y, mTextPaint);
}
 
Example 14
Project: GitHub   File: SimpleMonthView.java   View Source Code Vote up 5 votes
/**
 * 绘制头部(年份月份,星期几)
 *
 * @param canvas
 */
private void drawMonthTitle(Canvas canvas) {
    int x = (mWidth + 2 * mPadding) / 2;
    int y = (MONTH_HEADER_SIZE - WEEK_TEXT_SIZE) / 2 + (YEAR_MONTH_TEXT_SIZE / 3);
    StringBuilder stringBuilder = new StringBuilder(getMonthAndYearString().toLowerCase());
    stringBuilder.setCharAt(0, Character.toUpperCase(stringBuilder.charAt(0)));
    canvas.drawText(stringBuilder.toString(), x, y, mYearMonthPaint);
}
 
Example 15
Project: RLibrary   File: ImageUtils.java   View Source Code Vote up 5 votes
/**
 * 添加文字水印
 *
 * @param src      源图片
 * @param content  水印文本
 * @param textSize 水印字体大小
 * @param color    水印字体颜色
 * @param x        起始坐标x
 * @param y        起始坐标y
 * @param recycle  是否回收
 * @return 带有文字水印的图片
 */
public static Bitmap addTextWatermark(Bitmap src, String content, float textSize, int color, float x,
                                      float y, boolean recycle) {
    if (isEmptyBitmap(src) || content == null) return null;
    Bitmap ret = src.copy(src.getConfig(), true);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Canvas canvas = new Canvas(ret);
    paint.setColor(color);
    paint.setTextSize(textSize);
    Rect bounds = new Rect();
    paint.getTextBounds(content, 0, content.length(), bounds);
    canvas.drawText(content, x, y + textSize, paint);
    if (recycle && !src.isRecycled()) src.recycle();
    return ret;
}
 
Example 16
Project: GitHub   File: AxesRenderer.java   View Source Code Vote up 4 votes
private void drawAxisLabelsAndName(Canvas canvas, Axis axis, int position) {
    float labelX, labelY;
    labelX = labelY = 0;
    boolean isAxisVertical = isAxisVertical(position);
    if (LEFT == position || RIGHT == position) {
        labelX = labelBaselineTab[position];
    } else if (TOP == position || BOTTOM == position) {
        labelY = labelBaselineTab[position];
    }

    for (int valueToDrawIndex = 0; valueToDrawIndex < valuesToDrawNumTab[position]; ++valueToDrawIndex) {
        int charsNumber = 0;
        if (axis.isAutoGenerated()) {
            final float value = autoValuesToDrawTab[position][valueToDrawIndex];
            charsNumber = axis.getFormatter().formatValueForAutoGeneratedAxis(labelBuffer, value,
                    autoValuesBufferTab[position].decimals);
        } else {
            AxisValue axisValue = valuesToDrawTab[position][valueToDrawIndex];
            charsNumber = axis.getFormatter().formatValueForManualAxis(labelBuffer, axisValue);
        }

        if (isAxisVertical) {
            labelY = rawValuesTab[position][valueToDrawIndex];
        } else {
            labelX = rawValuesTab[position][valueToDrawIndex];
        }

        if (axis.hasTiltedLabels()) {
            canvas.save();
            canvas.translate(tiltedLabelXTranslation[position], tiltedLabelYTranslation[position]);
            canvas.rotate(-45, labelX, labelY);
            canvas.drawText(labelBuffer, labelBuffer.length - charsNumber, charsNumber, labelX, labelY,
                    labelPaintTab[position]);
            canvas.restore();
        } else {
            canvas.drawText(labelBuffer, labelBuffer.length - charsNumber, charsNumber, labelX, labelY,
                    labelPaintTab[position]);
        }
    }

    // Drawing axis name
    final Rect contentRectMargins = computator.getContentRectMinusAxesMargins();
    if (!TextUtils.isEmpty(axis.getName())) {
        if (isAxisVertical) {
            canvas.save();
            canvas.rotate(-90, contentRectMargins.centerY(), contentRectMargins.centerY());
            canvas.drawText(axis.getName(), contentRectMargins.centerY(), nameBaselineTab[position],
                    namePaintTab[position]);
            canvas.restore();
        } else {
            canvas.drawText(axis.getName(), contentRectMargins.centerX(), nameBaselineTab[position],
                    namePaintTab[position]);
        }
    }
}
 
Example 17
Project: GravityBox   File: LockscreenAppBar.java   View Source Code Vote up 4 votes
private Drawable createBadgeDrawable(Drawable d, int count) {
    if (d == null) return null;

    NumberFormat f = NumberFormat.getIntegerInstance();
    String countStr = count > 99 ? "99+" : f.format(count);

    Bitmap b = Utils.drawableToBitmap(d);
    b = b.copy(Bitmap.Config.ARGB_8888, true);
    Canvas c = new Canvas(b);

    Paint p = new Paint();
    p.setTextAlign(Paint.Align.CENTER);
    p.setColor(Color.WHITE);
    p.setAntiAlias(true);
    p.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
            mResources.getDisplayMetrics()));

    Drawable bg = mGbResources.getDrawable(R.drawable.ic_notification_overlay, null);

    final int w = b.getWidth();
    final int h = b.getHeight();
    final Rect r = new Rect();
    p.getTextBounds(countStr, 0, countStr.length(), r);
    final int tw = r.right - r.left;
    final int th = r.bottom - r.top;
    bg.getPadding(r);
    int dw = r.left + tw + r.right;
    if (dw < bg.getMinimumWidth()) {
        dw = bg.getMinimumWidth();
    }
    int x = w-r.right-((dw-r.right-r.left)/2);
    int dh = r.top + th + r.bottom;
    if (dh < bg.getMinimumHeight()) {
        dh = bg.getMinimumHeight();
    }
    if (dw < dh) dw = dh;
    int y = h-r.bottom-((dh-r.top-th-r.bottom)/2);
    bg.setBounds(w-dw, h-dh, w, h);

    bg.draw(c);
    c.drawText(countStr, x, y, p);

    return new BitmapDrawable(mResources, b);
}
 
Example 18
Project: buildAPKsApps   File: AbstractChart.java   View Source Code Vote up 4 votes
/**
 * Draws a text label.
 * 
 * @param canvas the canvas
 * @param labelText the label text
 * @param renderer the renderer
 * @param prevLabelsBounds the previous rendered label bounds
 * @param centerX the round chart center on X axis
 * @param centerY the round chart center on Y axis
 * @param shortRadius the short radius for the round chart
 * @param longRadius the long radius for the round chart
 * @param currentAngle the current angle
 * @param angle the label extra angle
 * @param left the left side
 * @param right the right side
 * @param color the label color
 * @param paint the paint
 * @param line if a line to the label should be drawn
 * @param display display the label anyway
 */
protected void drawLabel(Canvas canvas, String labelText, DefaultRenderer renderer,
    List<RectF> prevLabelsBounds, int centerX, int centerY, float shortRadius, float longRadius,
    float currentAngle, float angle, int left, int right, int color, Paint paint, boolean line,
    boolean display) {
  if (renderer.isShowLabels() || display) {
    paint.setColor(color);
    double rAngle = Math.toRadians(90 - (currentAngle + angle / 2));
    double sinValue = Math.sin(rAngle);
    double cosValue = Math.cos(rAngle);
    int x1 = Math.round(centerX + (float) (shortRadius * sinValue));
    int y1 = Math.round(centerY + (float) (shortRadius * cosValue));
    int x2 = Math.round(centerX + (float) (longRadius * sinValue));
    int y2 = Math.round(centerY + (float) (longRadius * cosValue));

    float size = renderer.getLabelsTextSize();
    float extra = Math.max(size / 2, 10);
    paint.setTextAlign(Align.LEFT);
    if (x1 > x2) {
      extra = -extra;
      paint.setTextAlign(Align.RIGHT);
    }
    float xLabel = x2 + extra;
    float yLabel = y2;
    float width = right - xLabel;
    if (x1 > x2) {
      width = xLabel - left;
    }
    labelText = getFitText(labelText, width, paint);
    float widthLabel = paint.measureText(labelText);
    boolean okBounds = false;
    while (!okBounds && line) {
      boolean intersects = false;
      int length = prevLabelsBounds.size();
      for (int j = 0; j < length && !intersects; j++) {
        RectF prevLabelBounds = prevLabelsBounds.get(j);
        if (prevLabelBounds.intersects(xLabel, yLabel, xLabel + widthLabel, yLabel + size)) {
          intersects = true;
          yLabel = Math.max(yLabel, prevLabelBounds.bottom);
        }
      }
      okBounds = !intersects;
    }

    if (line) {
      y2 = (int) (yLabel - size / 2);
      canvas.drawLine(x1, y1, x2, y2, paint);
      canvas.drawLine(x2, y2, x2 + extra, y2, paint);
    } else {
      paint.setTextAlign(Align.CENTER);
    }
    canvas.drawText(labelText, xLabel, yLabel, paint);
    if (line) {
      prevLabelsBounds.add(new RectF(xLabel, yLabel, xLabel + widthLabel, yLabel + size));
    }
  }
}
 
Example 19
Project: SmartChart   File: NumberPickerView.java   View Source Code Vote up 4 votes
private void drawHint(Canvas canvas){
    if(TextUtils.isEmpty(mHintText)) return;
    canvas.drawText(mHintText,
            mViewCenterX + (mMaxWidthOfDisplayedValues + mWidthOfHintText)/2 + mMarginStartOfHint,
            (dividerY0 + dividerY1) / 2 + mTextSizeHintCenterYOffset, mPaintHint);
}
 
Example 20
Project: Tensorflow_Andriod_With_Audio_Output   File: BorderedText.java   View Source Code Vote up 4 votes
public void drawText(final Canvas canvas, final float posX, final float posY, final String text) {
  canvas.drawText(text, posX, posY, exteriorPaint);
  canvas.drawText(text, posX, posY, interiorPaint);
}