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:app.android.datetimepicker.date.SimpleMonthView.java

/**
 * Draws the week and month day numbers for this week. Override this method
 * if you need different placement./*from w  w  w  .  j a va 2  s.  c  om*/
 *
 * @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);
        }

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

From source file:com.dirkgassen.wator.ui.view.RollingGraphView.java

/**
 * Paints the data points of one series vertically.
 *
 * @param c          canvas to paint to//from ww w  .  j a  va  2  s .  co  m
 * @param nameBounds bounds of the label
 * @param seriesNo   index of the series to paint
 * @param width      width of the view
 * @param height     height of the view
 * @param maxValue   calculated possible maximum data value
 */
private void paintSeriesVertical(Canvas c, Rect nameBounds, int seriesNo, int width,
        @SuppressWarnings("UnusedParameters") int height, float maxValue) {
    float paddingLeft = getPaddingLeft();
    float paddingTop = getPaddingTop();
    float paddingRight = getPaddingRight();
    float paddingBottom = getPaddingBottom();

    float y = nameBounds.height() + paddingTop;
    float x;
    if (currentValue == -1) {
        x = (width - paddingLeft - paddingRight) / (seriesNames.length + 1) * (seriesNo + 1) + paddingLeft;
    } else {
        if (dataValues == null) {
            Log.e("Wa-Tor", "NO DATA VALUES although currentValue is " + currentValue);
        } else if (dataValues[currentValue] == null) {
            Log.e("Wa-Tor", "NO SERIES DATA although currentValue is " + currentValue);
        }
        x = dataValues[currentValue][seriesNo] * (width - paddingLeft - paddingRight) / maxValue + paddingLeft;
    }
    c.drawText(seriesNames[seriesNo], x - nameBounds.width() / 2, y, seriesPaints[seriesNo]);

    float scale = seriesPaints[seriesNo].getStrokeWidth();
    y += 6f * scale;
    c.drawLine(x, y, x, y - 3f * scale, seriesPaints[seriesNo]);
    c.drawLine(x, y, x - 1.5f * scale, y - 1.5f * scale, seriesPaints[seriesNo]);
    c.drawLine(x, y, x + 1.5f * scale, y - 1.5f * scale, seriesPaints[seriesNo]);

    if (currentValue != -1) {
        int no = currentValue;
        do {
            if (dataValues[no] == null) {
                break;
            }
            float newX = dataValues[no][seriesNo] * (width - paddingLeft - paddingRight) / maxValue
                    + paddingLeft;
            float newY = y + 1;
            c.drawLine(x, y, newX, newY, seriesPaints[seriesNo]);
            x = newX;
            y = newY;
            no = no == 0 ? dataValues.length - 1 : no - 1;
        } while (no != oldestValue && y < height - paddingBottom);
    }
}

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

/**
 * Draws the week and month day numbers for this week. Override this method if you need different placement.
 *
 * @param canvas The canvas to draw on//ww  w.j a  va 2s  .  com
 */
protected void drawMonthNums(Canvas canvas) {
    int y = (((mRowHeight + sMiniDayNumberTextSize) / 2) - DAY_SEPARATOR_WIDTH) + sMonthHeaderSize;
    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 - (sMiniDayNumberTextSize / 3), sDaySelectedCircleSize,
                    mSelectedCirclePaint);
        }

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

From source file:com.mojtaba.materialdatetimepicker.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 localWeekDisplayName = mDayLabelCalendar.getPersianWeekDayName(); // TODO: RTLize
        String weekString = localWeekDisplayName.substring(0, 1);
        canvas.drawText(weekString, x, y, mMonthDayLabelPaint);
    }//  w  ww.  java2s.  c  om
}

From source file:com.dirkgassen.wator.ui.view.RollingGraphView.java

/**
 * Paints the data points of one series horizontally.
 *
 * @param c canvas to paint to//from w  w  w .j av  a2  s  . co m
 * @param nameBounds bounds of the label
 * @param seriesNo index of the series to paint
 * @param width width of the view
 * @param height height of the view
 * @param maxValue calculated possible maximum data value
 */
