Example usage for android.graphics Paint setTextSize

List of usage examples for android.graphics Paint setTextSize

Introduction

In this page you can find the example usage for android.graphics Paint 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 getTextHeightByBounds(String text, float textSize) {
    Paint paint = new Paint();
    Rect bounds = new Rect();
    paint.setTextSize(textSize);
    paint.getTextBounds(text, 0, text.length(), bounds);
    return bounds.height();
}

From source file:Main.java

private static void setTextSizeForWidth(String text, Paint paint, float desiredWidth) {

    final float testTextSize = 48f;
    paint.setTextSize(testTextSize);
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    float desiredTextSize = testTextSize * desiredWidth / bounds.width();
    paint.setTextSize(desiredTextSize);//from w  w w . ja v a  2 s .c om
}

From source file:Main.java

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

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

public static float setTextSizeForWidth(Paint paint, float desiredWidth, String text) {

    final float testTextSize = 100;
    paint.setTextSize(testTextSize);
    final float testWidth = paint.measureText(text);

    // Calculate the desired size as a proportion of our testTextSize.
    float desiredTextSize = testTextSize * desiredWidth / testWidth;

    // Set the paint for that size.
    paint.setTextSize(desiredTextSize);//  ww w . j  a  v  a 2  s  .  co  m
    return desiredTextSize;
}

From source file:Main.java

public static Bitmap addLabelToBitmap(Bitmap src, String label) {
    float densityFactor = Resources.getSystem().getDisplayMetrics().density;
    final float textPadding = src.getWidth() * 0.05f;

    Bitmap result = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Paint textPaint = new Paint();
    textPaint.setAntiAlias(true);/*from ww  w.  j  a  v  a  2s. c o m*/
    textPaint.setTextSize(12 * densityFactor);
    textPaint.setColor(Color.WHITE);
    textPaint.setStrokeWidth(2 * densityFactor);
    textPaint.setShadowLayer(1 * densityFactor, 0, 0, Color.BLACK);

    float textWidth = textPaint.measureText(label);

    float scaleFactor = (src.getWidth() - textPadding * 2) / textWidth;

    canvas.drawBitmap(src, 0, 0, textPaint);

    canvas.save();
    canvas.scale(scaleFactor, scaleFactor);
    float textPosX = (src.getWidth() / scaleFactor - textWidth) / 2;
    float textPosY = (src.getHeight() - textPadding) / scaleFactor;

    canvas.drawText(label, textPosX, textPosY, textPaint);
    canvas.restore();
    return result;
}

From source file:Main.java

public static float getTextWidth(Paint paint, String text, float textSize) {
    if (TextUtils.isEmpty(text)) {
        return 0;
    }/*from   w  ww  .j av  a  2  s. c o m*/
    paint.setTextSize(textSize);
    float[] widths = new float[text.length()];
    paint.getTextWidths(text, widths);
    float totalWidth = 0;
    for (int i = 0; i < widths.length; i++) {
        totalWidth += widths[i];
    }
    return totalWidth;
}

From source file:Main.java

public static Paint getPaint(Paint.Style style, int color) {
    Paint mPaint = new Paint();
    mPaint.setAntiAlias(true);//from  w  w w .j a va  2s. c  o  m
    mPaint.setStyle(style);
    mPaint.setColor(color);
    mPaint.setTextSize(30);
    return mPaint;
}

From source file:Main.java

public static float setTextSizeForWidth(Paint paint, float desiredWidth, String text, float max, float min) {
    final float textSize = 12.0f;

    paint.setTextSize(textSize);
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    float desiredTextSize = textSize * desiredWidth / bounds.width();
    if (desiredTextSize > max) {
        desiredTextSize = max;/*from w ww.  j  a  v  a2s.c  om*/
    } else if (desiredTextSize < min) {
        desiredTextSize = min;
    }
    paint.setTextSize(desiredTextSize);
    return desiredTextSize;
}

From source file:Main.java

private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) {
    final Paint paint = new Paint();
    paint.setAntiAlias(true);/*  w w w.  j av  a  2s  .c om*/
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(textSize);
    paint.setColor(color);
    final Rect bounds = new Rect();
    paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
    final int width = Math.round(bounds.width() + 0.5f);
    final int height = Math.round(bounds.height() + 0.5f);
    final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(buffer);
    canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint);
    return new BitmapDrawable(res, buffer);
}

From source file:Main.java

static Bitmap generatorContactCountIcon(Context context, Bitmap icon) {
    int iconSize = (int) context.getResources().getDimension(android.R.dimen.app_icon_size);
    Bitmap contactIcon = Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888);
    Canvas canvas = new Canvas(contactIcon);

    Paint iconPaint = new Paint();
    iconPaint.setDither(true);/*from   w w w . j av a2 s  .com*/
    iconPaint.setFilterBitmap(true);
    Rect src = new Rect(0, 0, icon.getWidth(), icon.getHeight());
    Rect dst = new Rect(0, 0, iconSize, iconSize);
    canvas.drawBitmap(icon, src, dst, iconPaint);
    int contacyCount = 11;
    Paint countPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
    countPaint.setColor(Color.RED);
    countPaint.setTextSize(20f);
    countPaint.setTypeface(Typeface.DEFAULT_BOLD);
    canvas.drawText(String.valueOf(contacyCount), iconSize - 18, 25, countPaint);
    return contactIcon;
}