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.kmagic.solitaire.DrawMaster.java

/**
 * Draw score/*w ww.j a va2 s  . c o  m*/
 * @param canvas canvas to draw on
 * @param score the score to draw
 */
public void drawScore(final Canvas canvas, final String score) {
    mTimePaint.setARGB(255, 20, 20, 20);
    final int textX = mScreenHeight - (mFontSize * 5) / 2;
    final int textY = mScreenWidth - mFontSize;
    canvas.drawText(score, textY, textX, mTimePaint);
    if (score.charAt(0) == '-') {
        mTimePaint.setARGB(255, 255, 0, 0);
    } else {
        mTimePaint.setARGB(255, 0, 0, 0);
    }
    canvas.drawText(score, textY - 1, textX - 1, mTimePaint);

}

From source file:Main.java

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

    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);
    //      ByteBuffer buf = ByteBuffer.allocate(b.getRowBytes());
    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()));

    //      byte bytes[] = buf.array();
    //      for (int i = 0; i < size * size * 2; i++)
    //         bytes[i] = (byte)(Math.random() * 255);
    return buf.array();
}

From source file:ch.carteggio.ui.ConversationIconLoader.java

/**
 * Calculates a bitmap with a color and a capital letter for contacts without picture.
 *//*  w w  w. ja  v  a 2s.c om*/