private void paintSeriesHorizontal(Canvas c, Rect nameBounds, int seriesNo, int width, int height,
        float maxValue) {
    float paddingLeft = getPaddingLeft();
    float paddingTop = getPaddingTop();
    float paddingRight = getPaddingRight();
    float paddingBottom = getPaddingBottom();

    float x = width - nameBounds.width() - paddingRight;
    float y;
    if (currentValue == -1) {
        y = height - (height - paddingBottom - paddingTop) / (seriesNames.length + 1) * (seriesNo + 1)
                + paddingTop;
    } else {
        if (dataValues == null) {
            Log.e("Wa-Tor", "NO DATA VALUES although currentValue is " + currentValue);
        } else if (dataValues[currentValue] == null) {
            Log.e("Wa-Tor", "NO SERIES DATA although currentValue is " + currentValue);
        }
        y = height - paddingBottom
                - dataValues[currentValue][seriesNo] * (height - paddingBottom - paddingTop) / maxValue;
    }
    c.drawText(seriesNames[seriesNo], x, y + nameBounds.height() / 2, seriesPaints[seriesNo]);

    float scale = seriesPaints[seriesNo].getStrokeWidth();
    x -= 6f * scale;
    c.drawLine(x, y, x + 3f * scale, y, seriesPaints[seriesNo]);
    c.drawLine(x, y, x + 1.5f * scale, y + 1.5f * scale, seriesPaints[seriesNo]);
    c.drawLine(x, y, x + 1.5f * scale, y - 1.5f * scale, seriesPaints[seriesNo]);
    if (currentValue != -1) {
        int no = currentValue;
        do {
            if (dataValues[no] == null) {
                break;
            }
            float newX = x - 1;
            float newY = height - paddingBottom
                    - dataValues[no][seriesNo] * (height - paddingBottom - paddingTop) / maxValue;
            c.drawLine(x, y, newX, newY, seriesPaints[seriesNo]);
            x = newX;
            y = newY;
            no = no == 0 ? dataValues.length - 1 : no - 1;
        } while (no != oldestValue && x > paddingLeft);
    }
}

From source file:com.andexert.calendarlistview.library.SimpleMonthView.java

/**
 * /*from w  w  w  .  ja  v a  2  s  . c  o  m*/
 * @param canvas
 */
private void drawMonthTitle(Canvas canvas) {
    StringBuilder stringBuilder = new StringBuilder(getMonthAndYearString().toLowerCase());
    stringBuilder.setCharAt(0, Character.toUpperCase(stringBuilder.charAt(0)));
    String text = stringBuilder.toString();
    Rect rect = new Rect();
    mMonthTitlePaint.getTextBounds(text, 0, text.length(), rect);
    //        int x = (mWidth + 2 * mPadding) / 2;
    int leftPadding = getResources().getDimensionPixelSize(R.dimen.month_day_left_padding);
    int x = rect.width() / 2 + leftPadding;
    //        int y = (MONTH_HEADER_SIZE - MONTH_DAY_LABEL_TEXT_SIZE) / 2 + (MONTH_LABEL_TEXT_SIZE / 3);
    int y = (MONTH_HEADER_SIZE - MONTH_DAY_LABEL_TEXT_SIZE) + (MONTH_LABEL_TEXT_SIZE / 4);
    if (mIsShowMonthDay) {
        y = MONTH_HEADER_SIZE - MONTH_DAY_LABEL_TEXT_SIZE - (MONTH_LABEL_TEXT_SIZE * 2);
    }
    canvas.drawText(text, x, y, mMonthTitlePaint);
    int linePadding = getResources().getDimensionPixelSize(R.dimen.month_day_line_padding);
    canvas.drawLine(leftPadding, y + linePadding, rect.width() + leftPadding, y + linePadding,
            mMonthNumLinePaint);
}

From source file:com.example.angel.parkpanda.MainActivity.java

