Java Code Examples for android.graphics.Paint.FontMetrics

The following are Jave code examples for showing how to use FontMetrics 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: ContactsList   File: FloatingBarItemDecoration.java   View Source Code Vote up 6 votes
public FloatingBarItemDecoration(Context context, Map<Integer, String> list) {
    this.mContext = context;
    Resources resources = mContext.getResources();
    this.mList = list;
    this.mTitleHeight = resources.getDimensionPixelSize(R.dimen.item_decoration_title_height);

    mBackgroundPaint = new Paint();
    mBackgroundPaint.setColor(ContextCompat.getColor(mContext, R.color.item_decoration_title_background));

    mTextPaint = new Paint();
    mTextPaint.setColor(ContextCompat.getColor(mContext, R.color.item_decoration_title_fontcolor));
    mTextPaint.setTextSize(mContext.getResources().getDimensionPixelSize(R.dimen.item_decoration_title_fontsize));

    Paint.FontMetrics fm = mTextPaint.getFontMetrics();
    mTextHeight = (int) (fm.bottom - fm.top);
    mTextBaselineOffset = (int) fm.bottom;
    mTextStartMargin = resources.getDimensionPixelOffset(R.dimen.item_decoration_title_start_margin);
}
 
Example 2
Project: CalendarView_master   File: MeiZuCalendarCardView.java   View Source Code Vote up 6 votes
public MeiZuCalendarCardView(Context context) {
    super(context);

    mTextPaint.setTextSize(dipToPx(context, 8));
    mTextPaint.setColor(0xff111111);
    mTextPaint.setAntiAlias(true);
    mTextPaint.setFakeBoldText(true);

    mSchemeBasicPaint.setAntiAlias(true);
    mSchemeBasicPaint.setStyle(Paint.Style.FILL);
    mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
    mSchemeBasicPaint.setColor(0xffed5353);
    mSchemeBasicPaint.setFakeBoldText(true);
    mRadio = dipToPx(getContext(), 7);
    mPadding = dipToPx(getContext(), 4);
    Paint.FontMetrics metrics = mSchemeBasicPaint.getFontMetrics();
    mSchemeBaseLine = mRadio - metrics.descent + (metrics.bottom - metrics.top) / 2 + dipToPx(getContext(), 1);

}
 
Example 3
Project: Tribe   File: SelectRegoinDecoration.java   View Source Code Vote up 6 votes
public SelectRegoinDecoration(Context context, DecorationCallback decorationCallback) {
    Resources res = context.getResources();
    this.callback = decorationCallback;

    paint = new Paint();
    paint.setColor(res.getColor(R.color.regoin_line));
    textPaint = new TextPaint();
    textPaint.setTypeface(Typeface.DEFAULT);
    textPaint.setAntiAlias(true);
    textPaint.setTextSize(30);
    textPaint.setColor(Color.BLACK);
    textPaint.getFontMetrics(fontMetrics);
    textPaint.setTextAlign(Paint.Align.LEFT);
    fontMetrics = new Paint.FontMetrics();
    //高度
    topGap = res.getDimensionPixelSize(R.dimen.sectioned_top);

}
 
Example 4
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 5
Project: FlickLauncher   File: Utilities.java   View Source Code Vote up 5 votes
/**
 * Calculates the height of a given string at a specific text size.
 */
public 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 6
Project: XERUNG   File: MaterialEditText.java   View Source Code Vote up 5 votes
private void initPadding() {
  extraPaddingTop = floatingLabelEnabled ? floatingLabelTextSize + floatingLabelPadding : floatingLabelPadding;
  textPaint.setTextSize(bottomTextSize);
  Paint.FontMetrics textMetrics = textPaint.getFontMetrics();
  extraPaddingBottom = (int) ((textMetrics.descent - textMetrics.ascent) * currentBottomLines) + (hideUnderline ? bottomSpacing : bottomSpacing * 2);
  extraPaddingLeft = iconLeftBitmaps == null ? 0 : (iconOuterWidth + iconPadding);
  extraPaddingRight = iconRightBitmaps == null ? 0 : (iconOuterWidth + iconPadding);
  correctPaddings();
}
 
Example 7
Project: Cashew   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: nativehtml   File: AndroidContainerElement.java   View Source Code Vote up 5 votes
@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int listIndex = 1;
    CssEnum listStyleType = getComputedStyle().getEnum(CssProperty.LIST_STYLE_TYPE);
    float scale = getOwnerDocument().getSettings().getScale();
    Paint.FontMetrics bulletMetrics = null;

    for (int i = 0; i < getChildCount(); i++) {
        View component = getChildAt(i);
        Element element = (Element) component;
        CssStyleDeclaration childStyle = element.getComputedStyle();

        if (childStyle.getEnum(CssProperty.DISPLAY) == CssEnum.LIST_ITEM
                && listStyleType != CssEnum.NONE) {
            if (bulletMetrics == null) {
                bulletMetrics = new Paint.FontMetrics();
            }
            String bullet = Strings.getBullet(listStyleType, listIndex++);
            AndroidCss.setTextPaint(childStyle, scale, paint);
            paint.getFontMetrics(bulletMetrics);

            float top = childStyle.getPx(CssProperty.BORDER_TOP_WIDTH, 0)
                    + childStyle.getPx(CssProperty.PADDING_TOP, 0);
            float left = childStyle.getPx(CssProperty.BORDER_LEFT_WIDTH, 0)
                    + childStyle.getPx(CssProperty.PADDING_LEFT, 0);

            canvas.drawText(
                    bullet,
                    component.getX() + left * scale - paint.measureText(bullet),
                    component.getY() + top * scale - bulletMetrics.top,
                    paint);
        }
    }
}
 
