Java Code Examples for android.text.TextPaint.getFontMetrics()

The following are Jave code examples for showing how to use getFontMetrics() of the android.text.TextPaint class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: mongol-library   File: MongolTextLine.java   Source Code and License Vote up 6 votes
TextRun(int offset, int length, boolean isRotated, boolean isSpanned) {

            this.offset = offset;
            this.length = length;
            this.isRotated = isRotated;

            TextPaint wp;
            if (isSpanned) {
                wp = mWorkPaint;
                wp.set(mPaint);
                MetricAffectingSpan[] spans = ((Spanned) mText).getSpans(offset, offset + length, MetricAffectingSpan.class);
                for(MetricAffectingSpan span : spans) {
                    span.updateDrawState(wp);
                }
            } else {
                wp = mPaint;
            }

            // just record the normal non-rotated values here
            // measure and draw will take rotation into account
            measuredWidth = wp.measureText(mText, offset, offset + length);
            measuredHeight = wp.getFontMetrics().bottom - wp.getFontMetrics().top;
        }
 
Example 2
Project: Virtualview-Android   File: TMReminderTagsView.java   Source Code and License Vote up 6 votes
public TMReminderTagsView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    if (mDensity == -1) {
        initWith(context);
    }
    textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(Color.rgb(255, 59, 68));
    textPaint.setTextSize(dp2px(10));

    bgPaint = new Paint();
    bgPaint.setColor(Color.rgb(250, 211, 213));

    tagsGap = dp2px(7);

    hPadding = dp2px(3);

    tagRect = new Rect();

    textFontMetrics = textPaint.getFontMetrics();
}
 
Example 3
Project: chromium-for-android-56-debug-video   File: RoundedIconGenerator.java   Source Code and License Vote up 6 votes
/**
 * Constructs the generator and initializes the common members ignoring display density.
 *
 * @param iconWidthPx The width of the generated icon in pixels.
 * @param iconHeightPx The height of the generated icon in pixels.
 * @param cornerRadiusPx The radius of the corners in the icon in pixels.
 * @param backgroundColor Color at which the rounded rectangle should be drawn.
 * @param textSizePx Size at which the text should be drawn in pixels.
 */
public RoundedIconGenerator(int iconWidthPx, int iconHeightPx, int cornerRadiusPx,
        int backgroundColor, float textSizePx) {
    mIconWidthPx = iconWidthPx;
    mIconHeightPx = iconHeightPx;
    mCornerRadiusPx = cornerRadiusPx;

    mBackgroundRect = new RectF(0, 0, mIconWidthPx, mIconHeightPx);

    mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mBackgroundPaint.setColor(backgroundColor);

    mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mTextPaint.setColor(Color.WHITE);
    mTextPaint.setFakeBoldText(true);
    mTextPaint.setTextSize(textSizePx);

    FontMetrics textFontMetrics = mTextPaint.getFontMetrics();
    mTextHeight = (float) Math.ceil(textFontMetrics.bottom - textFontMetrics.top);
    mTextYOffset = -textFontMetrics.top;
}
 
Example 4
Project: Tribe   File: SelectRegoinDecoration.java   Source Code and License 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 5
Project: EasyChart   File: EasyGraphHistogram.java   Source Code and License Vote up 6 votes
private void init() {
    borderPaint = new Paint();
    borderPaint.setColor(borderColor);
    borderPaint.setStyle(Paint.Style.STROKE);
    borderPaint.setStrokeWidth(borderWidth);
    rectPaint = new Paint();
    rectPaint.setColor(rectColor);
    rectPaint.setStyle(Paint.Style.FILL);
    linePaint = new Paint();
    linePaint.setAntiAlias(true);
    linePaint.setColor(lineColor);
    linePaint.setStyle(Paint.Style.STROKE);
    linePaint.setStrokeWidth(lineWidth);
    linePaint.setStrokeJoin(Paint.Join.ROUND);
    textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(textColor);
    textPaint.setTextSize(textSize);
    fm = textPaint.getFontMetrics();
    path = new Path();
    pathDst = new Path();
    pm = new PathMeasure();
    selectedIndex = -1;
}
 
Example 6
Project: mongol-library   File: MongolLayout.java   Source Code and License Vote up 5 votes
/**
 * Return how wide a layout must be in order to display the
 * specified text slice with one line per paragraph.
 */
public static Rect getDesiredSize(CharSequence source,
                                  int start, int end,
                                  TextPaint paint) {

    MongolTextLine tl = MongolTextLine.obtain();

    float longestWidth = 0;
    float heightSum = 0;

    int next;
    for (int i = start; i <= end; i = next) {
        next = TextUtils.indexOf(source, '\n', i, end);

        if (next < 0)
            next = end;

        tl.set(paint, source, i, next);
        RectF size = tl.measure();
        float width = size.width(); // horizontal line orientation
        heightSum += size.height(); // horizontal line orientation

        if (width > longestWidth)
            longestWidth = width;

        next++;
    }

    MongolTextLine.recycle(tl);

    if (heightSum == 0) {
        heightSum = paint.getFontMetrics().bottom - paint.getFontMetrics().top;
    }

    // returning the size as a vertical line orientation (swapping width and height)
    return new Rect(0, 0, (int) heightSum, (int) longestWidth);
}
 
