Example usage for android.text TextPaint TextPaint

List of usage examples for android.text TextPaint TextPaint

Introduction

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

Prototype

public TextPaint(Paint p) 

Source Link

Usage

From source file:Main.java

static int getTextHeight(CharSequence text, TextPaint paint, int targetWidth, float textSize) {
    TextPaint paintCopy = new TextPaint(paint);
    paintCopy.setTextSize(textSize);//from w w  w  .j a v a 2  s. co  m
    StaticLayout layout = new StaticLayout(text, paintCopy, (int) targetWidth, Layout.Alignment.ALIGN_NORMAL,
            1.0f, 0.0f, true);
    return layout.getHeight();
}

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. jav  a  2 s . 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:Main.java

public static Bitmap text2Bitmap(String text, int color, float size) {
    if (TextUtils.isEmpty(text)) {
        return null;
    }//from   w  w  w . jav a  2s.  c  o m

    Paint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(size);
    paint.setColor(color);
    paint.setTextAlign(Paint.Align.LEFT);

    float baseline = -paint.ascent();
    int width = (int) (paint.measureText(text) + 0.5f);
    int height = (int) (baseline + paint.descent() + 0.5f);

    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    canvas.drawText(text, 0, baseline, paint);
    return image;
}

From source file:com.irccloud.android.data.model.Avatar.java

public static Bitmap generateBitmap(String text, int textColor, int bgColor, boolean isDarkTheme, int size,
        boolean round) {
    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    if (bitmap != null) {
        Canvas c = new Canvas(bitmap);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setStyle(Paint.Style.FILL);

        if (isDarkTheme || !round) {
            p.setColor(bgColor);/*from  w w w.  ja  va2  s  .  c o m*/
            if (round)
                c.drawCircle(size / 2, size / 2, size / 2, p);
            else
                c.drawColor(bgColor);
        } else {
            float[] hsv = new float[3];
            Color.colorToHSV(bgColor, hsv);
            hsv[2] *= 0.8f;
            p.setColor(Color.HSVToColor(hsv));
            c.drawCircle(size / 2, size / 2, (size / 2) - 2, p);
            p.setColor(bgColor);
            c.drawCircle(size / 2, (size / 2) - 2, (size / 2) - 2, p);
        }
        TextPaint tp = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        tp.setTextAlign(Paint.Align.CENTER);
        tp.setTypeface(font);
        tp.setTextSize((int) (size * 0.65));
        tp.setColor(textColor);
        if (isDarkTheme || !round) {
            c.drawText(text, size / 2, (size / 2) - ((tp.descent() + tp.ascent()) / 2), tp);
        } else {
            c.drawText(text, size / 2, (size / 2) - 4 - ((tp.descent() + tp.ascent()) / 2), tp);
        }

        return bitmap;
    } else {
        return null;
    }
}

From source file:io.plaidapp.ui.widget.CutoutTextView.java

public CutoutTextView(Context context, AttributeSet attrs) {
    super(context, attrs);

    textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);

    final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CutoutTextView, 0, 0);
    if (a.hasValue(R.styleable.CutoutTextView_android_fontFamily)) {
        try {//ww  w .j a  v a  2s  . c o  m
            Typeface font = ResourcesCompat.getFont(getContext(),
                    a.getResourceId(R.styleable.CutoutTextView_android_fontFamily, 0));
            if (font != null) {
                textPaint.setTypeface(font);
            }
        } catch (Resources.NotFoundException nfe) {
        }
    }
    if (a.hasValue(R.styleable.CutoutTextView_foregroundColor)) {
        foregroundColor = a.getColor(R.styleable.CutoutTextView_foregroundColor, foregroundColor);
    }
    if (a.hasValue(R.styleable.CutoutTextView_android_text)) {
        text = a.getString(R.styleable.CutoutTextView_android_text);
    }
    maxTextSize = context.getResources().getDimensionPixelSize(R.dimen.display_4_text_size);
    a.recycle();
}

From source file:org.telegram.ui.Cells.BotHelpCell.java

public BotHelpCell(Context context) {
    super(context);

    textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setTextSize(AndroidUtilities.dp(16));
    textPaint.setColor(ContextCompat.getColor(context, R.color.primary_text));
    textPaint.linkColor = Theme.MSG_LINK_TEXT_COLOR;

    urlPaint = new Paint();
    urlPaint.setColor(Theme.MSG_LINK_SELECT_BACKGROUND_COLOR);
}

From source file:org.telegram.ui.Cells.AboutLinkCell.java

public AboutLinkCell(Context context) {
    super(context);

    setElevation(AndroidUtilities.dp(2));
    setBackgroundColor(ContextCompat.getColor(context, R.color.card_background));

    textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setTextSize(AndroidUtilities.dp(16));
    textPaint.setColor(ContextCompat.getColor(context, R.color.primary_text));
    textPaint.linkColor = Theme.MSG_LINK_TEXT_COLOR;

    urlPaint = new Paint();
    urlPaint.setColor(Theme.MSG_LINK_SELECT_BACKGROUND_COLOR);

    imageView = new ImageView(context);
    imageView.setScaleType(ImageView.ScaleType.CENTER);
    addView(imageView,//from   www . j  av a2  s  .co  m
            LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT,
                    (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP,
                    LocaleController.isRTL ? 0 : 16, 5, LocaleController.isRTL ? 16 : 0, 0));
    setWillNotDraw(false);
}