private Bitmap calculateFallbackBitmap(String emails[]) {
    Bitmap result = Bitmap.createBitmap(mPictureSizeInPx, mPictureSizeInPx, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(result);

    int rgb = CONTACT_DUMMY_COLORS_ARGB[0];

    if (emails.length > 0) {
        calcUnknownContactColor(emails[0]);
    }

    result.eraseColor(rgb);

    String letter = FALLBACK_CONTACT_LETTER;

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    paint.setARGB(255, 255, 255, 255);
    paint.setTextSize(mPictureSizeInPx * 3 / 4); // just scale this down a bit
    Rect rect = new Rect();
    paint.getTextBounds(letter, 0, 1, rect);
    float width = paint.measureText(letter);
    canvas.drawText(letter, (mPictureSizeInPx / 2f) - (width / 2f),
            (mPictureSizeInPx / 2f) + (rect.height() / 2f), paint);

    return result;
}

From source file:com.kmagic.solitaire.DrawMaster.java

/**
 * Draw card value and small suit below value
 * in both top left and bottom right corners
 * bottom right is reversed/upside down/*  w w w . j a  va 2 s.c om*/
 * @param paint paint styled for card value and small suit
 * @param canvas canvas to draw on
 * @param value card value
 * @param smallSuit small card suit
 * @param suitIdx suit index
 */
private void drawCardValue(final Paint paint, final Canvas canvas, final String value, final Bitmap smallSuit,
        final int suitIdx) {
    if ((suitIdx & 1) == 1) {
        paint.setARGB(255, 255, 0, 0);
    } else {
        paint.setARGB(255, 0, 0, 0);
    }
    canvas.drawText(value, mSuitsSize4th, mSuitsSize, paint);
    canvas.drawBitmap(smallSuit, mSuitsSize4th, mSuitsSize4th + mSuitsSize, mSuitPaint);
    canvas.save();
    canvas.rotate(180, Card.WIDTH / 2, Card.HEIGHT / 2);
    canvas.drawBitmap(smallSuit, mSuitsSize4th, mSuitsSize4th + mSuitsSize, mSuitPaint);
    canvas.drawText(value, mSuitsSize4th, mSuitsSize, paint);
    canvas.restore();
}

From source file:com.borax12.materialdaterangepicker.date.MonthView.java

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

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

private Bitmap createTagChip(Tag tag) {
    // Create the tag string (prepend/append spaces to better ux). Create a clickable
    // area for deleting the tag in non-readonly mode
    String tagText = String.format(" %s " + (mReadOnly || !isEnabled() ? "" : CHIP_REMOVE_TEXT), tag.mTag);

    // Create a new color for the tag if necessary
    if (tag.mColor == 0) {
        if (mChipBackgroundColor == 0) {
            tag.mColor = newRandomColor();
        } else {/*from   w  w  w.  jav  a  2s .  co  m*/
            tag.mColor = mChipBackgroundColor;
        }
    }
    mChipBgPaint.setColor((isEnabled()) ? tag.mColor : Color.LTGRAY);

    // Measure the chip rect
    Rect bounds = new Rect();
    mChipFgPaint.getTextBounds("|", 0, 1, bounds);
    int minHeight = bounds.height();
    mChipFgPaint.getTextBounds(tagText, 0, tagText.length(), bounds);
    int padding = (int) ONE_PIXEL * 2;
    int w = (int) (mChipFgPaint.measureText(tagText) + (padding * 2));
    int h = Math.max(bounds.height() + (padding * 4), minHeight + (padding * 4));
    float baseline = h / 2 + bounds.height() / 2;

    // Create the bitmap
    Bitmap bitmap = Bitmap.createBitmap(w + padding, h + padding, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    // Draw the bitmap
    canvas.drawRoundRect(new RectF(0, (padding / 2), w, h), 6, 6, mChipBgPaint);
    canvas.drawText(tagText, (padding / 2), baseline, mChipFgPaint);
    return bitmap;
}

From source file:com.grarak.kerneladiutor.elements.SplashView.java

private void draw(Canvas canvas, int x, int y, int radius) {
    if (radius > 0)
        canvas.drawCircle(x / 2, y / 2, radius, mPaintCircle);
    matrix.postRotate(rotate);/* w w  w . ja va 2 s.co  m*/
    Bitmap iconRotate = Bitmap.createBitmap(icon, 0, 0, icon.getWidth(), icon.getHeight(), matrix, false);
    canvas.drawBitmap(iconRotate, x / 2 - iconRotate.getWidth() / 2, y / 2 - iconRotate.getHeight() / 2,
            mPaintCircle);

    TextPaint textPaint = new TextPaint();
    textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    textPaint.setColor(textColor);
    textPaint.setAntiAlias(true);
    textPaint.setTextAlign(Paint.Align.CENTER);
    textPaint.setTextSize(textSize);
    float textHeight = textPaint.descent() - textPaint.ascent();
    float textOffset = (textHeight / 2) - textPaint.descent();

    canvas.drawText(getResources().getString(R.string.root_waiting), x / 2, y - textOffset - y / 4, textPaint);
}

From source file:com.andremion.counterfab.CounterFab.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mCount > 0 || isAnimating()) {
        if (getContentRect(mContentBounds)) {
            mCircleBounds.offsetTo(mContentBounds.left + mContentBounds.width() - mCircleBounds.width(),
                    mContentBounds.top);
        }//www  .  j a  v  a2  s .c o  m
        float cx = mCircleBounds.centerX();
        float cy = mCircleBounds.centerY();
        float radius = mCircleBounds.width() / 2f * mAnimationFactor;
        // Solid circle
        canvas.drawCircle(cx, cy, radius, mCirclePaint);
        // Mask circle
        canvas.drawCircle(cx, cy, radius, mMaskPaint);
        // Count text
        mTextPaint.setTextSize(mTextSize * mAnimationFactor);
        canvas.drawText(mText, cx, cy + mTextHeight / 2f, mTextPaint);
    }
}

From source file:org.holoeverywhere.widget.datetimepicker.date.SimpleMonthView.java

private void drawMonthDayLabels(Canvas canvas) {
    int y = MONTH_HEADER_SIZE - (MONTH_DAY_LABEL_TEXT_SIZE / 2);
    int dayWidthHalf = (mWidth - mPadding * 2) / (mNumDays * 2);

    for (int i = 0; i < mNumDays; i++) {
        int calendarDay = (i + mWeekStart) % mNumDays;
        int x = (2 * i + 1) * dayWidthHalf + mPadding;
        mDayLabelCalendar.set(Calendar.DAY_OF_WEEK, calendarDay);
        canvas.drawText(DatePickerDialog
                .getDisplayName(mDayLabelCalendar, Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault())
                .toUpperCase(Locale.getDefault()), x, y, mMonthDayLabelPaint);
    }/*  w ww.j av  a2s  . c om*/
}