Example 7
Project: chromium-for-android-56-debug-video   File: TitleBitmapFactory.java   Source Code and License Vote up 5 votes
/**
 * @param context   The current Android's context.
 * @param incognito Whether the title are for incognito mode.
 * @param nullFaviconResourceId A drawable resource id of a default favicon.
 */
public TitleBitmapFactory(Context context, boolean incognito, int nullFaviconResourceId) {
    mNullFaviconResourceId = nullFaviconResourceId;

    Resources res = context.getResources();
    int textColor = ApiCompatibilityUtils.getColor(res, incognito
            ? R.color.compositor_tab_title_bar_text_incognito
            : R.color.compositor_tab_title_bar_text);
    float textSize = res.getDimension(R.dimen.compositor_tab_title_text_size);

    boolean fakeBoldText = res.getBoolean(R.bool.compositor_tab_title_fake_bold_text);

    mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mTextPaint.setColor(textColor);

    mTextPaint.setTextSize(textSize);
    mTextPaint.setFakeBoldText(fakeBoldText);
    mTextPaint.density = res.getDisplayMetrics().density;

    FontMetrics textFontMetrics = mTextPaint.getFontMetrics();
    mTextHeight = (float) Math.ceil(textFontMetrics.bottom - textFontMetrics.top);
    mTextYOffset = -textFontMetrics.top;

    mFaviconDimension = res.getDimensionPixelSize(R.dimen.compositor_tab_title_favicon_size);
    mViewHeight = (int) Math.max(mFaviconDimension, mTextHeight);

    int width = res.getDisplayMetrics().widthPixels;
    int height = res.getDisplayMetrics().heightPixels;
    mMaxWidth = (int) (TITLE_WIDTH_PERCENTAGE * Math.max(width, height));

    // Set the favicon dimension here.
    mFaviconDimension = Math.min(mMaxWidth, mFaviconDimension);
}
 
Example 8
Project: CurveView   File: CurveView.java   Source Code and License Vote up 5 votes
private int getTextOffsetY(TextPaint paint, int gravity) {
    int height = (int) (paint.getFontMetrics().descent - paint.getFontMetrics().ascent);
    int offset = (int) (paint.getFontMetrics().descent + paint.getFontMetrics().ascent) / 2;
    if ((gravity & Gravity.CENTER_VERTICAL) != 0) {
        offset += height / 2;
    } else if ((gravity & Gravity.BOTTOM) != 0) {
        offset += height;
    }
    return offset;
}
 
Example 9
Project: RRFramework-Android   File: TextRecyclerCellViewHolder.java   Source Code and License Vote up 5 votes
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public void setBadge(String text) {

    TextPaint textPaint = mTvBadge.getPaint();
    float textWidth = textPaint.measureText(text);
    Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
    float textHeight = fontMetrics.bottom - fontMetrics.top;

    int width = (int) Math.max(textWidth, textHeight);

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mTvBadge.getLayoutParams();
    params.width = width;
    params.height = width;
    mTvBadge.setLayoutParams(params);
    mTvBadge.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

    int radius = width / 2;

    RoundRectShape shape = new RoundRectShape(new float[] {radius, radius, radius, radius, radius, radius, radius, radius}, null, null);
    ShapeDrawable backgroundDrawable = new ShapeDrawable(shape);
    backgroundDrawable.getPaint().setColor(Color.parseColor("#ff0000"));
    backgroundDrawable.setAlpha(100);
    mTvBadge.setBackground(backgroundDrawable);

    mTvBadge.setText(text);

}
 
Example 10
Project: Rotatable-Scalable-Font   File: WidgetTextFactory.java   Source Code and License Vote up 5 votes
/**
 * get the height which limited line count
 *
 * @param typeface
 * @param maxLineCount
 * @return
 */
private int getMaxTextRectHeight(String typeface, int maxLineCount) {
    TextPaint textPaint = new TextPaint();
    textPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mDefaultMinTextSize, mContext.getResources().getDisplayMetrics()));
    if (typeface != null) {
        textPaint.setTypeface(Typeface.createFromAsset(mContext.getAssets(), typeface));
    }
    Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
    int textHeight = (int) Math.ceil(fontMetrics.bottom - fontMetrics.top);
    return textHeight * maxLineCount + (int) (textHeight * 0.9f) * (maxLineCount - 1);
}
 
