Example usage for android.graphics Canvas drawText

List of usage examples for android.graphics Canvas drawText

Introduction

In this page you can find the example usage for android.graphics Canvas drawText.

Prototype

public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint) 

Source Link

Document

Draw the text, with origin at (x,y), using the specified paint.

Usage

From source file:com.android.yijiang.kzx.widget.betterpickers.calendardatepicker.SimpleMonthView.java

private void drawMonthTitle(Canvas canvas) {
    int x = (mWidth + 2 * mPadding) / 2;
    int y = (sMonthHeaderSize - sMonthDayLabelTextSize) / 2 + (sMonthLabelTextSize / 3);
    canvas.drawText(getMonthAndYearString(), x, y, mMonthTitlePaint);
}

From source file:hide.com.android.datetimepicker.date.SimpleMonthView.java

private void drawMonthTitle(Canvas canvas) {
    int x = (mWidth + 2 * mPadding) / 2;
    int y = (MONTH_HEADER_SIZE - MONTH_DAY_LABEL_TEXT_SIZE) / 2 + (MONTH_LABEL_TEXT_SIZE / 3);
    canvas.drawText(Utils.getMonthAndYearString(mCalendar), x, y, mMonthTitlePaint);
}

From source file:iSoron.HistoryChart.java

private void drawSquare(Canvas canvas, RectF location, GregorianCalendar date, int checkmarkOffset) {
    if (checkmarkOffset >= checkmarks.length)
        pSquareBg.setColor(colors[0]);/*from w  w w. ja  v  a2s  .com*/
    else
        pSquareBg.setColor(colors[checkmarks[checkmarkOffset]]);

    pSquareFg.setColor(reverseTextColor);
    canvas.drawRect(location, pSquareBg);
    String text = Integer.toString(date.get(Calendar.DAY_OF_MONTH));
    canvas.drawText(text, location.centerX(), location.centerY() + squareTextOffset, pSquareFg);
}

From source file:io.doist.datetimepicker.date.SimpleMonthView.java

private void drawMonthTitle(Canvas canvas) {
    final float x = (mWidth + 2 * mPadding) / 2f;
    final float y = (mMonthHeaderSize - mMonthDayLabelTextSize) / 2f;
    canvas.drawText(getMonthAndYearString(), x, y, mMonthTitlePaint);
}

From source file:org.smart.library.widget.SlideSwitch.java

private void drawText(Canvas canvas, int radius) {
    if (TextUtils.isEmpty(mOnText) || TextUtils.isEmpty(mOffText)) {
        // /*from  w ww . jav a 2  s  .  c o  m*/
        paint.setColor(isOpen ? mOnTextColor : mOffTextColor);
        paint.setTextAlign(Align.CENTER);
        paint.setTextSize(mTextSize);
        canvas.drawText(isOpen ? mOnText : mOffText, isOpen ? radius : backRect.width() * 3 / 4,
                backRect.height() / 2 + RIM_SIZE, paint);
    }
}

From source file:com.sebible.cordova.videosnapshot.VideoSnapshot.java

private void drawTimestamp(Bitmap bm, String prefix, long timeMs, int textSize) {
    float w = bm.getWidth(), h = bm.getHeight();
    float size = (float) (textSize * bm.getWidth()) / 1280;
    float margin = (float) (w < h ? w : h) * 0.05f;

    Canvas c = new Canvas(bm);
    Paint p = new Paint();
    p.setColor(Color.WHITE);/*www .  ja  v  a2 s. co m*/
    p.setStrokeWidth((int) (size / 10));
    p.setTextSize((int) size); // Text Size
    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern

    long second = (timeMs / 1000) % 60;
    long minute = (timeMs / (1000 * 60)) % 60;
    long hour = (timeMs / (1000 * 60 * 60)) % 24;

    String text = String.format("%s %02d:%02d:%02d", prefix, hour, minute, second);
    Rect r = new Rect();
    p.getTextBounds(text, 0, text.length(), r);
    //c.drawBitmap(originalBitmap, 0, 0, paint);
    c.drawText(text, bm.getWidth() - r.width() - margin, bm.getHeight() - r.height() - margin, p);
}

From source file:com.savvasdalkitsis.betwixt.demo.InterpolatorView.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.WHITE);
    int inset = (getHeight() - rectHeight()) / 2;
    canvas.drawRect(getPaddingLeft(), getPaddingTop() + inset, getWidth() - getPaddingRight(),
            getHeight() - inset - getPaddingBottom(), rectPaint);
    float x = 0;/*from   w ww .  j a v a 2 s. c om*/
    float y = 0;
    for (int i = 0; i < STEPS; i++) {
        float newX = i / (float) STEPS;
        float newY = interpolator.getInterpolation(newX);
        if (i == 0) {
            x = newX;
            y = newY;
        }
        canvas.drawLine(x(x), y(y), x(newX), y(newY), paint);
        x = newX;
        y = newY;
    }
    canvas.drawText(description, getPaddingLeft() + textPaddingLeft, getHeight() - textHeight, textPaint);
    if (animating) {
        canvas.drawRect(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(),
                getHeight() - getPaddingBottom(), dim);
        int areaWidth = getWidth() - getPaddingLeft() - getPaddingRight() - animationInset * 2;
        float circleX = getPaddingLeft() + animationInset + areaWidth * progress;
        linePath.reset();
        linePath.moveTo(getPaddingLeft() + animationInset, getPaddingTop());
        linePath.lineTo(getPaddingLeft() + animationInset, getHeight() - getPaddingBottom());
        linePath.moveTo(getWidth() - getPaddingRight() - animationInset, getPaddingTop());
        linePath.lineTo(getWidth() - getPaddingRight() - animationInset, getHeight() - getPaddingBottom());
        canvas.drawPath(linePath, linePaint);
        canvas.drawCircle(circleX, getHeight() / 2, radius, circlePaint);
    }
}

