Example usage for android.text TextPaint measureText

List of usage examples for android.text TextPaint measureText

Introduction

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

Prototype

public float measureText(String text) 

Source Link

Document

Return the width of the text.

Usage

From source file:Main.java

public static float getFontlength(TextPaint paint, String str) {
    return paint.measureText(str);
}

From source file:Main.java

public static float getStringWidth(String str, TextPaint paint) {
    float strWidth = paint.measureText(str);
    return strWidth;
}

From source file:Main.java

public static int GetTextWidth(String text, float Size) {
    TextPaint FontPaint = new TextPaint();
    FontPaint.setTextSize(Size);//www  .j  ava 2 s  . c  o m
    return (int) FontPaint.measureText(text);
}

From source file:Main.java

/**
 * Draw text on canvas. Shade if text too long to fit.
 *
 * @param canvas The canvas to draw in.//w w  w. ja  va 2s  .  com
 * @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.hellofyc.base.util.ViewUtils.java

/**
 * ?/*from   w w w . j ava  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:Main.java

public static String fillTextBox(TextPaint paint, int fragmentWidth, String source) {
    StringBuilder sb = new StringBuilder();
    final int length = source.length();
    // Display whole words only
    int lastWhiteSpace = 0;

    for (int index = 0; paint.measureText(sb.toString()) < fragmentWidth && index < length; index++) {
        char c = source.charAt(index);
        if (Character.isWhitespace(c))
            lastWhiteSpace = index;/*from w  w w  .ja  v  a2  s.  com*/
        sb.append(c);
    }

    if (sb.length() != length) {
        // Delete last word part
        sb.delete(lastWhiteSpace, sb.length());
        sb.append("...");
    }

    return sb.toString();

}

From source file:Main.java

public static String fillTextBox(TextPaint paint, int fragmentWidth, String source, int start) {
    StringBuilder sb = new StringBuilder();
    final int length = source.length();
    int indexLeft = start;
    int indexRight = start + 1;
    int lastWhiteSpaceL = 0;
    int lastWhiteSpaceR = 0;
    while (paint.measureText(sb.toString()) < fragmentWidth && (indexLeft >= 0 || indexRight < length)) {
        if (indexLeft >= 0) {
            char c = source.charAt(indexLeft);
            if (Character.isWhitespace(c))
                lastWhiteSpaceL = indexLeft;
            sb.insert(0, c);//from  w w  w . j a va2s . c om
            indexLeft--;
        }

        if (indexRight < length) {
            char c = source.charAt(indexRight);
            if (Character.isWhitespace(c))
                lastWhiteSpaceR = indexRight;
            sb.append(c);
            indexRight++;
        }
    }

    if (indexLeft >= 0) {
        // Delete first word part
        sb.delete(0, lastWhiteSpaceL - indexLeft);
        sb.insert(0, "...");
        indexLeft = lastWhiteSpaceL - 3; // Set new index left
    }

    if (indexRight < length) {
        // Delete last word part
        sb.delete(lastWhiteSpaceR - (indexLeft + 1), sb.length());
        sb.append("...");
    }

    return sb.toString();
}

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
 *///w  w w  . j  a va 2  s.  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:au.com.zacher.popularmovies.activity.layout.CollapsingTitleLayout.java

/**
 * Recursive binary search to find the best size for the text
 *
 * Adapted from https://github.com/grantland/android-autofittextview
 *//* w ww  . j a va2s.c om*/
private 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:com.quran.labs.androidquran.widgets.SlidingTabLayout.java

private void populateTabStrip() {
    final PagerAdapter adapter = mViewPager.getAdapter();
    final View.OnClickListener tabClickListener = new TabClickListener();

    final DisplayMetrics metrics = getResources().getDisplayMetrics();
    final float fontSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP,
            metrics);// w  ww  . j  av  a  2 s  .  c om
    final TextPaint paint = new TextPaint();
    paint.setTextSize(fontSize);
    paint.setTypeface(Typeface.DEFAULT_BOLD);

    int targetWidth = 0;
    final int tabs = adapter.getCount();
    for (int i = 0; i < tabs; i++) {
        String str = adapter.getPageTitle(i).toString();
        str = str.toUpperCase(Locale.getDefault());

        int width = (int) paint.measureText(str);
        width = width + 2 * mTabPadding;
        if (width > targetWidth) {
            targetWidth = width;
        }
    }

    if (targetWidth * tabs < metrics.widthPixels) {
        targetWidth = metrics.widthPixels / tabs;
    }

    for (int i = 0; i < adapter.getCount(); i++) {
        View tabView = null;
        TextView tabTitleView = null;

        if (mTabViewLayoutId != 0) {
            // If there is a custom tab view layout id set, try and inflate it
            tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false);
            tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
        }

        if (tabView == null) {
            tabView = createDefaultTabView(getContext());
        }

        if (tabTitleView == null && TextView.class.isInstance(tabView)) {
            tabTitleView = (TextView) tabView;
        }

        if (tabTitleView != null) {
            tabTitleView.setText(adapter.getPageTitle(i));
        }
        tabView.setOnClickListener(tabClickListener);

        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(targetWidth,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        mTabStrip.addView(tabView, params);
    }
}