Example usage for android.text TextPaint getColor

List of usage examples for android.text TextPaint getColor

Introduction

In this page you can find the example usage for android.text TextPaint getColor.

Prototype

@ColorInt
public int getColor() 

Source Link

Document

Return the paint's color.

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  w  w .j  a v a  2s  .c  o m*/
 * @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:com.android.ex.chips.RecipientEditTextView.java

private DrawableRecipientChip constructChipSpan(final RecipientEntry contact, final boolean pressed,
        final boolean leaveIconSpace) throws NullPointerException {
    if (mChipBackground == null)
        throw new NullPointerException("Unable to render any chips as setChipDimensions was not called.");
    final TextPaint paint = getPaint();
    final float defaultSize = paint.getTextSize();
    final int defaultColor = paint.getColor();
    Bitmap tmpBitmap;/*from  w  ww.j a  v a  2 s.c o m*/
    if (pressed)
        tmpBitmap = createSelectedChip(contact, paint);
    else
        tmpBitmap = createUnselectedChip(contact, paint, leaveIconSpace);
    // Pass the full text, un-ellipsized, to the chip.
    final Drawable result = new BitmapDrawable(getResources(), tmpBitmap);
    result.setBounds(0, 0, tmpBitmap.getWidth(), tmpBitmap.getHeight());
    final DrawableRecipientChip recipientChip = new VisibleRecipientChip(result, contact);
    // Return text to the original size.
    paint.setTextSize(defaultSize);
    paint.setColor(defaultColor);
    return recipientChip;
}