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 CharSequence text, int start, int end, float x, float y, @NonNull Paint paint) 

Source Link

Document

Draw the specified range of text, specified by start/end, with its origin at (x,y), in the specified Paint.

Usage

From source file:net.opacapp.multilinecollapsingtoolbar.CollapsingTextHelper.java

private void ensureCollapsedTexture() {
    if (mCollapsedTitleTexture != null || mCollapsedBounds.isEmpty() || TextUtils.isEmpty(mTextToDraw)) {
        return;/*w ww.  ja  v  a2  s . c  o  m*/
    }
    calculateOffsets(0f);
    final int w = Math.round(mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length()));
    final int h = Math.round(mTextPaint.descent() - mTextPaint.ascent());
    if (w <= 0 && h <= 0) {
        return; // If the width or height are 0, return
    }
    mCollapsedTitleTexture = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(mCollapsedTitleTexture);
    c.drawText(mTextToDrawCollapsed, 0, mTextToDrawCollapsed.length(), 0, -mTextPaint.ascent() / mScale,
            mTextPaint);
    if (mTexturePaint == null) {
        // Make sure we have a paint
        mTexturePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    }
}

From source file:net.opacapp.multilinecollapsingtoolbar.CollapsingTextHelper.java

private void ensureCrossSectionTexture() {
    if (mCrossSectionTitleTexture != null || mCollapsedBounds.isEmpty() || TextUtils.isEmpty(mTextToDraw)) {
        return;/*from   w ww. j av  a  2s.  c om*/
    }
    calculateOffsets(0f);
    final int w = Math
            .round(mTextPaint.measureText(mTextToDraw, mTextLayout.getLineStart(0), mTextLayout.getLineEnd(0)));
    final int h = Math.round(mTextPaint.descent() - mTextPaint.ascent());
    if (w <= 0 && h <= 0) {
        return; // If the width or height are 0, return
    }
    mCrossSectionTitleTexture = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(mCrossSectionTitleTexture);
    c.drawText(mTextToDraw, mTextLayout.getLineStart(0), mTextLayout.getLineEnd(0), 0,
            -mTextPaint.ascent() / mScale, mTextPaint);
    if (mTexturePaint == null) {
        // Make sure we have a paint
        mTexturePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    }
}

From source file:android.support.design.widget.CustomCollapsingTextHelper.java

private void ensureExpandedTexture() {
    if (mExpandedTitleTexture != null || mExpandedBounds.isEmpty() || TextUtils.isEmpty(mTextToDraw)) {
        return;//  w ww  .ja  v  a 2s  . c o m
    }

    calculateOffsets(0f);
    mTextureAscent = mTitlePaint.ascent();
    mTextureDescent = mTitlePaint.descent();

    final int w = Math.round(mTitlePaint.measureText(mTextToDraw, 0, mTextToDraw.length()));
    final int h = Math.round(mTextureDescent - mTextureAscent);

    if (w <= 0 || h <= 0) {
        return; // If the width or height are 0, return
    }

    mExpandedTitleTexture = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(mExpandedTitleTexture);
    c.drawText(mTextToDraw, 0, mTextToDraw.length(), 0, h - mTitlePaint.descent(), mTitlePaint);

    if (mTexturePaint == null) {
        // Make sure we have a paint
        mTexturePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    }
}

From source file:com.googlecode.eyesfree.example.bargraph.BarGraphView.java

/**
 * Draws the title of the bar graph.//from  ww  w .  j av  a 2  s  . com
 *
 * @param c The canvas on which to draw.
 */
private void drawTitle(Canvas c) {
    mPaint.reset();
    mPaint.setAntiAlias(true);
    mPaint.setColor(mTitleColor);
    mPaint.setTextSize(mTitleSize);
    mPaint.setTextAlign(Align.CENTER);
    mPaint.getTextBounds(mTitle.toString(), 0, mTitle.length(), mTempRect);

    final float x = (getWidth() / 2.0f);
    final float y = mTempRect.height();
    c.drawText(mTitle, 0, mTitle.length(), x, y, mPaint);
}

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

/**
 * Draw the title on the supplied Canvas.
 *
 * @param canvas The Canvas/*from   w  ww  .ja  v  a2 s  .  c o  m*/
 */
private void writeTitle(@NonNull Canvas canvas) {
    canvas.drawText(mTitle, 0, mTitle.length(),
            mBlockSize * mNumCols / 2 + mContentRect.left + mContentOffset.x,
            mBlockSize + mContentRect.top + mContentOffset.y, mTitlePaint);
}

From source file:com.example.psumaps.MapView.java

private void drawMarker(LatLng point, String str) {
    // Creating an instance of MarkerOptions
    MarkerOptions markerOptions = new MarkerOptions();

    //IconGenerator tc = new IconGenerator(this);
    //Bitmap bmp = tc.makeIcon("hello");

    Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.bubble_white);// get the image same as your EditText

    int h = bitmap.getHeight() * 2;
    int w = bitmap.getWidth() * 2;

    bitmap = convertToMutable(bitmap);// converting the bitmap to mutable
    Bitmap incrsize = Bitmap.createScaledBitmap(bitmap, w, h, false);

    Canvas cs = new Canvas(incrsize);
    Paint pt = new Paint();
    pt.setColor(Color.BLACK);/*from  w w w  .j a va  2 s .c om*/
    //Typeface tf = Typeface.create("Helvetica",Typeface.BOLD);
    pt.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

    String iam = str.toUpperCase(Locale.getDefault());

    pt.setTextSize(25);
    cs.drawText(iam, 0, iam.length(), (h / 2) + 10, (w / 2) / 2, pt);
    //pt.setColor(Color.RED);
    //cs.drawText("this is praki", 0, 13, h / 2, w / 3, pt);

    // Setting latitude and longitude for the marker
    markerOptions.position(point).title(str).icon(BitmapDescriptorFactory.fromBitmap(incrsize));

    // Adding marker on the Google Map
    map.addMarker(markerOptions);
}