Example 9
Project: AndroidTvDemo   File: CountDownProgress.java   View Source Code Vote up 5 votes
private void measureView()
{
    float textWidth = textPaint.measureText(text.toString());
    Paint.FontMetrics metrics = textPaint.getFontMetrics();
    float textHeight = metrics.bottom - metrics.top;
    circleRadius =
        (int)Math.round((Math.sqrt(textWidth * textWidth + textHeight * textHeight) + circleStrokeWidth) / 2.0f);
    width = getPaddingLeft() + circleRadius * 2 + circleStrokeWidth + getPaddingRight();
    height = getPaddingTop() + circleRadius * 2 + circleStrokeWidth + getPaddingBottom();
}
 
Example 10
Project: Tesseract-OCR-Scanner   File: ScannerFinderView.java   View Source Code Vote up 5 votes
private void drawText(Canvas canvas, Rect rect) {
    int margin = 40;
    mPaint.setColor(mTextColor);
    mPaint.setTextSize(getResources().getDimension(R.dimen.text_size_13sp));
    String text = getResources().getString(R.string.auto_scan_notification);
    Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
    float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
    float offY = fontTotalHeight / 2 - fontMetrics.bottom;
    float newY = rect.bottom + margin + offY;
    float left = (ScreenUtils.getScreenWidth() - mPaint.getTextSize() * text.length()) / 2;
    canvas.drawText(text, left, newY, mPaint);
}
 
Example 11
Project: GitHub   File: SimpleMonthView.java   View Source Code Vote up 5 votes
/**
 * 在使用drawText方法时文字不能根据y坐标居中,所以重新计算y坐标
 * @param paint
 * @param y
 * @return
 */
private float getTextYCenter(Paint paint, int y) {
    Paint.FontMetrics fontMetrics = paint.getFontMetrics();
    float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
    float offY = fontTotalHeight / 2 - fontMetrics.bottom;
    return y + offY;
}
 
Example 12
Project: GitHub   File: CustomViewWithTypefaceSupport.java   View Source Code Vote up 5 votes
@Override
protected void onDraw(Canvas canvas) {
    String text = "This is a custom view with setTypeface support";
    Paint.FontMetrics fm = paint.getFontMetrics();
    paint.getTextBounds(text, 0, text.length(), textBounds);

    width = textBounds.left + textBounds.right + getPaddingLeft() + getPaddingRight();
    height = (int) (Math.abs(fm.top) + fm.bottom);

    canvas.drawText(text, 0, -fm.top + getPaddingTop(), paint);
}
 
Example 13
Project: MaterialDesignDemo   File: MyDecorationThree.java   View Source Code Vote up 4 votes
/**
 * 返回指定笔离文字顶部的基准距离
 */
private float getFontLeading(Paint paint) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    return fm.leading - fm.ascent;
}
 
Example 14
Project: Hotspot-master-devp   File: AlignTextView.java   View Source Code Vote up 4 votes
@Override
protected void onDraw(Canvas canvas) {
    TextPaint paint = getPaint();
    paint.setColor(getCurrentTextColor());
    paint.drawableState = getDrawableState();

    width = getMeasuredWidth();

    Paint.FontMetrics fm = paint.getFontMetrics();
    float firstHeight = getTextSize() - (fm.bottom - fm.descent + fm.ascent - fm.top);

    int gravity = getGravity();
    if ((gravity & 0x1000) == 0) { // 是否垂直居中
        firstHeight = firstHeight + (textHeight - firstHeight) / 2;
    }

    int paddingTop = getPaddingTop();
    int paddingLeft = getPaddingLeft();
    int paddingRight = getPaddingRight();
    width = width - paddingLeft - paddingRight;

    for (int i = 0; i < lines.size(); i++) {
        float drawY = i * textHeight + firstHeight;
        String line = lines.get(i);
        // 绘画起始x坐标
        float drawSpacingX = paddingLeft;
        float gap = (width - paint.measureText(line));
        float interval = gap / (line.length() - 1);

        // 绘制最后一行
        if (tailLines.contains(i)) {
            interval = 0;
            if (align == Align.ALIGN_CENTER) {
                drawSpacingX += gap / 2;
            } else if (align == Align.ALIGN_RIGHT) {
                drawSpacingX += gap;
            }
        }

        for (int j = 0; j < line.length(); j++) {
            float drawX = paint.measureText(line.substring(0, j)) + interval * j;
            canvas.drawText(line.substring(j, j + 1), drawX + drawSpacingX, drawY +
                    paddingTop + textLineSpaceExtra * i, paint);
        }
    }
}
 
