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:jp.seesaa.android.datetimepicker.date.MonthView.java

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

    for (int i = 0; i < mNumDays; i++) {
        int calendarDay = (i + mWeekStart) % mNumDays;
        final int x = (int) ((2 * i + 1) * dayWidthHalf + mEdgePadding);

        mDayLabelCalendar.set(Calendar.DAY_OF_WEEK, calendarDay);
        canvas.drawText(
                mDayLabelCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault())
                        .toUpperCase(Locale.getDefault()),
                x, y, mMonthDayLabelPaint);
    }//w  ww .  j ava  2s . c  o  m
}

From source file:com.example.nitish.welcomapp.widgetpt.PeriodicTableView.java

/**
 * Draw the headers and placeholders on the supplied Canvas.
 *
 * @param canvas The Canvas// w  ww . j a v a2s.c o m
 */
private void writeHeaders(@NonNull Canvas canvas) {
    mHeaderPaint.setTextSize(mBlockSize / 4);

    for (int i = 1; i <= mNumCols; i++) {
        canvas.drawText(String.valueOf(i), mBlockSize * i + mContentRect.left + mContentOffset.x,
                mPadding / 2 + mContentRect.top + mContentOffset.y, mHeaderPaint);
    }
    for (int i = 1; i <= mNumRows - 2; i++) {
        canvas.drawText(String.valueOf(i), mPadding / 2 + mContentRect.left + mContentOffset.x,
                mBlockSize * i + mContentRect.top + mContentOffset.y, mHeaderPaint);
    }

    canvas.drawText("57-71", mBlockSize * 3 + mContentRect.left + mContentOffset.x,
            mBlockSize * 6 + mContentRect.top + mContentOffset.y + mHeaderPaint.getTextSize() / 2,
            mHeaderPaint);

    canvas.drawText("89-103", mBlockSize * 3 + mContentRect.left + mContentOffset.x,
            mBlockSize * 7 + mContentRect.top + mContentOffset.y + mHeaderPaint.getTextSize() / 2,
            mHeaderPaint);
}

From source file:org.ciasaboark.tacere.manager.NotificationManagerWrapper.java

private Bitmap createMarkerIcon(Drawable backgroundImage, String text, int width, int height) {

    Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    // Create a canvas, that will draw on to canvasBitmap.
    Canvas imageCanvas = new Canvas(canvasBitmap);

    // Draw the image to our canvas
    backgroundImage.draw(imageCanvas);//  w  w w  .java  2 s.  c om

    // Set up the paint for use with our Canvas
    TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | TextPaint.LINEAR_TEXT_FLAG);
    textPaint.setTextAlign(TextPaint.Align.CENTER);
    textPaint.setTypeface(Typeface.DEFAULT);
    textPaint.setTextSize(100f);
    textPaint.setColor(context.getResources().getColor(android.R.color.white));

    int xPos = (imageCanvas.getWidth() / 2);
    int yPos = (int) ((imageCanvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2));
    Rect r = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), r);
    //        yPos += (Math.abs(r.height()))/2;

    // Draw the text on top of our image
    imageCanvas.drawText(text, xPos, yPos, textPaint);

    // Combine background and text to a LayerDrawable
    LayerDrawable layerDrawable = new LayerDrawable(
            new Drawable[] { backgroundImage, new BitmapDrawable(canvasBitmap) });
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    layerDrawable.setBounds(0, 0, width, height);
    layerDrawable.draw(new Canvas(newBitmap));
    return newBitmap;
}

From source file:com.ickphum.android.isoview.IsoCanvas.java

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

    canvas.translate(-mOriginX, -mOriginY);

    for (int x = 0; x < 100; x++) {
        for (int y = 0; y < 100; y++) {
            canvas.save();//from   ww  w  . j  ava  2s .  co  m
            canvas.translate(-x * 7, -y * 7);
            canvas.drawPath(mPaths[0], mPaints[0]);
            //canvas.drawRect(0.0f, 0.0f, 5.0f, 5.0f, mPaints[0]);
            canvas.restore();
        }
    }

    canvas.drawLine(mContentRect.left, mContentRect.top, mContentRect.right, mContentRect.bottom,
            mBackgroundLinePaint);
    canvas.drawLine(mContentRect.left, mContentRect.bottom, mContentRect.right, mContentRect.top,
            mBackgroundLinePaint);

    for (int x = -1000; x < 1000; x += 200) {
        for (int y = -1000; y < 1000; y += 200) {
            canvas.drawText(x + "," + y, x, y, mBackgroundLinePaint);
        }
    }
}

From source file:com.appsimobile.appsii.module.appsiagenda.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;
        String displayCalendarDay = getWeekDayName(calendarDay);

        canvas.drawText(displayCalendarDay, x, y, mMonthDayLabelPaint);
    }//from   www.  ja va2s  .c  o  m
}

From source file:com.borax12.materialdaterangepicker.single.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 x = (2 * i + 1) * dayWidthHalf + mEdgePadding;

        int calendarDay = (i + mWeekStart) % mNumDays;
        mDayLabelCalendar.set(Calendar.DAY_OF_WEEK, calendarDay);
        String weekString = getWeekDayLabel(mDayLabelCalendar);
        canvas.drawText(weekString, x, y, mMonthDayLabelPaint);
    }/*  w  w  w .j av a2  s. c o m*/
}