From source file:org.telegram.ui.Cells.HintDialogCell.java

public HintDialogCell(Context context) {
    super(context);
    setBackgroundResource(R.drawable.list_selector);

    imageView = new BackupImageView(context);
    imageView.setRoundRadius(AndroidUtilities.dp(27));
    addView(imageView, LayoutHelper.createFrame(54, 54, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 7, 0, 0));

    nameTextView = new TextView(context);
    nameTextView.setTextColor(ContextCompat.getColor(context, R.color.primary_text));
    nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
    nameTextView.setMaxLines(2);/*w  w w . j  ava2  s . com*/
    nameTextView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    nameTextView.setLines(2);
    nameTextView.setEllipsize(TextUtils.TruncateAt.END);
    addView(nameTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT,
            Gravity.LEFT | Gravity.TOP, 6, 64, 6, 0));

    if (countDrawable == null) {
        countDrawable = getResources().getDrawable(R.drawable.dialogs_badge);
        countDrawableGrey = getResources().getDrawable(R.drawable.dialogs_badge2);

        countPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
        countPaint.setColor(0xffffffff);
        countPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    }
    countPaint.setTextSize(AndroidUtilities.dp(13));
}

From source file:io.plaidapp.ui.widget.FabOverlapTextView.java

private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FabOverlapTextView);

    float defaultTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, DEFAULT_TEXT_SIZE_SP,
            getResources().getDisplayMetrics());

    setFabOverlapGravity(a.getInt(R.styleable.FabOverlapTextView_fabGravity, Gravity.BOTTOM | Gravity.RIGHT));
    setFabOverlapHeight(a.getDimensionPixelSize(R.styleable.FabOverlapTextView_fabOverlayHeight, 0));
    setFabOverlapWidth(a.getDimensionPixelSize(R.styleable.FabOverlapTextView_fabOverlayWidth, 0));

    if (a.hasValue(R.styleable.FabOverlapTextView_android_textAppearance)) {
        final int textAppearance = a.getResourceId(R.styleable.FabOverlapTextView_android_textAppearance,
                android.R.style.TextAppearance);
        TypedArray atp = getContext().obtainStyledAttributes(textAppearance, R.styleable.FontTextAppearance);
        paint.setColor(atp.getColor(R.styleable.FontTextAppearance_android_textColor, Color.BLACK));
        paint.setTextSize(atp.getDimensionPixelSize(R.styleable.FontTextAppearance_android_textSize,
                (int) defaultTextSize));
        if (atp.hasValue(R.styleable.FontTextAppearance_font)) {
            paint.setTypeface(FontUtil.get(getContext(), atp.getString(R.styleable.FontTextAppearance_font)));
        }//ww w.  j  a  v  a  2  s  . c o  m
        atp.recycle();
    }

    if (a.hasValue(R.styleable.FabOverlapTextView_font)) {
        setFont(a.getString(R.styleable.FabOverlapTextView_font));
    }

    if (a.hasValue(R.styleable.FabOverlapTextView_android_textColor)) {
        setTextColor(a.getColor(R.styleable.FabOverlapTextView_android_textColor, 0));
    }
    if (a.hasValue(R.styleable.FabOverlapTextView_android_textSize)) {
        setTextSize(a.getDimensionPixelSize(R.styleable.FabOverlapTextView_android_textSize,
                (int) defaultTextSize));
    }

    lineHeightHint = a.getDimensionPixelSize(R.styleable.FabOverlapTextView_lineHeightHint, 0);
    topPaddingHint = a.getDimensionPixelSize(R.styleable.FabOverlapTextView_topPaddingHint, 0);

    breakStrategy = a.getInt(R.styleable.FabOverlapTextView_android_breakStrategy,
            Layout.BREAK_STRATEGY_BALANCED);

    a.recycle();
}

From source file:org.telegram.ui.Cells.SharedLinkCell.java

public SharedLinkCell(Context context) {
    super(context);

    if (titleTextPaint == null) {
        titleTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        titleTextPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));

        TEXT_COLOR = ContextCompat.getColor(context, R.color.primary_text);
        titleTextPaint.setColor(TEXT_COLOR);

        descriptionTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);

        paint = new Paint();
        paint.setColor(ContextCompat.getColor(context, R.color.divider));
        paint.setStrokeWidth(1);/*from  w w w  . j a v a  2 s.  c  om*/

        urlPaint = new Paint();
        urlPaint.setColor(Theme.MSG_LINK_SELECT_BACKGROUND_COLOR);
    }

    titleTextPaint.setTextSize(AndroidUtilities.dp(16));
    descriptionTextPaint.setTextSize(AndroidUtilities.dp(16));

    setWillNotDraw(false);
    linkImageView = new ImageReceiver(this);
    letterDrawable = new LetterDrawable();

    checkBox = new CheckBox(context, R.drawable.round_check2);
    checkBox.setVisibility(INVISIBLE);
    addView(checkBox,
            LayoutHelper.createFrame(22, 22,
                    (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP,
                    LocaleController.isRTL ? 0 : 44, 44, LocaleController.isRTL ? 44 : 0, 0));
}