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.ruesga.rview.widget.ActivityStatsChart.java

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

    // Draw the grid lines
    float gap = mViewArea.height() / (MAX_Y_TICKS_LABELS * 1f);
    float y = mViewArea.top + gap;
    for (int i = 0; i < MAX_Y_TICKS_LABELS; i++) {
        canvas.drawLine(mViewArea.left, y, mViewArea.right, y, mGridLinesPaint);
        y += gap;/*from  w ww  .  ja  va  2 s . c o  m*/
    }

    // Draw the points data
    canvas.drawPath(mAreaPath, mAreaPaint);
    canvas.drawPath(mLinePath, mLinePaint);

    if (mDataAvailable) {
        // Draw ticks labels
        float margin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2f,
                getResources().getDisplayMetrics());
        y = mViewArea.top + gap;
        for (int i = 0; i < MAX_Y_TICKS_LABELS; i++) {
            canvas.drawText(mYTicksLabels[i], mViewArea.right - margin, y - margin, mTicksPaint);
            y += gap;
        }
    }
}

From source file:com.owen.view.views.PieChart.java

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

    // Draw the shadow
    canvas.drawOval(mShadowBounds, mShadowPaint);

    // Draw the label text
    if (getShowText()) {
        canvas.drawText(mData.get(mCurrentItem).mLabel, mTextX, mTextY, mTextPaint);
    }// ww  w  . j  av a  2s . co  m

    // If the API level is less than 11, we can't rely on the view animation system to
    // do the scrolling animation. Need to tick it here and call postInvalidate() until the scrolling is done.
    if (Build.VERSION.SDK_INT < 11) {
        tickScrollAnimation();
        if (!mScroller.isFinished()) {
            postInvalidate();
        }
    }
}

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

public void drawPrice(Canvas canvas) {
    //canvas.restore();
    Paint paint = this.mPaint;
    paint.setTypeface(Typeface.DEFAULT_BOLD);
    paint.setAntiAlias(true);/*from  w  w  w  .j av a2  s. c om*/
    if (quoteData != null) {
        try {
            JSONArray jArr = quoteData.getJSONArray("data");
            JSONObject jo = jArr.getJSONObject(0);

            paint.setTextAlign(Paint.Align.LEFT);
            paint.setTextSize(mTextSize);
            paint.setColor(GlobalColor.colorLabelName);
            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);
            canvas.translate(0, DY);
            canvas.drawText("?", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x, y, paint);
            canvas.translate(0, DY);
            int syjd = jo.getInt("syjd");
            switch (syjd) {
            case 1:
                canvas.drawText("()", x, y, paint);
                break;
            case 2:
                canvas.drawText("()", x, y, paint);
                break;
            case 3:
                canvas.drawText("()", x, y, paint);
                break;
            case 4:
                canvas.drawText("()", x, y, paint);
                break;
            default:
                canvas.drawText("", x, y, paint);
                break;
            }

            if (jo.getDouble("wb") < 0)
                paint.setColor(GlobalColor.colorPriceDown);
            else if (jo.getDouble("wb") > 0)
                paint.setColor(GlobalColor.colorpriceUp);
            else
                paint.setColor(GlobalColor.colorPriceEqual);
            paint.setTextAlign(Paint.Align.RIGHT);
            canvas.translate(width / 2, -DY * 5);
            if (jo.getInt("tp") == 1)
                canvas.drawText("", x - tips, y, paint);
            else
                canvas.drawText(Utils.dataFormation(jo.getDouble("wb") * 100, 1) + "%", x - tips, y, paint);
            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getInt("cjsl"), false), x - tips, y, paint);
            paint.setColor(GlobalColor.colorpriceUp);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("wp"), false), x - tips, y, paint);
            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(0, DY);
            canvas.drawText(Utils.dataFormation(jo.getDouble("hs") * 100, 1) + "%", x - tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText(Utils.dataFormation(jo.getDouble("jz"), 1), x - tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText(Utils.dataFormation(jo.getDouble("mgsy"), 2), x - tips, y, paint);

            paint.setTextAlign(Paint.Align.LEFT);
            paint.setColor(GlobalColor.colorLabelName);
            canvas.translate(0, -DY * 5);
            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);
            canvas.translate(0, DY);
            canvas.drawText("?", x, y, paint);
            canvas.translate(0, DY);
            if (NameRule.isBond(type))
                canvas.drawText("", x, y, paint);
            else
                canvas.drawText("PE()", x, y, paint);

            paint.setTextAlign(Paint.Align.RIGHT);
            canvas.translate(width / 2, -DY * 5);
            if (jo.getDouble("wc") < 0) {
                paint.setColor(GlobalColor.colorPriceDown);
                canvas.drawText("-" + Utils.getAmountFormat(Math.abs(jo.getDouble("wc")), true), x - tips, y,
                        paint);
            } else if (jo.getDouble("wc") > 0) {
                paint.setColor(GlobalColor.colorpriceUp);
                canvas.drawText(Utils.getAmountFormat(jo.getDouble("wc"), true), x - tips, y, paint);
            } else {
                paint.setColor(GlobalColor.colorPriceEqual);
                canvas.drawText(Utils.getAmountFormat(jo.getDouble("wc"), false), x - tips, y, paint);
            }
            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(0, DY);
            canvas.drawText(Utils.dataFormation(jo.getDouble("lb"), 1, jo.getInt("tp")), x - tips, y, paint);
            paint.setColor(GlobalColor.colorPriceDown);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("np"), false), x - tips, y, paint);
            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("gb"), true), x - tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("ltsl") * 100, true), x - tips, y, paint);
            canvas.translate(0, DY);
            if (NameRule.isBond(type)) {
                canvas.drawText(Utils.dataFormation(jo.getDouble("fullprice"), 1), x - tips, y, paint);
            } else {
                canvas.drawText(Utils.dataFormation(jo.getDouble("sy"), 1), x - tips, y, paint);
            }
        } catch (JSONException e) {
            Log.e(TAG, e.toString());
        }
    }
}