From source file:com.mukesh.OtpView.java

private void drawTextAtBox(Canvas canvas, Paint paint, CharSequence text, int charAt) {
    paint.getTextBounds(text.toString(), charAt, charAt + 1, textRect);
    float cx = itemCenterPoint.x;
    float cy = itemCenterPoint.y;
    float x = cx - Math.abs((float) textRect.width()) / 2 - textRect.left;
    float y = cy + Math.abs((float) textRect.height()) / 2 - textRect.bottom;
    canvas.drawText(text, charAt, charAt + 1, x, y, paint);
}

From source file:com.alimuzaffar.lib.widgets.PinEntryEditText.java

@Override
protected void onDraw(Canvas canvas) {
    //super.onDraw(canvas);
    CharSequence text = getFullText();
    int textLength = text.length();
    float[] textWidths = new float[textLength];
    getPaint().getTextWidths(text, 0, textLength, textWidths);

    for (int i = 0; i < mNumChars; i++) {
        //If a background for the pin characters is specified, it should be behind the characters.
        if (mPinBackground != null) {
            updateDrawableState(i < textLength, i == textLength);
            mPinBackground.setBounds((int) mLineCoords[i].left, (int) mLineCoords[i].top,
                    (int) mLineCoords[i].right, (int) mLineCoords[i].bottom);
            mPinBackground.draw(canvas);
        }/*w  w w . java 2 s  .  c  o  m*/
        if (textLength > i) {
            float middle = mLineCoords[i].left + mCharSize / 2;
            if (!mAnimate || i != textLength - 1) {
                canvas.drawText(text, i, i + 1, middle - textWidths[i] / 2, mCharBottom[i], mCharPaint);
            } else {
                canvas.drawText(text, i, i + 1, middle - textWidths[i] / 2, mCharBottom[i], mLastCharPaint);
            }
        }
        //The lines should be in front of the text (because that's how I want it).
        if (mPinBackground == null) {
            updateColorForLines(i <= textLength);
            canvas.drawLine(mLineCoords[i].left, mLineCoords[i].top, mLineCoords[i].right,
                    mLineCoords[i].bottom, mLinesPaint);
        }
    }
}

From source file:org.mariotaku.twidere.text.OriginalStatusSpan.java

@Override
public void draw(final Canvas canvas, final CharSequence text, final int start, final int end, final float x,
        final int top, final int y, final int bottom, final Paint paint) {
    if (!(paint instanceof TextPaint))
        return;/*  w  w  w .  j av  a2 s  . c  om*/
    final TextPaint tp = (TextPaint) paint;
    mBounds.left = x;
    mBounds.right = x + paint.measureText(text, start, end) + mPadding * 2;
    mBounds.top = top;
    mBounds.bottom = bottom;
    final int innerTextColor = TwidereColorUtils.getContrastYIQ(tp.linkColor, ThemeUtils.ACCENT_COLOR_THRESHOLD,
            mDarkLightColors[0], mDarkLightColors[1]);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(tp.linkColor);
    mBounds.inset(mPaint.getStrokeWidth() / 2, mPaint.getStrokeWidth() / 2);
    canvas.drawRoundRect(mBounds, mCornerRadius, mCornerRadius, mPaint);
    mBounds.inset(-mPaint.getStrokeWidth() / 2, -mPaint.getStrokeWidth() / 2);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setColor(
            ColorUtils.compositeColors(ColorUtils.setAlphaComponent(innerTextColor, 0x80), tp.linkColor));
    mBounds.inset(mPaint.getStrokeWidth() / 2, mPaint.getStrokeWidth() / 2);
    canvas.drawRoundRect(mBounds, mCornerRadius, mCornerRadius, mPaint);
    paint.setColor(innerTextColor);
    canvas.drawText(text, start, end, x + mPadding,
            top + (bottom - top) / 2 - (paint.descent() + paint.ascent()) / 2, paint);
}

From source file:com.lamcreations.scaffold.common.utils.CollapsingTextHelper.java

public void draw(Canvas canvas) {
    int saveCount = canvas.save();
    if (this.mTextToDraw != null) {
        boolean isRtl = this.mIsRtl;
        float x = isRtl ? this.mCurrentRight : this.mCurrentLeft;
        float y = this.mCurrentTop;
        boolean drawTexture = this.mUseTexture && this.mExpandedTitleTexture != null;
        this.mTextPaint.setTextSize(this.mCurrentTextSize);
        float ascent;
        if (drawTexture) {
            ascent = this.mTextureAscent * this.mScale;
        } else {/*w w w  .j  av a2 s . c o m*/
            ascent = this.mTextPaint.ascent() * this.mScale;
        }

        if (drawTexture) {
            y += ascent;
        }

        if (this.mScale != 1.0F) {
            canvas.scale(this.mScale, this.mScale, x, y);
        }

        if (isRtl) {
            x -= this.mTextWidth;
        }

        if (drawTexture) {
            canvas.drawBitmap(this.mExpandedTitleTexture, x, y, this.mTexturePaint);
        } else {
            canvas.drawText(this.mTextToDraw, 0, this.mTextToDraw.length(), x, y, this.mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}