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.googlecode.eyesfree.widget.RadialMenuView.java

private static String getEllipsizedText(Paint paint, String title, float maxWidth) {
    final float textWidth = paint.measureText(title);
    if (textWidth <= maxWidth) {
        return title;
    }/*from   w  w w . j a  v  a 2 s .  c  o  m*/

    // Find the maximum length with an ellipsis.
    final float ellipsisWidth = paint.measureText(ELLIPSIS);
    final int length = paint.breakText(title, true, (maxWidth - ellipsisWidth), null);

    // Try to land on a word break.
    // TODO(alanv): Use breaking iterator for better i18n support.
    final int space = title.lastIndexOf(' ', length);
    if (space > 0) {
        return title.substring(0, space) + ELLIPSIS;
    }

    // Otherwise, cut off characters.
    return title.substring(0, length) + ELLIPSIS;
}

From source file:com.android.mail.text.FolderSpan.java

private int getWidth(Paint p) {
    p.setTextSize(mRes.folderFontSize);/* w w  w  . jav  a 2 s . c o m*/
    return Math.min((int) p.measureText(mName) + 2 * mRes.folderHorizontalPadding, mDim.getMaxChipWidth());
}

From source file:com.appyvet.rangebar.PinView.java

private void calibrateTextSize(Paint paint, String text, float boxWidth) {
    paint.setTextSize(10);//from  w w w.j  a  v a2s  .  co m

    float textSize = paint.measureText(text);
    float estimatedFontSize = boxWidth * 8 / textSize / mDensity;

    if (estimatedFontSize < mMinPinFont) {
        estimatedFontSize = mMinPinFont;
    } else if (estimatedFontSize > mMaxPinFont) {
        estimatedFontSize = mMaxPinFont;
    }
    Log.d("pin", "size = " + estimatedFontSize * mDensity);
    paint.setTextSize(estimatedFontSize * mDensity);
}

From source file:airfree.xprojets.airfree.PinView.java

private void calibrateTextSize(Paint paint, String text, float boxWidth) {
    paint.setTextSize(10);//from   w w w  . jav a 2s  .c  o  m

    float textSize = paint.measureText(text);
    float estimatedFontSize = boxWidth * 8 / textSize / mDensity;

    if (estimatedFontSize < mMinPinFont) {
        estimatedFontSize = mMinPinFont;
    } else if (estimatedFontSize > mMaxPinFont) {
        estimatedFontSize = mMaxPinFont;
    }
    paint.setTextSize(estimatedFontSize * mDensity);
}

From source file:br.com.mauker.materialseekbar.PinView.java

private void calibrateTextSize(Paint paint, String text, float boxWidth) {
    paint.setTextSize(mMinPinFont);//from  w  w  w . ja v a 2 s  . com

    float textSize = paint.measureText(text);
    Log.d("pin", "textSize: " + textSize);
    float estimatedFontSize = boxWidth * 8 / textSize / mDensity;

    if (estimatedFontSize < mMinPinFont) {
        estimatedFontSize = mMinPinFont;
        Log.d("pin", "< mMinPinFont - Value: " + estimatedFontSize);
    } else if (estimatedFontSize > mMaxPinFont) {
        estimatedFontSize = mMaxPinFont;
        Log.d("pin", "> mMaxPinFont - Value: " + estimatedFontSize);
    }
    //        Log.d("pin","estimatedSize: " + estimatedFontSize);
    //        Log.d("pin","size: " + estimatedFontSize * mDensity);
    paint.setTextSize(estimatedFontSize * mDensity * 1.5f);
}

From source file:com.camnter.easyrecyclerviewsidebar.EasyRecyclerViewSidebar.java

/**
 * @param paint paint/*from  ww  w .  ja v a2  s.c  o  m*/
 * @param text text
 * @return float[] [0]=width [1]=height
 */
private float[] measureText(Paint paint, String text) {
    float[] property = new float[2];
    property[0] = paint.measureText(text);
    Paint.FontMetrics fontMetrics = paint.getFontMetrics();
    property[1] = fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading;
    return property;
}

From source file:com.pipit.agc.adapter.DayPickerAdapter.java

private void refitText(TextView txtv, int targetheight) {
    if (targetheight <= 0)
        return;/*from w w  w .j  ava2  s  .c  om*/
    Paint mTestPaint = new Paint();
    mTestPaint.set(txtv.getPaint());
    float hi = 100;
    float lo = 2;
    final float threshold = 0.5f; // How close we have to be

    while ((hi - lo) > threshold) {
        float size = (hi + lo) / 2;
        mTestPaint.setTextSize(size);
        if (mTestPaint.measureText((String) txtv.getText()) >= targetheight)
            hi = size; // too big
        else
            lo = size; // too small
    }
    // Use lo so that we undershoot rather than overshoot
    txtv.setTextSize(TypedValue.COMPLEX_UNIT_PX, lo);
}

From source file:fr.magistry.taigime.CandidateView.java

private float reduceTextSizeFromWidth(Paint p, String str, float maxWidth) {
    float size = p.getTextSize();
    if (str.length() == 0)
        return size;
    while (p.measureText(str) > maxWidth) {
        p.setTextSize(--size);/*from   ww w . j a va 2  s  .c o  m*/
    }
    return size;
}

From source file:com.longle1.facedetection.MainActivity.java

@Override
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(Color.GREEN);/* w w  w  . java 2s . c  om*/
    paint.setTextSize(20);

    String s = "FacePreview - This side up.";
    float textWidth = paint.measureText(s);
    canvas.drawText(s, (getWidth() - textWidth) / 2, 20, paint);

    if (faces != null) {
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);
        float scaleX = (float) getWidth() / grayImage.width();
        float scaleY = (float) getHeight() / grayImage.height();
        int total = faces.total();
        for (int i = 0; i < total; i++) {
            CvRect r = new CvRect(cvGetSeqElem(faces, i));
            int x = r.x(), y = r.y(), w = r.width(), h = r.height();
            canvas.drawRect(x * scaleX, y * scaleY, (x + w) * scaleX, (y + h) * scaleY, paint);
        }
    }
}

From source file:ch.carteggio.ui.ConversationIconLoader.java

/**
 * Calculates a bitmap with a color and a capital letter for contacts without picture.
 */// w  ww . j av  a  2  s.  c om
private Bitmap calculateFallbackBitmap(String emails[]) {
    Bitmap result = Bitmap.createBitmap(mPictureSizeInPx, mPictureSizeInPx, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(result);

    int rgb = CONTACT_DUMMY_COLORS_ARGB[0];

    if (emails.length > 0) {
        calcUnknownContactColor(emails[0]);
    }

    result.eraseColor(rgb);

    String letter = FALLBACK_CONTACT_LETTER;

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    paint.setARGB(255, 255, 255, 255);
    paint.setTextSize(mPictureSizeInPx * 3 / 4); // just scale this down a bit
    Rect rect = new Rect();
    paint.getTextBounds(letter, 0, 1, rect);
    float width = paint.measureText(letter);
    canvas.drawText(letter, (mPictureSizeInPx / 2f) - (width / 2f),
            (mPictureSizeInPx / 2f) + (rect.height() / 2f), paint);

    return result;
}