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:github.daneren2005.dsub.util.ImageLoader.java

private Bitmap createUnknownImage(int size, int primaryColor, String topText, String bottomText) {
    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    Paint color = new Paint();
    color.setColor(primaryColor);// ww w .j a  va 2  s  . c  om
    canvas.drawRect(0, 0, size, size * 2.0f / 3.0f, color);

    color.setShader(new LinearGradient(0, 0, 0, size / 3.0f, Color.rgb(82, 82, 82), Color.BLACK,
            Shader.TileMode.MIRROR));
    canvas.drawRect(0, size * 2.0f / 3.0f, size, size, color);

    if (topText != null || bottomText != null) {
        Paint font = new Paint();
        font.setFlags(Paint.ANTI_ALIAS_FLAG);
        font.setColor(Color.WHITE);
        font.setTextSize(3.0f + size * 0.07f);

        if (topText != null) {
            canvas.drawText(topText, size * 0.05f, size * 0.6f, font);
        }

        if (bottomText != null) {
            canvas.drawText(bottomText, size * 0.05f, size * 0.8f, font);
        }
    }

    return bitmap;
}

From source file:com.raspi.chatapp.util.Notification.java

private Bitmap getLargeIcon(int bgColor, char letter, float width, boolean round) {
    Bitmap b = Bitmap.createBitmap((int) width, (int) width, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);

    RectF mInnerRectF = new RectF();
    mInnerRectF.set(0, 0, width, width);
    mInnerRectF.offset(0, 0);/*from   w  w w .  j  av  a 2s .  c om*/

    Paint mBgPaint = new Paint();
    mBgPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mBgPaint.setStyle(Paint.Style.FILL);
    mBgPaint.setColor(bgColor);

    TextPaint mTitleTextPaint = new TextPaint();
    mTitleTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mTitleTextPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    mTitleTextPaint.setTextAlign(Paint.Align.CENTER);
    mTitleTextPaint.setLinearText(true);
    mTitleTextPaint.setColor(Color.WHITE);
    mTitleTextPaint.setTextSize(width * 0.8f);

    float centerX = mInnerRectF.centerX();
    float centerY = mInnerRectF.centerY();

    int xPos = (int) centerX;
    int yPos = (int) (centerY - (mTitleTextPaint.descent() + mTitleTextPaint.ascent()) / 2);

    if (round)
        c.drawOval(mInnerRectF, mBgPaint);
    else
        c.drawRect(mInnerRectF, mBgPaint);
    c.drawText(String.valueOf(letter), xPos, yPos, mTitleTextPaint);

    return b;
}

From source file:com.appyvet.materialrangebar.PinView.java

@Override
public void draw(Canvas canvas) {
    //Draw the circle boundary only if mCircleBoundaryPaint was initialized
    if (mCircleBoundaryPaint != null)
        canvas.drawCircle(mX, mY, mCircleRadiusPx, mCircleBoundaryPaint);

    canvas.drawCircle(mX, mY, mCircleRadiusPx, mCirclePaint);
    //Draw pin if pressed
    if (mPinRadiusPx > 0 && (mHasBeenPressed || !mPinsAreTemporary)) {
        mBounds.set((int) mX - mPinRadiusPx, (int) mY - (mPinRadiusPx * 2) - (int) mPinPadding,
                (int) mX + mPinRadiusPx, (int) mY - (int) mPinPadding);
        mPin.setBounds(mBounds);/*  w  ww. j a  va2 s.c  om*/
        String text = mValue;

        if (this.formatter != null) {
            text = formatter.format(text);
        }

        calibrateTextSize(mTextPaint, text, mBounds.width());
        mTextPaint.getTextBounds(text, 0, text.length(), mBounds);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        DrawableCompat.setTint(mPin, pinColor);
        mPin.draw(canvas);
        canvas.drawText(text, mX, mY - mPinRadiusPx - mPinPadding + mTextYPadding, mTextPaint);
    }
    super.draw(canvas);
}

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

/**
 * Draw time, elapsed game time/*  w  w w .j a v  a2s . co m*/
 * @param canvas canvas to draw on
 * @param millis the time in millis to draw
 */
public void drawTime(final Canvas canvas, final int millis) {
    int seconds = (millis / 1000) % 60;
    int minutes = millis / 60000;
    if (seconds != mLastSeconds) {
        mLastSeconds = seconds;
        // String.format is insanely slow (~15ms)
        if (seconds < 10) {
            mTimeString = minutes + ":0" + seconds;
        } else {
            mTimeString = minutes + ":" + seconds;
        }
    }
    mTimePaint.setARGB(255, 20, 20, 20);
    final int textX = mScreenHeight - mFontSize;
    final int textY = mScreenWidth - mFontSize;
    canvas.drawText(mTimeString, textY, textX, mTimePaint);
    mTimePaint.setARGB(255, 0, 0, 0);
    canvas.drawText(mTimeString, textY - 1, textX - 1, mTimePaint);
}

From source file:com.appeaser.sublimepickerlibrary.datepicker.SimpleMonthView.java