public Bitmap drawTextToBitmap(int gResId, String gText) {
    Resources resources = getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);
    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();

    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }//from   w ww  .  j a v  a2 s . c  o m
    bitmap = bitmap.copy(bitmapConfig, true);
    Canvas canvas = new Canvas(bitmap);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.rgb(0, 0, 0));
    paint.setTextSize((int) (15 * scale));
    paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);

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

    return bitmap;
}

From source file:com.tealeaf.TeaLeaf.java

public Bitmap getBitmapFromView(EditText view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null) {
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);/*from  w  w w. jav  a 2s .com*/
    }
    // draw the view on the canvas
    view.draw(canvas);
    Paint p = new Paint();
    p.setColor(Color.BLACK);
    p.setTextSize(24);
    canvas.drawText("'ello mate", 0, 0, p);
    //return the bitmap
    return returnedBitmap;
}

From source file:com.cssweb.android.view.PriceMini.java

public void drawHKIndex(Canvas canvas) {
    Paint paint = this.mPaint;
    paint.setTypeface(Typeface.DEFAULT_BOLD);
    paint.setAntiAlias(true);//from w  w w . j a  v  a 2  s.  c  o m
    if (quoteData != null) {
        try {
            JSONArray jArr = quoteData.getJSONArray("data");
            JSONObject jo = jArr.getJSONObject(0);
            double zrsp = jo.getDouble("zrsp");

            double zjcj = jo.getDouble("zjcj");
            paint.setTextSize(mTextSize * 2);
            setColor(paint, zjcj, zrsp);
            paint.setTextAlign(Paint.Align.LEFT);
            canvas.translate(DX * 0.5f, DY * 2f);
            canvas.drawText(Utils.dataFormation(zjcj, stockdigit), x, y, paint);

            paint.setStyle(Paint.Style.STROKE);
            paint.setTextSize(mTextSize);

            double zhangd = jo.getDouble("zd");
            if (zhangd < 0) {
                paint.setColor(GlobalColor.colorPriceDown);
            } else if (zhangd > 0) {
                paint.setColor(GlobalColor.colorpriceUp);
            } else {
                paint.setColor(GlobalColor.colorPriceEqual);
            }
            canvas.translate(0, DY * 0.8f);
            String zhangdie = Utils.dataFormation(zhangd, stockdigit);
            if (zhangdie.equals("-"))
                canvas.drawText("", x, y, paint);
            else
                canvas.drawText(zhangdie, x, y, paint);

            double zhangf = jo.getDouble("zf");
            if (zhangf < 0) {
                paint.setColor(GlobalColor.colorPriceDown);
            } else if (zhangf > 0) {
                paint.setColor(GlobalColor.colorpriceUp);
            } else {
                paint.setColor(GlobalColor.colorPriceEqual);
            }
            canvas.translate(DX * 3.5f, 0);
            String zhangfu = Utils.dataFormation(zhangf * 100, stockdigit);
            if (zhangfu.equals("-"))
                canvas.drawText("", x, y, paint);
            else
                canvas.drawText(zhangfu + "%", x, y, paint);

            paint.setColor(GlobalColor.colorLabelName);

            canvas.translate(-DX * 4f, DY * 1.2f);
            canvas.drawText("", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("??", x, y, paint);

            canvas.translate(width / 2, -DY * 1);
            paint.setTextAlign(Paint.Align.RIGHT);
            paint.setColor(GlobalColor.colorLabelName);
            canvas.drawText(Utils.dataFormation(zrsp, stockdigit), x - tips, y, paint);

            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("cjje"), true), x - tips, y, paint);

            paint.setColor(GlobalColor.colorLabelName);
            paint.setTextAlign(Paint.Align.LEFT);
            canvas.translate(0, -DY * 4);
            canvas.drawText("", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x, y, paint);

            paint.setTextAlign(Paint.Align.RIGHT);
            canvas.translate(width / 2, -DY * 3);
            double jrkp = jo.getDouble("jrkp");
            setColor(paint, jrkp, zrsp);
            canvas.drawText(Utils.dataFormation(jrkp, stockdigit), x - tips, y, paint);

            canvas.translate(0, DY);
            double zg = jo.getDouble("zgcj");
            setColor(paint, zg, zrsp);
            canvas.drawText(Utils.dataFormation(zg, stockdigit), x - tips, y, paint);

            canvas.translate(0, DY);
            double zd = jo.getDouble("zdcj");
            setColor(paint, zd, zrsp);
            canvas.drawText(Utils.dataFormation(zd, stockdigit), x - tips, y, paint);

            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(0, DY);
            canvas.drawText(Utils.dataFormation(jo.getDouble("amp") * 100, 1) + "%", x - tips, y, paint);
        } catch (JSONException e) {
            Log.e(TAG, e.toString());
        }
    } else {//?????
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setColor(GlobalColor.colorLabelName);

        canvas.translate(DX, DY * 2f);
        canvas.drawText("-", x, y, paint);

        paint.setStyle(Paint.Style.STROKE);
        paint.setTextSize(mTextSize);

        canvas.translate(-DX / 2, DY * 0.8f);
        canvas.drawText("-", x, y, paint);

        canvas.translate(DX * 2.5f, 0);
        canvas.drawText("-", x, y, paint);
        canvas.translate(-DX * 3, DY * 1.2f);
        canvas.drawText("", x, y, paint);
        canvas.translate(0, DY);
        canvas.drawText("??", x, y, paint);

        paint.setTextAlign(Paint.Align.LEFT);
        canvas.translate(width / 2, -DY * 4);
        canvas.drawText("", x, y, paint);
        canvas.translate(0, DY);
        canvas.drawText("", x, y, paint);
        canvas.translate(0, DY);
        canvas.drawText("", x, y, paint);
        canvas.translate(0, DY);
        canvas.drawText("", x, y, paint);
    }
}

