Java Code Examples for android.graphics.Paint.getFontMetrics()

The following are Jave code examples for showing how to use getFontMetrics() of the android.graphics.Paint class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: EmoticonGIFKeyboard   File: EmoticonSpan.java   View Source Code Vote up 6 votes
@Override
public int getSize(final Paint paint, final CharSequence text, final int start,
                   final int end, final Paint.FontMetricsInt fontMetrics) {
    if (fontMetrics != null) {
        final Paint.FontMetrics paintFontMetrics = paint.getFontMetrics();
        final float fontHeight = paintFontMetrics.descent - paintFontMetrics.ascent;
        final float centerY = paintFontMetrics.ascent + fontHeight / 2;

        fontMetrics.ascent = (int) (centerY - mEmoticonSize / 2);
        fontMetrics.top = fontMetrics.ascent;
        fontMetrics.bottom = (int) (centerY + mEmoticonSize / 2);
        fontMetrics.descent = fontMetrics.bottom;
    }

    return (int) mEmoticonSize;
}
 
Example 2
Project: GifEmoji   File: EmoticonSpan.java   View Source Code Vote up 6 votes
@Override
public void draw(final Canvas canvas, final CharSequence text, final int start,
                 final int end, final float x, final int top, final int y,
                 final int bottom, final Paint paint) {
    final Drawable drawable = getDrawable();
    if (drawable == null) return;
    final Paint.FontMetrics paintFontMetrics = paint.getFontMetrics();
    final float fontHeight = paintFontMetrics.descent - paintFontMetrics.ascent;
    final float centerY = y + paintFontMetrics.descent - fontHeight / 2;
    final float transitionY = centerY - textSize / 2;

    canvas.save();
    canvas.translate(x, transitionY);
    drawable.draw(canvas);
    canvas.restore();
}
 
Example 3
Project: GifEmoticon   File: EmoticonSpan.java   View Source Code Vote up 6 votes
@Override
public int getSize(final Paint paint, final CharSequence text, final int start,
                   final int end, final Paint.FontMetricsInt fontMetrics) {
    if (fontMetrics != null) {
        final Paint.FontMetrics paintFontMetrics = paint.getFontMetrics();
        final float fontHeight = paintFontMetrics.descent - paintFontMetrics.ascent;
        final float centerY = paintFontMetrics.ascent + fontHeight / 2;

        fontMetrics.ascent = (int) (centerY - textSize / 2);
        fontMetrics.top = fontMetrics.ascent;
        fontMetrics.bottom = (int) (centerY + textSize / 2);
        fontMetrics.descent = fontMetrics.bottom;
    }

    return textSize;
}
 
Example 4
Project: AndroidUtilCode-master   File: SpanUtils.java   View Source Code Vote up 6 votes
@Override
public int getSize(@NonNull Paint paint, CharSequence text,
                   int start, int end,
                   Paint.FontMetricsInt fm) {
    Drawable d = getCachedDrawable();
    Rect rect = d.getBounds();
    final int fontHeight = (int) (paint.getFontMetrics().descent - paint.getFontMetrics().ascent);
    if (fm != null) { // this is the fucking code which I waste 3 days
        if (rect.height() > fontHeight) {
            if (mVerticalAlignment == ALIGN_TOP) {
                fm.descent += rect.height() - fontHeight;
            } else if (mVerticalAlignment == ALIGN_CENTER) {
                fm.ascent -= (rect.height() - fontHeight) / 2;
                fm.descent += (rect.height() - fontHeight) / 2;
            } else {
                fm.ascent -= rect.height() - fontHeight;
            }
        }
    }
    return rect.right;
}
 
Example 5
Project: sealtalk-android-master   File: RongGenerate.java   View Source Code Vote up 5 votes
public static String generateDefaultAvatar(String username, String userid) {
    String s = null;
    if (!TextUtils.isEmpty(username)) {
        s = String.valueOf(username.charAt(0));
    }
    if (s == null) {
        s = "A";
    }
    String color = getColorRGB(userid);
    String string = getAllFirstLetter(username);
    createDir(SAVEADDRESS);
    File f = new File(SAVEADDRESS, string + "_" + userid);
    if (f.exists()) {
        return SCHEMA + f.getPath();
    }
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    paint.setTextSize(220);
    paint.setAntiAlias(true);
    int width = 480;
    int height = 480;
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(Color.parseColor(color));
    Rect rect = new Rect();
    paint.getTextBounds(s, 0, s.length(), rect);
    Paint.FontMetrics fm = paint.getFontMetrics();
    int textLeft = (int) ((width - paint.measureText(s)) / 2);
    int textTop = (int) (height - width / 2 + Math.abs(fm.ascent) / 2 - 25);
    canvas.drawText(s, textLeft, textTop, paint);
    return saveBitmap(bitmap, string + "_" + userid);
}
 
Example 6
Project: rongyunDemo   File: RongGenerate.java   View Source Code Vote up 5 votes
public static String generateDefaultAvatar(String username, String userid) {

        String s = null;
        if (!TextUtils.isEmpty(username)) {
            s = String.valueOf(username.charAt(0));
        }
        if (s == null) {
            s = "A";
        }
        String color = getColorRGB(userid);
        String string = getAllFirstLetter(username);
        createDir(SAVEADDRESS);
        File f = new File(SAVEADDRESS, string + "_" + userid);
        if (f.exists()) {
            return SCHEMA + f.getPath();
        }
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(220);
        paint.setAntiAlias(true);
        int width = 480;
        int height = 480;
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.parseColor(color));
        Rect rect = new Rect();
        paint.getTextBounds(s, 0, s.length(), rect);
        Paint.FontMetrics fm = paint.getFontMetrics();
        int textLeft = (int) ((width - paint.measureText(s)) / 2);
        int textTop = (int) (height - width / 2 + Math.abs(fm.ascent) / 2 - 25);
        canvas.drawText(s, textLeft, textTop, paint);
        return saveBitmap(bitmap, string + "_" + userid);
    }
 