Example 15
Project: KrGallery   File: CheckBox.java   View Source Code Vote up 4 votes
@Override
protected void onDraw(Canvas canvas) {
    if (getVisibility() != VISIBLE) {
        return;
    }
    if (drawBackground || progress != 0) {
        eraser2.setStrokeWidth(AndroidUtilities.dp(size + 6));

        drawBitmap.eraseColor(0);
        float rad = getMeasuredWidth() / 2;

        float roundProgress = progress >= 0.5f ? 1.0f : progress / 0.5f;
        float checkProgress = progress < 0.5f ? 0.0f : (progress - 0.5f) / 0.5f;

        float roundProgressCheckState = isCheckAnimation ? progress : (1.0f - progress);
        if (roundProgressCheckState < progressBounceDiff) {
            rad -= AndroidUtilities.dp(2) * roundProgressCheckState / progressBounceDiff;
        } else if (roundProgressCheckState < progressBounceDiff * 2) {
            rad -= AndroidUtilities.dp(2) - AndroidUtilities.dp(2)
                    * (roundProgressCheckState - progressBounceDiff) / progressBounceDiff;
        }
        if (drawBackground) {
            paint.setColor(0x44000000);
            canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2,
                    rad - AndroidUtilities.dp(1), paint);
            canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2,
                    rad - AndroidUtilities.dp(1), backgroundPaint);
        }

        paint.setColor(color);

        bitmapCanvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, rad, paint);
        bitmapCanvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2,
                rad * (1 - roundProgress), eraser);
        canvas.drawBitmap(drawBitmap, 0, 0, null);

        // 选中时显示√
        // checkBitmap.eraseColor(0);
        // int w = checkDrawable.getIntrinsicWidth();
        // int h = checkDrawable.getIntrinsicHeight();
        // int x = (getMeasuredWidth() - w) / 2;
        // int y = (getMeasuredHeight() - h) / 2;
        //
        // checkDrawable.setBounds(x, y + checkOffset, x + w, y + h + checkOffset);
        // checkDrawable.draw(checkCanvas);
        // checkCanvas.drawCircle(getMeasuredWidth() / 2 - AndroidUtilities.dp(2.5f),
        // getMeasuredHeight() / 2 + AndroidUtilities.dp(4), ((getMeasuredWidth() +
        // AndroidUtilities.dp(6)) / 2) * (1 - checkProgress), eraser2);
        //
        // canvas.drawBitmap(checkBitmap, 0, 0, null);

        // 选中时显示数字
        if (isChecked && number != -1) {
            int w = getMeasuredWidth() / 2;
            int h = getMeasuredHeight();
            textPaint.setTextSize(w);

            Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
            // 计算文字高度
            float fontHeight = fontMetrics.bottom - fontMetrics.top;
            // 计算文字baseline
            float baseY = h - (h - fontHeight) / 2 - fontMetrics.bottom;
            canvas.drawText(String.valueOf(number), w, baseY, textPaint);
        }
    }
}
 
Example 16
Project: GitHub   File: EmoticonsKeyboardUtils.java   View Source Code Vote up 4 votes
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);
}
 
Example 17
Project: topnews   File: ClipPagerTitleView.java   View Source Code Vote up 4 votes
@Override
public int getContentBottom() {
    Paint.FontMetrics metrics = mPaint.getFontMetrics();
    float contentHeight = metrics.bottom - metrics.top;
    return (int) (getHeight() / 2 + contentHeight / 2);
}
 
Example 18
Project: GitHub   File: ClipPagerTitleView.java   View Source Code Vote up 4 votes
@Override
public int getContentBottom() {
    Paint.FontMetrics metrics = mPaint.getFontMetrics();
    float contentHeight = metrics.bottom - metrics.top;
    return (int) (getHeight() / 2 + contentHeight / 2);
}
 
Example 19
Project: topnews   File: ClipPagerTitleView.java   View Source Code Vote up 4 votes
@Override
public int getContentTop() {
    Paint.FontMetrics metrics = mPaint.getFontMetrics();
    float contentHeight = metrics.bottom - metrics.top;
    return (int) (getHeight() / 2 - contentHeight / 2);
}
 
Example 20
Project: RandomCodeView   File: RandomCodeView.java   View Source Code Vote up 2 votes
/**
 * getRandomY 获取文字Y坐标位置,y为文字baseline坐标
 * 从 ascent到height-descent之间随机
 *
 * @param fontMetrics
 * @param height
 * @return float
 * @api 6
 * @since 3.1.0
 */
private float getRandomY(Paint.FontMetrics fontMetrics, int height) {

    int min = (int) (height - Math.abs(fontMetrics.ascent) - fontMetrics.descent);
    return mRandom.nextInt(min) + Math.abs(fontMetrics.ascent);
}