From source file:com.hgdendi.contactslist.common.FloatingBarItemDecoration.java

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);
    final int position = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
    if (position == RecyclerView.NO_POSITION) {
        return;/*from   w  w  w. j a va 2s . c  o m*/
    }
    View child = parent.findViewHolderForAdapterPosition(position).itemView;
    String initial = getTag(position);
    if (initial == null) {
        return;
    }

    boolean flag = false;
    if (getTag(position + 1) != null && !initial.equals(getTag(position + 1))) {
        if (child.getHeight() + child.getTop() < mTitleHeight) {
            c.save();
            flag = true;
            c.translate(0, child.getHeight() + child.getTop() - mTitleHeight);
        }
    }

    c.drawRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(),
            parent.getPaddingTop() + mTitleHeight, mBackgroundPaint);
    c.drawText(initial, child.getPaddingLeft() + mTextStartMargin,
            parent.getPaddingTop() + mTitleHeight - (mTitleHeight - mTextHeight) / 2 - mTextBaselineOffset,
            mTextPaint);

    if (flag) {
        c.restore();
    }
}

From source file:com.king.android.common.widget.badge.BGADragBadgeView.java

private void drawTextBadge(Canvas canvas) {
    // ?        mBadgePaint.setColor(mBadgeViewHelper.getBadgeBgColor());
    // /*w w  w  .j a  va2s  .c o  m*/
    canvas.drawRoundRect(
            new RectF(mStartX, mStartY, mStartX + mBadgeViewHelper.getBadgeRectF().width(),
                    mStartY + mBadgeViewHelper.getBadgeRectF().height()),
            mBadgeViewHelper.getBadgeRectF().height() / 2, mBadgeViewHelper.getBadgeRectF().height() / 2,
            mBadgePaint);

    // 
    mBadgePaint.setColor(mBadgeViewHelper.getBadgeTextColor());
    float x = mStartX + mBadgeViewHelper.getBadgeRectF().width() / 2;
    // ?y???        
    float y = mStartY + mBadgeViewHelper.getBadgeRectF().height() - mBadgeViewHelper.getBadgePadding();
    // 
    String badgeText = mBadgeViewHelper.getBadgeText() == null ? "" : mBadgeViewHelper.getBadgeText();
    canvas.drawText(badgeText, x, y, mBadgePaint);
}

From source file:com.qjay.android_widget.badge.BGADragBadgeView.java

private void drawTextBadge(Canvas canvas) {
    // /*  w w  w .j  a  v a2 s  .  c  o  m*/
    mBadgePaint.setColor(mBadgeViewHelper.getBadgeBgColor());
    // 
    canvas.drawRoundRect(
            new RectF(mStartX, mStartY, mStartX + mBadgeViewHelper.getBadgeRectF().width(),
                    mStartY + mBadgeViewHelper.getBadgeRectF().height()),
            mBadgeViewHelper.getBadgeRectF().height() / 2, mBadgeViewHelper.getBadgeRectF().height() / 2,
            mBadgePaint);

    // 
    mBadgePaint.setColor(mBadgeViewHelper.getBadgeTextColor());
    float x = mStartX + mBadgeViewHelper.getBadgeRectF().width() / 2;
    // ?y?
    float y = mStartY + mBadgeViewHelper.getBadgeRectF().height() - mBadgeViewHelper.getBadgePadding();
    // 
    String badgeText = mBadgeViewHelper.getBadgeText() == null ? "" : mBadgeViewHelper.getBadgeText();
    canvas.drawText(badgeText, x, y, mBadgePaint);
}

From source file:com.philliphsu.bottomsheetpickers.date.MonthView.java

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

    for (int i = 0; i < mNumDays; i++) {
        int calendarDay = (i + mWeekStart) % mNumDays;
        int x = (int) ((2 * i + 1) * dayWidthHalf + mEdgePadding);
        mDayLabelCalendar.set(Calendar.DAY_OF_WEEK, calendarDay);
        canvas.drawText(getDayOfWeekString(mDayLabelCalendar), x, y, mMonthDayLabelPaint);
    }/*  www  .  ja v a 2  s  .  c om*/
}

From source file:com.android.datetimepicker.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);
        canvas.drawText(
                mDayLabelCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault())
                        .toUpperCase(Locale.getDefault()),
                x, y, mMonthDayLabelPaint);
    }//from  ww w .  j  a  v  a2 s .c  o  m
}

From source file:edu.hawaii.android.datetimepickermaterial.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);

        canvas.drawText(
                mDayLabelCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault())
                        .toUpperCase(Locale.getDefault()).substring(0, 1),
                x, y, mMonthDayLabelPaint);
    }/*w  w w  .j  av  a 2 s.  c o m*/
}

From source file:kankan.wheel.widget.WheelView.java

/**
 * Draws rect for current value//from w ww  .ja v a  2  s.  c  o m
 * @param canvas the canvas for drawing
 */
private void drawCenterRect(Canvas canvas) {
    centerDrawable.draw(canvas);

    int left = (int) (getWidth() / 2 + itemWidth / 2) + hintPadding;
    int top = getHeight() / 2 + hintSize;
    canvas.drawText(hint, left, top, textPaint);
    if (drawCenterRunnable != null)
        drawCenterRunnable.run();
}