Example usage for android.graphics Paint measureText

List of usage examples for android.graphics Paint measureText

Introduction

In this page you can find the example usage for android.graphics Paint measureText.

Prototype

public float measureText(String text) 

Source Link

Document

Return the width of the text.

Usage

From source file:com.fanfou.app.opensource.ui.viewpager.TitlePageIndicator.java

/**
 * Calculate the bounds for a view's title
 * // w  w w  .j a  v  a 2s. c o m
 * @param index
 * @param paint
 * @return
 */
private RectF calcBounds(final int index, final Paint paint) {
    // Calculate the text bounds
    final RectF bounds = new RectF();
    bounds.right = paint.measureText(this.mTitleProvider.getTitle(index));
    bounds.bottom = paint.descent() - paint.ascent();
    return bounds;
}

From source file:org.mozilla.mozstumbler.client.mapview.MapFragment.java

private void initTextView(int textViewId, String bound) {
    TextView textView = (TextView) mRootView.findViewById(textViewId);
    Paint textPaint = textView.getPaint();
    int width = (int) Math.ceil(textPaint.measureText(bound));
    textView.setWidth(width);//from w w w.java 2 s.c  o m
    android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(width,
            android.widget.LinearLayout.LayoutParams.MATCH_PARENT);
    textView.setLayoutParams(params);
    textView.setText("0");
}

From source file:at.ac.uniklu.mobile.sportal.ui.viewpagerindicator.TitlePageIndicator.java

/**
 * Calculate the bounds for a view's title
 *
 * @param index/*  ww  w  .  ja v  a  2 s.c  o m*/
 * @param paint
 * @return
 */
private RectF calcBounds(int index, Paint paint) {
    //Calculate the text bounds
    RectF bounds = new RectF();
    bounds.right = paint.measureText(mTitleProvider.getTitle(index));
    bounds.bottom = paint.descent() - paint.ascent();
    return bounds;
}

From source file:com.android.common.TitlePageIndicator.java

/**
 * Calculate the bounds for a view's title
 *
 * @param index/* ww  w.j a v a 2 s  . com*/
 * @param paint
 * @return
 */
private RectF calcBounds(int index, Paint paint) {
    //Calculate the text bounds
    RectF bounds = new RectF();
    bounds.right = paint.measureText(mTitleProvider.GetTitle(index));
    bounds.bottom = paint.descent() - paint.ascent();
    return bounds;
}

From source file:com.mappn.gfan.ui.HomeTabActivity.java

private Bitmap drawText(DisplayMetrics dm, Resources res, Bitmap bm, int num) {
    final int height = bm.getScaledHeight(dm);
    final int width = bm.getScaledWidth(dm);
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawBitmap(bm, new Matrix(), new Paint());
    Paint textPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
    textPainter.setColor(res.getColor(R.color.tab_app_num));
    textPainter.setTextSize(dm.scaledDensity * 12);
    textPainter.setTypeface(Typeface.DEFAULT_BOLD);
    float textWidth = textPainter.measureText(String.valueOf(num)) / 2;
    canvas.drawText(String.valueOf(num), width / 2 - textWidth, height / 2 + (dm.scaledDensity * 6),
            textPainter);/*from   w  w  w  . j  av  a  2s .c  om*/
    canvas.save();
    return newBitmap;
}

From source file:com.rks.musicx.misc.utils.Helper.java

/**
 * Return text As bitmap//from  w w  w.  j  av a2 s.  c  om
 *
 * @param text
 * @param textSize
 * @param textColor
 * @return
 */
public static Bitmap textAsBitmap(String text, float textSize, int textColor) {
    Paint paint = new Paint(ANTI_ALIAS_FLAG);
    paint.setTextSize(textSize); //text size
    paint.setColor(textColor); //text color
    paint.setTextAlign(Paint.Align.LEFT); //align center
    float baseline = -paint.ascent(); // ascent() is negative
    int width = (int) (paint.measureText(text) + 0.0f); // round
    int height = (int) (baseline + paint.descent() + 0.0f);
    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    canvas.drawText(text, 0, baseline, paint); //draw text
    return image;
}