Example 7
Project: RetroMusicPlayer   File: BottomNavigationViewEx.java   View Source Code Vote up 5 votes
/**
 * get text height by font size
 *
 * @param fontSize
 * @return
 */
private static int getFontHeight(float fontSize) {
    Paint paint = new Paint();
    paint.setTextSize(fontSize);
    Paint.FontMetrics fm = paint.getFontMetrics();
    return (int) Math.ceil(fm.descent - fm.top) + 2;
}
 
Example 8
Project: live_master   File: SimpleTextCacheStuffer.java   View Source Code Vote up 5 votes
protected Float getCacheHeight(BaseDanmaku danmaku, Paint paint) {
    Float textSize = paint.getTextSize();
    Float textHeight = sTextHeightCache.get(textSize);
    if (textHeight == null) {
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        textHeight = fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading;
        sTextHeightCache.put(textSize, textHeight);
    }
    return textHeight;
}
 
Example 9
Project: LaunchEnr   File: Utilities.java   View Source Code Vote up 5 votes
/**
 * Calculates the height of a given string at a specific text size.
 */
static int calculateTextHeight(float textSizePx) {
    Paint p = new Paint();
    p.setTextSize(textSizePx);
    Paint.FontMetrics fm = p.getFontMetrics();
    return (int) Math.ceil(fm.bottom - fm.top);
}
 
Example 10
Project: android-project-gallery   File: ViewUtils.java   View Source Code Vote up 5 votes
/**
 * 返回字体高度
 * 
 * @param textSize
 * @return
 */
public static int getFontHeight(float textSize)
{
	Paint paint = new Paint();
	paint.setTextSize(textSize);
	FontMetrics fm = paint.getFontMetrics();

	return (int) Math.ceil(fm.descent - fm.top) + 2;
}
 
Example 11
Project: Hotspot-master-devp   File: SliderView.java   View Source Code Vote up 5 votes
/**
 * 获取字体的高度
 * @param fontSize
 * @return
 */
private int getFontHeight(float fontSize){  
     Paint paint = new Paint();  
     paint.setTextSize(fontSize);  
     FontMetrics fm = paint.getFontMetrics();  
    return (int) Math.ceil(fm.descent - fm.top) + 2;  
}
 
Example 12
Project: android-project-gallery   File: SingleLineEditText.java   View Source Code Vote up 5 votes
/**
 * 返回字体高度
 * 
 * @param textSize
 * @return
 */
public int getFontHeight(float textSize)
{
	Paint paint = new Paint();
	paint.setTextSize(textSize);
	FontMetrics fm = paint.getFontMetrics();
	
	return (int) Math.ceil(fm.descent - fm.top) + 2;
}
 
Example 13
Project: GitHub   File: Utils.java   View Source Code Vote up 4 votes
public static float getLineHeight(Paint paint, Paint.FontMetrics fontMetrics){
    paint.getFontMetrics(fontMetrics);
    return fontMetrics.descent - fontMetrics.ascent;
}
 
Example 14
Project: GitHub   File: Utils.java   View Source Code Vote up 4 votes
public static float getLineSpacing(Paint paint, Paint.FontMetrics fontMetrics){
    paint.getFontMetrics(fontMetrics);
    return fontMetrics.ascent - fontMetrics.top + fontMetrics.bottom;
}
 
Example 15
Project: PaoMovie   File: RangeSeekBar.java   View Source Code Vote up 4 votes
/**
 * @return 返回指定笔的文字高度
 */
private float getFontHeight(Paint paint) {
	FontMetrics fm = paint.getFontMetrics();
	return fm.descent - fm.ascent;
}
 
Example 16
Project: codedemos   File: ScreenUtil.java   View Source Code Vote up 4 votes
public static double getTextHeight(float frontSize) {
    Paint paint = new Paint();
    paint.setTextSize(frontSize);
    Paint.FontMetrics fm = paint.getFontMetrics();
    return Math.ceil(fm.descent - fm.top) + 2;
}
 
Example 17
Project: VirtualHook   File: BaseView.java   View Source Code Vote up 4 votes
public float getFontHeight(Paint paint) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    return fm.descent - fm.ascent;
}
 
Example 18
Project: boohee_v5.6   File: ViewUtils.java   View Source Code Vote up 4 votes
public static int getTextHeight(Paint paint) {
    FontMetrics fm = paint.getFontMetrics();
    return (int) Math.ceil((double) (fm.descent - fm.ascent));
}
 
Example 19
Project: MaterialDesignDemo   File: MyDecorationThree.java   View Source Code Vote up 4 votes
/**
 * 返回指定笔的文字高度
 */
private float getFontHeight(Paint paint) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    return fm.descent - fm.ascent;
}
 
Example 20
Project: WeChatPwd   File: Util.java   View Source Code Vote up 2 votes
/**
 * @param backgroundTop
 * @param backgroundBottom
 * @param paint
 * @return paint绘制居中文字时,获取文本底部坐标
 */
public static float getTextBaseLine(float backgroundTop, float backgroundBottom, Paint paint) {
    final Paint.FontMetrics fontMetrics = paint.getFontMetrics();
    return (backgroundTop + backgroundBottom - fontMetrics.bottom - fontMetrics.top) / 2;
}