Example usage for android.graphics Paint getFontMetricsInt

List of usage examples for android.graphics Paint getFontMetricsInt

Introduction

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

Prototype

public FontMetricsInt getFontMetricsInt() 

Source Link

Usage

From source file:android.support.v17.leanback.app.ErrorSupportFragment.java

private static FontMetricsInt getFontMetricsInt(TextView textView) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(textView.getTextSize());
    paint.setTypeface(textView.getTypeface());
    return paint.getFontMetricsInt();
}

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

private static TextProperty computeTextProperty(String content, Paint paint, int maxWidth, int maxHeight) {
    FontMetricsInt fm = paint.getFontMetricsInt();
    int h = (int) Math.ceil(fm.bottom - fm.top);
    int maxContentWidth = 0;

    String[] lines = splitString(content, maxHeight, maxWidth, paint);

    if (maxWidth != 0) {
        maxContentWidth = maxWidth;/*from w  w w .  j  a va 2  s  . co  m*/
    } else {
        /*
          * Compute the max width
          */
        int temp = 0;
        for (String line : lines) {
            temp = (int) Math.ceil(paint.measureText(line, 0, line.length()));
            if (temp > maxContentWidth) {
                maxContentWidth = temp;
            }
        }
    }

    return new TextProperty(maxContentWidth, h, lines);
}

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

private static String[] splitString(String content, int maxHeight, int maxWidth, Paint paint) {
    String[] lines = content.split("\\n");
    String[] ret = null;/*  w w w  .j a  v a 2 s  .  c  o  m*/
    FontMetricsInt fm = paint.getFontMetricsInt();
    int heightPerLine = (int) Math.ceil(fm.bottom - fm.top);
    int maxLines = maxHeight / heightPerLine;

    if (maxWidth != 0) {
        LinkedList<String> strList = new LinkedList<String>();
        for (String line : lines) {
            /*
             * The width of line is exceed maxWidth, should divide it into
             * two or more lines.
             */
            int lineWidth = (int) Math.ceil(paint.measureText(line));
            if (lineWidth > maxWidth) {
                strList.addAll(divideStringWithMaxWidth(paint, line, maxWidth));
            } else {
                strList.add(line);
            }

            /*
             * Should not exceed the max height;
             */
            if (maxLines > 0 && strList.size() >= maxLines) {
                break;
            }
        }

        /*
         * Remove exceeding lines
         */
        if (maxLines > 0 && strList.size() > maxLines) {
            while (strList.size() > maxLines) {
                strList.removeLast();
            }
        }

        ret = new String[strList.size()];
        strList.toArray(ret);
    } else if (maxHeight != 0 && lines.length > maxLines) {
        /*
         * Remove exceeding lines
         */
        LinkedList<String> strList = new LinkedList<String>();
        for (int i = 0; i < maxLines; i++) {
            strList.add(lines[i]);
        }
        ret = new String[strList.size()];
        strList.toArray(ret);
    } else {
        ret = lines;
    }

    return ret;
}

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

public static void createTextBitmap(String content, String fontName, int fontSize, int alignment, int width,
        int height) {

    content = refactorString(content);//from  www.  j  a v  a 2 s. c om
    Paint paint = newPaint(fontName, fontSize, alignment);

    TextProperty textProperty = computeTextProperty(content, paint, width, height);

    int bitmapTotalHeight = (height == 0 ? textProperty.totalHeight : height);

    // Draw text to bitmap
    Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, bitmapTotalHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    // Draw string
    FontMetricsInt fm = paint.getFontMetricsInt();
    int x = 0;
    int y = computeY(fm, height, textProperty.totalHeight, alignment);
    String[] lines = textProperty.lines;
    for (String line : lines) {
        x = computeX(paint, line, textProperty.maxWidth, alignment);
        canvas.drawText(line, x, y, paint);
        y += textProperty.heightPerLine;
    }

    initNativeObject(bitmap);
}

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

private int getHeight(Paint p) {
    p.setTextSize(mRes.folderFontSize);/*  ww  w.  j  ava 2  s  . c  o  m*/
    final Paint.FontMetricsInt fm = p.getFontMetricsInt();
    return fm.bottom - fm.top;
}

From source file:es.usc.citius.servando.calendula.activities.CalendarActivity.java

public CharSequence addPickupList(CharSequence msg, List<PickupInfo> pks) {

    Paint textPaint = new Paint();
    //obviously, we have to set textSize into Paint object
    textPaint.setTextSize(getResources().getDimensionPixelOffset(R.dimen.medium_font_size));
    Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();

    for (PickupInfo p : pks) {
        Patient patient = pickupUtils.getPatient(p);
        int color = patient.color();
        String str = "       " + p.medicine().name() + " (" + dtf2.format(p.from().toDate()) + " - "
                + dtf2.format(p.to().toDate()) + ")\n";
        Spannable text = new SpannableString(str);
        text.setSpan(new ForegroundColorSpan(color), 0, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        Drawable d = getResources().getDrawable(AvatarMgr.res(patient.avatar()));
        d.setBounds(0, 0, fontMetrics.bottom, fontMetrics.bottom);
        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        text.setSpan(span, 0, 5, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        msg = TextUtils.concat(msg, text);
    }//from w w  w .ja v a  2 s. c om
    return msg;
}