From source file:com.icloud.listenbook.base.view.viewpagerindicator.FixedTitlePageIndicator.java

/**
 * //from w  w w  .  j  a  v  a 2 s. co  m
 * 
 * @param index
 * @param paint
 * @return
 */
private RectF calcBounds(int index, Paint paint) {
    // Calculate the text bounds
    RectF bounds = new RectF();
    bounds.right = paint.measureText(mTitleProvider.getPageTitle(index).toString());
    bounds.bottom = paint.descent() - paint.ascent();
    return bounds;
}

From source file:com.viewpagerindicator.RichTitlePageIndicator.java

private void drawItem(Canvas canvas, int pos, Rect bound, Paint paint, boolean drawIcon) {
    String title = getTitle(pos);
    Bitmap icon = getIcon(pos);/*  ww w.  ja  v a  2 s  .c o  m*/
    // TODO: more flexible

    int textWidth = (int) paint.measureText(title);
    int iconWidth = 0, iconHeight = 0;
    if (icon != null) {
        iconWidth = icon.getWidth();
        iconHeight = icon.getHeight();
    }

    int offset = 0;
    if (drawIcon) {
        int boundH = bound.bottom - bound.top;
        float t = iconHeight;
        canvas.drawBitmap(icon, bound.left, mTopPadding, paint);
        offset = iconWidth;
    }
    canvas.drawText(title, bound.left + offset, bound.bottom + mTopPadding, paint);
}

From source file:com.viewpagerindicator.RichTitlePageIndicator.java

/**
 * Calculate the bounds for a view's title
 *
 * @param index//  w w w . j  a  v a2s .c om
 * @param paint
 * @return
 */
private Rect calcBounds(int index, Paint paint) {
    //Calculate the text bounds
    Rect bounds = new Rect();
    String title = getTitle(index);
    Bitmap icon = getIcon(index);

    int textWidth = (int) paint.measureText(title);
    int iconWidth = icon == null ? 0 : icon.getWidth();
    bounds.right = textWidth + iconWidth;

    int textHeight = (int) (paint.descent() - paint.ascent());
    bounds.bottom = textHeight;
    //int iconHeight = icon == null ? 0 : icon.getHeight();
    //bounds.bottom = Math.max(textHeight, iconHeight);
    return bounds;
}

From source file:jp.gr.java_conf.piropiroping.bluetoothcommander.MainActivity.java

private void convertBitmapCommand(String command, int start, int stop) {
    //???/*from w  w  w  . j ava2  s  .  co  m*/
    Paint objPaint = new Paint();
    Bitmap objBitmap;
    Canvas objCanvas;
    int textSize = 40;
    int textWidth = textSize * command.length(), textHeight = textSize;

    objPaint.setAntiAlias(true);
    objPaint.setColor(Color.BLUE);
    objPaint.setTextSize(textSize);
    Paint.FontMetrics fm = objPaint.getFontMetrics();
    objPaint.getTextBounds(command, 0, command.length(), new Rect(0, 0, textWidth, textHeight));

    //?
    textWidth = (int) objPaint.measureText(command);
    textHeight = (int) (Math.abs(fm.top) + fm.bottom);
    objBitmap = Bitmap.createBitmap(textWidth, textHeight, Bitmap.Config.ARGB_8888);

    //???
    objCanvas = new Canvas(objBitmap);
    objCanvas.drawText(command, 0, Math.abs(fm.top), objPaint);

    Editable editable = mOutEditText.getText();
    SpannableStringBuilder ssb = (SpannableStringBuilder) editable;
    ImageSpan is = new ImageSpan(getApplicationContext(), objBitmap);
    ssb.setSpan(is, start, stop + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}