Example 11
Project: GitHub   File: Utils.java   Source Code and License Vote up 4 votes
public static void drawMultilineText(Canvas c, StaticLayout textLayout,
                                     float x, float y,
                                     TextPaint paint,
                                     MPPointF anchor, float angleDegrees) {

    float drawOffsetX = 0.f;
    float drawOffsetY = 0.f;
    float drawWidth;
    float drawHeight;

    final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer);

    drawWidth = textLayout.getWidth();
    drawHeight = textLayout.getLineCount() * lineHeight;

    // Android sometimes has pre-padding
    drawOffsetX -= mDrawTextRectBuffer.left;

    // Android does not snap the bounds to line boundaries,
    //  and draws from bottom to top.
    // And we want to normalize it.
    drawOffsetY += drawHeight;

    // To have a consistent point of reference, we always draw left-aligned
    Paint.Align originalTextAlign = paint.getTextAlign();
    paint.setTextAlign(Paint.Align.LEFT);

    if (angleDegrees != 0.f) {

        // Move the text drawing rect in a way that it always rotates around its center
        drawOffsetX -= drawWidth * 0.5f;
        drawOffsetY -= drawHeight * 0.5f;

        float translateX = x;
        float translateY = y;

        // Move the "outer" rect relative to the anchor, assuming its centered
        if (anchor.x != 0.5f || anchor.y != 0.5f) {
            final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
                    drawWidth,
                    drawHeight,
                    angleDegrees);

            translateX -= rotatedSize.width * (anchor.x - 0.5f);
            translateY -= rotatedSize.height * (anchor.y - 0.5f);
            FSize.recycleInstance(rotatedSize);
        }

        c.save();
        c.translate(translateX, translateY);
        c.rotate(angleDegrees);

        c.translate(drawOffsetX, drawOffsetY);
        textLayout.draw(c);

        c.restore();
    } else {
        if (anchor.x != 0.f || anchor.y != 0.f) {

            drawOffsetX -= drawWidth * anchor.x;
            drawOffsetY -= drawHeight * anchor.y;
        }

        drawOffsetX += x;
        drawOffsetY += y;

        c.save();

        c.translate(drawOffsetX, drawOffsetY);
        textLayout.draw(c);

        c.restore();
    }

    paint.setTextAlign(originalTextAlign);
}
 
Example 12
Project: GitHub   File: DanmakuItem.java   Source Code and License Vote up 4 votes
private static int getFontHeight(TextPaint paint){
    Paint.FontMetrics fm = paint.getFontMetrics();
    return (int) Math.ceil(fm.descent - fm.top) + 2;
}
 
Example 13
Project: Hotspot-master-devp   File: AlignTextView.java   Source Code and License 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 14
Project: TextEmoji   File: TextEmoji.java   Source Code and License Vote up 4 votes
/**
 *得到文字的高度
 */
private float getFontHeight(TextPaint paint) {
    Paint.FontMetrics fm = paint.getFontMetrics();// 得到系统默认字体属性
    return fm.bottom - fm.top;
}
 
Example 15
Project: Android-LabelView   File: LabelView.java   Source Code and License Vote up 4 votes
private void init(Context context, AttributeSet attrs) {

        if (attrs != null) {
            TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LabelView, 0, 0);
            try {
                textValue = a.getString(R.styleable.LabelView_textValue);
                labelValue = a.getString(R.styleable.LabelView_labelValue);
                lineColour = a.getColor(R.styleable.LabelView_lineColour, ContextCompat.getColor(context, DEFAULT_LINE_COLOR));
                textColour = a.getColor(R.styleable.LabelView_textColour, ContextCompat.getColor(context, DEFAULT_TEXT_COLOR));
                labelColour = a.getColor(R.styleable.LabelView_labelColour, ContextCompat.getColor(context, DEFAULT_TEXT_COLOR));
                lineThickness = a.getFloat(R.styleable.LabelView_lineThickness, 4f);
                labelSize = a.getDimensionPixelSize(R.styleable.LabelView_labelSize, (int) DEFAULT_TEXT_SIZE);
                textSize = a.getDimensionPixelSize(R.styleable.LabelView_textSize, (int) DEFAULT_TEXT_SIZE);
            } finally {
                a.recycle();
            }
        } else {
            textSize = labelSize = (int) DEFAULT_TEXT_SIZE;
            lineColour = ContextCompat.getColor(context, DEFAULT_LINE_COLOR);
            textColour = ContextCompat.getColor(context, DEFAULT_TEXT_COLOR);
            labelColour = textColour;
            lineThickness = DEFAULT_LINE_THICKNESS;
        }

        if (textValue == null) textValue = "";
        if (labelValue == null) labelValue = "";

        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setColor(lineColour);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(lineThickness);
        linePaint.setStrokeCap(Paint.Cap.SQUARE);
        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);

        textPaint.setColor(textColour);
        textPaint.setTextSize(textSize);
        textPaint.setStyle(Paint.Style.FILL);

        labelPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        labelPaint.setColor(labelColour);
        labelPaint.setTextSize(labelSize);
        textMetrics = textPaint.getFontMetrics();
        labelMetrics = labelPaint.getFontMetrics();

        textBounds = new Rect();
        labelBounds = new Rect();
    }