From source file:net.exclaimindustries.geohashdroid.wiki.WikiPictureEditor.java

private static void drawStrings(String[] strings, Canvas c, Paint textPaint, Paint backgroundPaint) {
    // FIXME: The math here is ugly and blunt and probably not too
    // efficient or flexible.  It might even fail.  This needs to be
    // fixed and made less-ugly later.

    // We need SOME strings.  If we've got nothing, bail out.
    if (strings.length < 1)
        return;/*w ww  .j  a  v a 2 s  .c  om*/

    // First, init our variables.  This is as good a place as any to do so.
    Rect textBounds = new Rect();
    int[] heights = new int[strings.length];
    int totalHeight = INFOBOX_MARGIN * 2;
    int longestWidth = 0;

    // Now, loop through the strings, adding to the height and keeping track
    // of the longest width.
    int i = 0;
    for (String s : strings) {
        textPaint.getTextBounds(s, 0, s.length(), textBounds);
        if (textBounds.width() > longestWidth)
            longestWidth = textBounds.width();
        totalHeight += textBounds.height();
        heights[i] = textBounds.height();
        i++;
    }

    // Now, we have us a rectangle.  Draw that.
    Rect drawBounds = new Rect(c.getWidth() - longestWidth - (INFOBOX_MARGIN * 2), 0, c.getWidth(),
            totalHeight);

    c.drawRect(drawBounds, backgroundPaint);

    // Now, place each of the strings.  We'll assume the topmost one is in
    // index 0.  They should all be left-justified, too.
    i = 0;
    int curHeight = 0;
    for (String s : strings) {
        Log.d(DEBUG_TAG, "Drawing " + s + " at " + (drawBounds.left + INFOBOX_MARGIN) + ","
                + (INFOBOX_MARGIN + (INFOBOX_PADDING * (i + 1)) + curHeight));
        c.drawText(s, drawBounds.left + INFOBOX_MARGIN,
                INFOBOX_MARGIN + (INFOBOX_PADDING * (i + 1)) + curHeight, textPaint);
        curHeight += heights[i];
        i++;
    }
}