Example usage for android.text TextPaint setTextSize

List of usage examples for android.text TextPaint setTextSize

Introduction

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

Prototype

public void setTextSize(float textSize) 

Source Link

Document

Set the paint's text size.

Usage

From source file:Main.java

public static int GetTextWidth(String text, float Size) {
    TextPaint FontPaint = new TextPaint();
    FontPaint.setTextSize(Size);
    return (int) FontPaint.measureText(text);
}

From source file:Main.java

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

From source file:Main.java

private static int getLineCount(CharSequence text, TextPaint paint, float size, float width,
        DisplayMetrics displayMetrics) {
    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, size, displayMetrics));
    StaticLayout layout = new StaticLayout(text, paint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
            true);//from   w  w w  .j ava 2 s . c o  m
    return layout.getLineCount();
}

From source file:Main.java

public static TextPaint setTextSize(Context c, TextPaint paint, float size) {
    Resources r;/* w w w. j a va  2 s.co  m*/
    if (c == null) {
        r = Resources.getSystem();
    } else {
        r = c.getResources();
    }
    if (r != null) {
        paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, size, r.getDisplayMetrics()));
    }
    return paint;
}

From source file:com.hellofyc.base.util.ViewUtils.java

/**
 * ?//from ww  w  .  j a  v  a  2 s. c om
 */
public static float getTextWidth(String text, float size) {
    TextPaint fontPaint = new TextPaint();
    fontPaint.setTextSize(size);
    return fontPaint.measureText(text);
}

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 ww .j ava2  s .  c  om
            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:enterprayz.megatools.Tools.java

public static int getTextHeight(String text, int maxWidth, float textSize, Typeface typeface) {
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    paint.setTextSize(textSize);
    paint.setTypeface(typeface);//from w  w  w  .ja v a 2  s .com

    int lineCount = 0;

    int index = 0;
    int length = text.length();

    while (index < length - 1) {
        index += paint.breakText(text, index, length, true, maxWidth, null);
        lineCount++;
    }

    Rect bounds = new Rect();
    paint.getTextBounds("Py", 0, 2, bounds);
    return (int) Math.floor(lineCount * bounds.height());
}

From source file:Main.java

/**
 * Recursive binary search to find the best size for the text.
 *///from  ww  w  . j a v a2 s .c o m
private static float getAutofitTextSize(CharSequence text, TextPaint paint, float targetWidth, int maxLines,
        float low, float high, float precision, DisplayMetrics displayMetrics) {
    float mid = (low + high) / 2.0f;
    int lineCount = 1;
    StaticLayout layout = null;

    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mid, displayMetrics));

    if (maxLines != 1) {
        layout = new StaticLayout(text, paint, (int) targetWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
                true);
        lineCount = layout.getLineCount();
    }

    if (SPEW)
        Log.d(TAG, "low=" + low + " high=" + high + " mid=" + mid + " target=" + targetWidth + " maxLines="
                + maxLines + " lineCount=" + lineCount);

    if (lineCount > maxLines) {
        // For the case that `text` has more newline characters than `maxLines`.
        if ((high - low) < precision) {
            return low;
        }
        return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
    } else if (lineCount < maxLines) {
        return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
    } else {
        float maxLineWidth = 0;
        if (maxLines == 1) {
            maxLineWidth = paint.measureText(text, 0, text.length());
        } else {
            for (int i = 0; i < lineCount; i++) {
                if (layout.getLineWidth(i) > maxLineWidth) {
                    maxLineWidth = layout.getLineWidth(i);
                }
            }
        }

        if ((high - low) < precision) {
            return low;
        } else if (maxLineWidth > targetWidth) {
            return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
        } else if (maxLineWidth < targetWidth) {
            return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
        } else {
            return mid;
        }
    }
}

From source file:com.agenmate.lollipop.util.ViewUtils.java

/**
 * Recursive binary search to find the best size for the text.
 *
 * Adapted from https://github.com/grantland/android-autofittextview
 *//*from  w ww .j av  a  2s.co  m*/
public static float getSingleLineTextSize(String text, TextPaint paint, float targetWidth, float low,
        float high, float precision, DisplayMetrics metrics) {
    final float mid = (low + high) / 2.0f;

    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mid, metrics));
    final float maxLineWidth = paint.measureText(text);

    if ((high - low) < precision) {
        return low;
    } else if (maxLineWidth > targetWidth) {
        return getSingleLineTextSize(text, paint, targetWidth, low, mid, precision, metrics);
    } else if (maxLineWidth < targetWidth) {
        return getSingleLineTextSize(text, paint, targetWidth, mid, high, precision, metrics);
    } else {
        return mid;
    }
}

From source file:org.cocos2dx.lib.Cocos2dxBitmap.java

private static String getStringWithEllipsis(String originalText, float width, float fontSize) {
    if (TextUtils.isEmpty(originalText)) {
        return "";
    }/*from   w w w. j a v  a 2  s .com*/

    TextPaint paint = new TextPaint();
    paint.setTypeface(Typeface.DEFAULT);
    paint.setTextSize(fontSize);

    return TextUtils.ellipsize(originalText, paint, width, TextUtils.TruncateAt.valueOf("END")).toString();
}