From source file:com.ruesga.rview.widget.MergedStatusChart.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // Bars//w w  w  .j  a  v  a 2s .c om
    canvas.drawRect(mOpenRect.left, mOpenRect.top,
            Math.min(mViewArea.width() * mAnimationDelta, mOpenRect.right), mOpenRect.bottom, mOpenPaint);
    canvas.drawRect(mMergedRect.left, mMergedRect.top,
            Math.min(mViewArea.width() * mAnimationDelta, mMergedRect.right), mMergedRect.bottom, mMergedPaint);
    canvas.drawRect(mAbandonedRect.left, mAbandonedRect.top,
            Math.min(mViewArea.width() * mAnimationDelta, mAbandonedRect.right), mAbandonedRect.bottom,
            mAbandonedPaint);

    // Number of items as text
    if (mAnimationDelta > .9f) {
        canvas.drawText(String.valueOf(mOpen), mOpenRect.left + mLabelPadding,
                mOpenRect.top + (mOpenRect.height() / 2) + (mLabelHeight / 2), mLabelPaint);
        canvas.drawText(String.valueOf(mMerged), mOpenRect.left + mLabelPadding,
                mMergedRect.top + (mMergedRect.height() / 2) + (mLabelHeight / 2), mLabelPaint);
        canvas.drawText(String.valueOf(mAbandoned), mOpenRect.left + mLabelPadding,
                mAbandonedRect.top + (mAbandonedRect.height() / 2) + (mLabelHeight / 2), mLabelPaint);
    }
}

From source file:Main.java

public static byte[] makeFontBitmap(String font, String code, int size, float[] arrayOfPos) {
    Canvas c = new Canvas();
    Paint p = new Paint();
    float density = context.getResources().getDisplayMetrics().density;
    //        Log.v(TAG, String.format("makeFontBitmap called(Java): font=%s code=%s density=%f", font, code, density));
    p.setTextSize((float) size * density);
    p.setAntiAlias(true);//w  ww  .j a  v a2 s .  c  o  m

    Rect textBounds = new Rect();
    p.getTextBounds(code, 0, code.length(), textBounds);
    //        Log.v(TAG, String.format("makeFontBitmap textBounds: %d,%d,%d,%d", textBounds.left, textBounds.top, textBounds.right, textBounds.bottom));

    Rect textBoundsAxA = new Rect();
    String axa = String.format("A%sA", code);
    p.getTextBounds(axa, 0, axa.length(), textBoundsAxA);
    Rect textBoundsAA = new Rect();
    String aa = "AA";
    p.getTextBounds(aa, 0, aa.length(), textBoundsAA);

    // cache.distDelta = Vec2(0, 0);
    arrayOfPos[0] = textBounds.left;
    arrayOfPos[1] = textBounds.top;

    // cache.srcWidth = Vec2(16, 16);
    arrayOfPos[2] = textBounds.width();
    arrayOfPos[3] = textBounds.height();

    // cache.step = 16;
    //      arrayOfPos[4] = textBounds.width() + 1;
    arrayOfPos[4] = textBoundsAxA.width() - textBoundsAA.width();

    if (textBounds.width() == 0 || textBounds.height() == 0) {
        Log.v(TAG, "makeFontBitmap: empty");
        return null;
    }

    Bitmap b = Bitmap.createBitmap(textBounds.width(), textBounds.height(), Bitmap.Config.ARGB_8888);
    c.setBitmap(b);

    Rect r = new Rect(0, 0, textBounds.width(), textBounds.height());
    //      p.setColor(Color.RED);
    p.setARGB(0, 0, 0, 0);
    c.drawRect(r, p);
    p.setARGB(255, 255, 255, 255);

    //        Log.v(TAG, "makeFontBitmap: drawText");
    c.drawText(code, -textBounds.left, -textBounds.top, p);
    //        Log.v(TAG, String.format("makeFontBitmap: w=%.2f h=%.2f", arrayOfPos[2], arrayOfPos[3]));

    ByteBuffer buf = ByteBuffer.allocate(textBounds.width() * textBounds.height() * 4);
    //        Log.v(TAG, String.format("makeFontBitmap: b.getRowBytes() %d", b.getRowBytes()));
    buf.position(0);
    b.copyPixelsToBuffer(buf);
    //        Log.v(TAG, String.format("makeFontBitmap results: capacity=%d", buf.capacity()));

    return buf.array();
}

From source file:com.android.datetimepicker.date.MonthView.java

protected void drawMonthTitle(Canvas canvas) {
    int x = (mWidth + 2 * mEdgePadding) / 2;
    int y = (getMonthHeaderSize() - MONTH_DAY_LABEL_TEXT_SIZE) / 2 + (MONTH_LABEL_TEXT_SIZE / 3);
    canvas.drawText(getMonthAndYearString(), x, y, mMonthTitlePaint);
}