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:Main.java

/**
 * Draw text on canvas. Shade if text too long to fit.
 *
 * @param canvas The canvas to draw in./*from w ww .j a v  a 2s . c  om*/
 * @param text The text to draw.
 * @param x The x coordinate.
 * @param y The y coordinate.
 * @param textPaint The paint to draw with.
 * @param availableWidth The available width for the text
 */
public static void drawText(Canvas canvas, String text, float x, float y, TextPaint textPaint,
        int availableWidth) {
    text = text.replaceAll("\\r?\\n", " ");
    final TextPaint localTextPaint = new TextPaint(textPaint);
    final float pixelsToShade = 1.5F * localTextPaint.getTextSize();
    int characters = text.length();

    if (localTextPaint.measureText(text) > availableWidth) {
        Paint.Align align = localTextPaint.getTextAlign();
        float shaderStopX;
        characters = localTextPaint.breakText(text, true, availableWidth, null);
        if (align == Paint.Align.LEFT) {
            shaderStopX = x + availableWidth;
        } else if (align == Paint.Align.CENTER) {
            float[] measuredWidth = new float[1];
            characters = localTextPaint.breakText(text, true, availableWidth, measuredWidth);
            shaderStopX = x + (measuredWidth[0] / 2);
        } else { // align == Paint.Align.RIGHT
            shaderStopX = x;
        }
        // Hex 0x60000000 = first two bytes is alpha, gives semitransparent
        localTextPaint.setShader(new LinearGradient(shaderStopX - pixelsToShade, 0, shaderStopX, 0,
                localTextPaint.getColor(), localTextPaint.getColor() + 0x60000000, Shader.TileMode.CLAMP));
    }
    canvas.drawText(text, 0, characters, x, y, localTextPaint);
}

From source file:io.github.hidroh.materialistic.widget.AsteriskSpan.java

@Override
public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y,
        int bottom, @NonNull Paint paint) {
    float radius = mPadding;
    float centerX = x + radius + mPadding;
    float centerY = top + radius + mPadding;
    paint.setColor(mBackgroundColor);/*  w w  w. j  av a2  s.c  om*/
    canvas.drawCircle(centerX, centerY, radius, paint);
    paint.setColor(mTextColor);
    canvas.drawText(text, start, end, x + mPadding * 2, y, paint);
}

From source file:com.madgag.android.lazydrawables.samples.DemoListActivity.java

private ImageResourceDownloader<String, Bitmap> slowImageMaker() {
    return new ImageResourceDownloader<String, Bitmap>() {
        public Bitmap get(String key) {
            Log.d(TAG, "Asked to download " + key);
            try {
                Thread.sleep(4000L);
            } catch (InterruptedException e) {
            }/* w  ww. j  ava  2 s  .co  m*/
            Bitmap bitmap = Bitmap.createBitmap(80, 80, Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(bitmap);
            Paint paint = new Paint();
            paint.setColor(0xff0000ff);
            c.drawCircle(0, 0, 50, paint);

            paint.setColor(0xffff007f);
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            paint.setTextSize(30);
            c.drawText(key, 0, 3, 0, 60, paint);
            Log.d(TAG, "Done drawing... " + key);
            return bitmap;
        }
    };
}

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

private void ensureExpandedTexture() {
    if (this.mExpandedTitleTexture == null && !this.mExpandedBounds.isEmpty()
            && !TextUtils.isEmpty(this.mTextToDraw)) {
        this.mTextPaint.setTextSize(this.mExpandedTextSize);
        this.mTextPaint.setColor(this.mExpandedTextColor);
        int w = Math.round(this.mTextPaint.measureText(this.mTextToDraw, 0, this.mTextToDraw.length()));
        int h = Math.round(this.mTextPaint.descent() - this.mTextPaint.ascent());
        this.mTextWidth = (float) w;
        if (w > 0 || h > 0) {
            this.mExpandedTitleTexture = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(this.mExpandedTitleTexture);
            c.drawText(this.mTextToDraw, 0, this.mTextToDraw.length(), 0.0F,
                    (float) h - this.mTextPaint.descent(), this.mTextPaint);
            if (this.mTexturePaint == null) {
                this.mTexturePaint = new Paint();
                this.mTexturePaint.setAntiAlias(true);
                this.mTexturePaint.setFilterBitmap(true);
            }//from  ww w .j a va 2 s .  c o  m

        }
    }
}

From source file:com.amitupadhyay.aboutexample.util.CollapsingTextHelper.java

private void ensureExpandedTexture() {
    if (mExpandedTitleTexture != null || mExpandedBounds.isEmpty() || TextUtils.isEmpty(mTextToDraw)) {
        return;/*from  w  w  w . j a  v a2s  .co m*/
    }
    mTextPaint.setTextSize(mExpandedTextSize);
    mTextPaint.setColor(mExpandedTextColor);
    mTextureAscent = mTextPaint.ascent();
    mTextureDescent = mTextPaint.descent();
    final int w = Math.round(mTextPaint.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 - mTextPaint.descent(), mTextPaint);
    if (mTexturePaint == null) {
        // Make sure we have a paint
        mTexturePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    }
}

From source file:xyz.berial.textinputlayout.CollapsingTextHelper.java

private void ensureExpandedTexture() {
    if (mExpandedTitleTexture != null || mExpandedBounds.isEmpty() || TextUtils.isEmpty(mTextToDraw)) {
        return;/*from  w ww  .ja va2  s .co m*/
    }

    mTextPaint.setTextSize(mExpandedTextSize);
    mTextPaint.setColor(mExpandedTextColor);
    mTextureAscent = mTextPaint.ascent();
    mTextureDescent = mTextPaint.descent();

    final int w = Math.round(mTextPaint.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 - mTextPaint.descent(), mTextPaint);

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

From source file:org.buffer.android.buffertextinputlayout.util.CollapsingTextHelper.java

private void ensureExpandedTexture() {
    if (mExpandedTitleTexture != null || mExpandedBounds.isEmpty() || TextUtils.isEmpty(mTextToDraw)) {
        return;//from   w w w  .  ja  v a 2  s . c o  m
    }
    calculateOffsets(0f);
    mTextureAscent = mTextPaint.ascent();
    mTextureDescent = mTextPaint.descent();
    final int w = Math.round(mTextPaint.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 - mTextPaint.descent(), mTextPaint);
    if (mTexturePaint == null) {
        // Make sure we have a paint
        mTexturePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    }
}

From source file:com.tr4android.support.extension.widget.CollapsingTextHelper.java

private void ensureExpandedTexture() {
    if (mExpandedTitleTexture != null || mExpandedBounds.isEmpty() || TextUtils.isEmpty(mTextToDraw)) {
        return;//from w w w.  j  ava 2s  . c  o m
    }

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

    final int w = Math.round(mTextPaint.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 - mTextPaint.descent(), 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.CollapsingTextHelper.java

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

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

    final int w = Math.round(mTextPaint.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 - mTextPaint.descent(), mTextPaint);

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

From source file:com.android.mail.browse.ConversationItemView.java

private static void drawText(Canvas canvas, CharSequence s, int x, int y, TextPaint paint) {
    canvas.drawText(s, 0, s.length(), x, y, paint);
}