From source file:com.xbm.android.matisse.internal.ui.widget.CheckView.java

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

    // draw outer and inner shadow
    initShadowPaint();/*from  ww w  .ja  va2s.  c  o m*/
    canvas.drawCircle((float) SIZE * mDensity / 2, (float) SIZE * mDensity / 2,
            (STROKE_RADIUS + STROKE_WIDTH / 2 + SHADOW_WIDTH) * mDensity, mShadowPaint);

    // draw white stroke
    canvas.drawCircle((float) SIZE * mDensity / 2, (float) SIZE * mDensity / 2, STROKE_RADIUS * mDensity,
            mStrokePaint);

    // draw content
    if (mCountable) {
        if (mCheckedNum != UNCHECKED) {
            initBackgroundPaint();
            canvas.drawCircle((float) SIZE * mDensity / 2, (float) SIZE * mDensity / 2, BG_RADIUS * mDensity,
                    mBackgroundPaint);
            initTextPaint();
            String text = String.valueOf(mCheckedNum);
            int baseX = (int) (canvas.getWidth() - mTextPaint.measureText(text)) / 2;
            int baseY = (int) (canvas.getHeight() - mTextPaint.descent() - mTextPaint.ascent()) / 2;
            canvas.drawText(text, baseX, baseY, mTextPaint);
        }
    } else {
        if (mChecked) {
            initBackgroundPaint();
            canvas.drawCircle((float) SIZE * mDensity / 2, (float) SIZE * mDensity / 2, BG_RADIUS * mDensity,
                    mBackgroundPaint);

            mCheckDrawable.setBounds(getCheckRect());
            mCheckDrawable.draw(canvas);
        }
    }

    // enable hint
    setAlpha(mEnabled ? 1.0f : 0.5f);
}

From source file:br.com.mauker.materialseekbar.PinView.java

@Override
public void draw(Canvas canvas) {

    //Draw pin if pressed
    if (mPinRadiusPx > 0 && (mHasBeenPressed || !mIsTemporary)) {
        //            mBounds.set(
        //                    (int) mX - mPinRadiusPx,
        //                    (int) mY - (mPinRadiusPx * 2) - (int) mPinPadding,
        //                    (int) mX + mPinRadiusPx,
        //                    (int) mY - (int) mPinPadding
        //            );

        mBounds.set((int) mX - mPinRadiusPx, (int) mY - (mPinRadiusPx * 2) - (int) mPinPadding,
                (int) mX + mPinRadiusPx, (int) mY - (int) mPinPadding);

        mPin.setBounds(mBounds);//w ww  .java2  s  . c  om
        String text = mValue;

        calibrateTextSize(mTextPaint, text, mBounds.width());
        mTextPaint.getTextBounds(text, 0, text.length(), mBounds);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mPin.setColorFilter(mPinFilter);
        mPin.draw(canvas);
        canvas.drawText(text, mX, mY - mPinRadiusPx - mPinPadding + mTextYPadding, mTextPaint);
    } else {
        canvas.drawCircle(mX, mY, mCircleRadiusPx, mCirclePaint);
    }
    super.draw(canvas);
}

From source file:net.gsantner.opoc.util.ContextUtils.java

/**
 * Draw text in the center of the given {@link DrawableRes}
 * This may be useful for e.g. badge counts
 *//*  ww w. j av a2 s.  c  o  m*/
public Bitmap drawTextOnDrawable(@DrawableRes int drawableRes, String text, int textSize) {
    Resources resources = _context.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = drawableToBitmap(drawableRes);

    bitmap = bitmap.copy(bitmap.getConfig(), true);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.rgb(61, 61, 61));
    paint.setTextSize((int) (textSize * scale));
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 2;
    int y = (bitmap.getHeight() + bounds.height()) / 2;
    canvas.drawText(text, x, y, paint);

    return bitmap;
}

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

/**
 * Draws the week and month day numbers for this week. Override this method
 * if you need different placement./* w  w  w .  j a  v  a2 s. c  o m*/
 *
 * @param canvas The canvas to draw on
 */
protected void drawMonthNums(Canvas canvas) {
    int y = (((mRowHeight + MINI_DAY_NUMBER_TEXT_SIZE) / 2) - DAY_SEPARATOR_WIDTH) + MONTH_HEADER_SIZE;
    int dayWidthHalf = (mWidth - mPadding * 2) / (mNumDays * 2);
    int j = findDayOffset();
    for (int dayNumber = 1; dayNumber <= mNumCells; dayNumber++) {
        int x = (2 * j + 1) * dayWidthHalf + mPadding;
        if (mSelectedDay == dayNumber) {
            canvas.drawCircle(x, y - (MINI_DAY_NUMBER_TEXT_SIZE / 3), DAY_SELECTED_CIRCLE_SIZE,
                    mSelectedCirclePaint);
        }

        mMonthNumPaint.setColor(mHasToday && mToday == dayNumber ? mTodayNumberColor : mDayTextColor);
        canvas.drawText(String.format("%d", dayNumber), x, y, mMonthNumPaint);
        j++;
        if (j == mNumDays) {
            j = 0;
            y += mRowHeight;
        }
    }
}