private void drawMonth(Canvas canvas) {
    final float x = mPaddedWidth / 2f;

    // Vertically centered within the month header height.
    final float lineHeight = mMonthPaint.ascent() + mMonthPaint.descent();
    final float y = (mMonthHeight - lineHeight) / 2f;

    canvas.drawText(getTitle().toString(), x, y, mMonthPaint);
}

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

private void drawWeekDayLabels(Canvas canvas) {
    final int y = mMonthHeaderSize - (mMonthDayLabelTextSize / 2);
    final int dayWidthHalf = (mWidth - mPadding * 2) / (mNumDays * 2);

    for (int i = 0; i < mNumDays; i++) {
        final int calendarDay = (i + mWeekStart) % mNumDays;
        mDayLabelCalendar.set(Calendar.DAY_OF_WEEK, calendarDay);

        final String dayLabel;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            dayLabel = getDayLabel(mDayLabelCalendar);
        } else {/*w w  w  .  jav  a 2  s  .c  o  m*/
            dayLabel = getDayLabelCompat(mDayLabelCalendar);
        }
        final int x = (2 * i + 1) * dayWidthHalf + mPadding;
        canvas.drawText(dayLabel, x, y, mMonthDayLabelPaint);
    }
}

From source file:com.miuhouse.yourcompany.student.view.widget.date.datepicker.SimpleMonthView.java

private void drawMonth(Canvas canvas) {
    final float x = mPaddedWidth / 2f;

    // Vertically centered within the month header height.
    final float lineHeight = mMonthPaint.ascent() + mMonthPaint.descent();
    final float y = (mMonthHeight - lineHeight) / 2f;
    canvas.drawText(getTitle().toString(), x, y, mMonthPaint);
}

From source file:com.github.dfa.diaspora_android.ui.BadgeDrawable.java

@Override
public void draw(Canvas canvas) {
    if (!shouldDraw) {
        return;//w w w . j  a  v a  2  s .c o m
    }
    Rect bounds = getBounds();
    float width = bounds.right - bounds.left;
    float height = bounds.bottom - bounds.top;

    // Position the badge in the top-right quadrant of the icon.
    float radius = ((Math.max(width, height) / 2)) / 2;
    float centerX = (width - radius - 1) + 5;
    float centerY = radius - 5;
    if (badgeValue.length() <= 2) {
        // Draw badge circle.
        canvas.drawCircle(centerX, centerY, (int) (radius + 7.5), badgeStroke);
        canvas.drawCircle(centerX, centerY, (int) (radius + 5.5), badgeBackground);
    } else {
        canvas.drawCircle(centerX, centerY, (int) (radius + 8.5), badgeStroke);
        canvas.drawCircle(centerX, centerY, (int) (radius + 6.5), badgeBackground);
        //canvas.drawRoundRect(radius, radius, radius, radius, 10, 10, badgeBackground);
    }
    // Draw badge count message inside the circle.
    badgeText.getTextBounds(badgeValue, 0, badgeValue.length(), textRect);
    float textHeight = textRect.bottom - textRect.top;
    float textY = centerY + (textHeight / 2f);
    if (badgeValue.length() > 2)
        canvas.drawText(BADGE_VALUE_OVERFLOW, centerX, textY, badgeText);
    else
        canvas.drawText(badgeValue, centerX, textY, badgeText);
}

From source file:com.mohamadamin.persianmaterialdatetimepicker.date.MonthView.java

protected void drawMonthDayLabels(Canvas canvas) {
    int y = getMonthHeaderSize() - (MONTH_DAY_LABEL_TEXT_SIZE / 2);
    int dayWidthHalf = (mWidth - mEdgePadding * 2) / (mNumDays * 2);

    for (int i = 0; i < mNumDays; i++) {
        int calendarDay = (i + mWeekStart) % mNumDays;
        int x = (2 * i + 1) * dayWidthHalf + mEdgePadding;
        mDayLabelCalendar.set(Calendar.DAY_OF_WEEK, calendarDay);
        String localWeekDisplayName = mDayLabelCalendar.getPersianWeekDayName(); // TODO: RTLize
        String weekString = localWeekDisplayName.substring(0, 1);
        canvas.drawText(weekString, x, y, mMonthDayLabelPaint);
    }//  w w  w .  j ava2s  .c o  m
}

From source file:org.mdc.chess.ChessBoard.java

private void drawLabel(Canvas canvas, int xCrd, int yCrd, boolean right, boolean bottom, char c) {
    String s = Character.toString(c);
    if (labelBounds == null) {
        labelBounds = new Rect();
        labelPaint.getTextBounds("f", 0, 1, labelBounds);
    }/*from  w  w  w .  ja va  2s .  c o  m*/
    int margin = sqSize / 16;
    if (right) {
        xCrd += sqSize - labelBounds.right - margin;
    } else {
        xCrd += -labelBounds.left + margin;
    }
    if (bottom) {
        yCrd += sqSize - labelBounds.bottom - margin;
    } else {
        yCrd += -labelBounds.top + margin;
    }
    canvas.drawText(s, xCrd, yCrd, labelPaint);
}