Example usage for android.graphics Paint getFontMetrics

List of usage examples for android.graphics Paint getFontMetrics

Introduction

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

Prototype

public FontMetrics getFontMetrics() 

Source Link

Document

Allocates a new FontMetrics object, and then calls getFontMetrics(fm) with it, returning the object.

Usage

From source file:Main.java

public static float getYOfDrawText(Paint p, float centerY) {
    FontMetrics metrics = p.getFontMetrics();
    return centerY - (metrics.top + (metrics.bottom - metrics.top) / 2);
}

From source file:Main.java

/**
 *
 * @param paint//from ww  w .j  a  v a  2 s  . c o  m
 * @param cellHeight
 * @return
 */
public static float getTextFitY(Paint paint, float cellHeight) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    float textHeight = (float) (Math.ceil(fm.descent - fm.ascent) + 2f);
    return (cellHeight + textHeight) / 2 - fm.bottom;
}

From source file:Main.java

public static int getFontHeight(float fontSize) {
    Paint paint = new Paint();
    paint.setTextSize(fontSize);//w w w . j  a  v a  2  s  .  c  o m
    Paint.FontMetrics fm = paint.getFontMetrics();
    return (int) Math.ceil(fm.descent - fm.top) + 2;
}

From source file:Main.java

private static int getFontHeight(TextView tv) {
    Paint paint = new Paint();
    paint.setTextSize(tv.getTextSize());
    Paint.FontMetrics fm = paint.getFontMetrics();
    return (int) Math.ceil(fm.bottom - fm.top);
}

From source file:Main.java

public static int getFontHeight(TextView textView) {
    Paint paint = new Paint();
    paint.setTextSize(textView.getTextSize());
    Paint.FontMetrics fm = paint.getFontMetrics();
    return (int) Math.ceil(fm.bottom - fm.top);
}

From source file:Main.java

public static float calcYWhenTextAlignCenter(int aCanvasHeight, Paint aPaint) {
    if (aPaint == null) {
        return 0;
    }//from  ww  w.  j a  v a  2s .c  o m

    final float fontHeight = aPaint.getFontMetrics().bottom - aPaint.getFontMetrics().top;
    return ((aCanvasHeight - fontHeight) / 2 - aPaint.getFontMetrics().top);
}

From source file:Main.java

public static int measureTextHeight(int textsize) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(textsize);//from ww w  .  j  a va2 s  .c om
    Paint.FontMetrics metrics = paint.getFontMetrics();
    return (int) Math.ceil(metrics.descent - metrics.ascent);
}

From source file:info.papdt.blacklight.support.Utility.java

public static int getFontHeight(Context context, float fontSize) {
    // Convert Dp To Px
    float px = context.getResources().getDisplayMetrics().density * fontSize + 0.5f;

    // Use Paint to get font height
    Paint p = new Paint();
    p.setTextSize(px);//from w ww  .  j  av a  2 s .c o  m
    FontMetrics fm = p.getFontMetrics();
    return (int) Math.ceil(fm.descent - fm.ascent);
}

From source file:com.atwal.wakeup.battery.util.Utilities.java

/**
 * Calculates the height of a given string at a specific text size.
 *//*from   ww w .j  a  v a  2 s . c o  m*/
public static float calculateTextHeight(float textSizePx) {
    Paint p = new Paint();
    p.setTextSize(textSizePx);
    Paint.FontMetrics fm = p.getFontMetrics();
    return -fm.top + fm.bottom;
}

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

/**
 * @param paint paint//from w ww  .j av  a2s.  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;
}