Java Code Examples for android.graphics.Paint#getFontMetricsInt()

The following examples show how to use android.graphics.Paint#getFontMetricsInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CodeSpan.java    From Markdown with MIT License 6 votes vote down vote up
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    mHeight = paint.getFontMetricsInt().descent - paint.getFontMetricsInt().ascent;

    float size = paint.getTextSize();
    paint.setTextSize(size * TEXT_SIZE_SCALE);
    paint.setTypeface(Typeface.MONOSPACE);

    mWidth = (int) (paint.measureText(text, start, end) + PADDING_HORIZONTAL * 2 + MARGIN * 2);
    if (fm != null) {
        fm.top -= PADDING_VERTICAL;
        fm.bottom += PADDING_VERTICAL;
    }

    paint.setTextSize(size);
    return mWidth;
}
 
Example 2
Source File: VerticalImageSpan.java    From browser with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end,
                 float x, int top, int y, int bottom, Paint paint) {

    Drawable drawable = getDrawable();
    canvas.save();

    int length = 0;
    int dy = bottom - drawable.getBounds().bottom + mPadding;
    if(mVerticalAlignment == ALIGN_BASELINE) {
        length = text.length();
    }

    for(int i = 0 ; i < length ; i++) {
        if (!(Character.isLetterOrDigit(text.charAt(i)))) {
            continue;
        }
        dy -= paint.getFontMetricsInt().descent;
        canvas.translate(x, dy);
        drawable.draw(canvas);
        canvas.restore();
        return ;
    }
}
 
Example 3
Source File: SpansV1.java    From Pioneer with Apache License 2.0 6 votes vote down vote up
@Override
public int getSize(@NonNull Paint paint, CharSequence text, @IntRange(from = 0) int start, @IntRange(from = 0) int end, @Nullable Paint.FontMetricsInt fm) {
    width = 0;
    for (CharacterStyle style : styles) {
        if (style instanceof ReplacementSpan) {
            width = Math.max(width, ((ReplacementSpan) style).getSize(paint, text, start, end, fm));
        } else if (paint instanceof TextPaint) {
            if (style instanceof MetricAffectingSpan) {
                ((MetricAffectingSpan) style).updateMeasureState((TextPaint) paint);
            }
        }
    }
    if (fm != null) {
        paint.getFontMetricsInt(fm);
    }
    paint.getFontMetricsInt(fontMetricsInt);
    width = Math.max(width, (int) Math.ceil(paint.measureText(text, start, end)));
    frame.right = width;
    frame.top = fontMetricsInt.top;
    frame.bottom = fontMetricsInt.bottom;
    if (bitmap == null) {
        bitmap = Bitmap.createBitmap(width, frame.bottom - frame.top, Bitmap.Config.ARGB_8888);
        bitmapCanvas = new Canvas(bitmap);
    }
    return width;
}
 
Example 4
Source File: Cocos2dxBitmap.java    From Example-of-Cocos2DX with MIT License 6 votes vote down vote up
private static TextProperty computeTextProperty(final String string,
		final int width, final int height, final Paint paint) {
	final FontMetricsInt fm = paint.getFontMetricsInt();
	final int h = (int) Math.ceil(fm.bottom - fm.top);
	int maxContentWidth = 0;

	final String[] lines = Cocos2dxBitmap.splitString(string, width,
			height, paint);

	if (width != 0) {
		maxContentWidth = width;
	} else {
		/* Compute the max width. */
		int temp = 0;
		for (final String line : lines) {
			temp = (int) FloatMath.ceil(paint.measureText(line, 0,
					line.length()));
			if (temp > maxContentWidth) {
				maxContentWidth = temp;
			}
		}
	}

	return new TextProperty(maxContentWidth, h, lines);
}
 
Example 5
Source File: ChipDrawableSpan.java    From lttrs-android with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(@NonNull final Canvas canvas,
                 final CharSequence text,
                 final int start,
                 final int end,
                 final float x,
                 final int top,
                 final int y,
                 final int bottom,
                 @NonNull final Paint paint) {
    final Drawable drawable = getDrawable();
    final Rect bounds = drawable.getBounds();
    canvas.save();
    final Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
    final int fontHeight = fontMetrics.descent - fontMetrics.ascent;
    final int centerY = y + fontMetrics.descent - fontHeight / 2;
    final int transY = centerY - (bounds.bottom - bounds.top) / 2;
    canvas.translate(x, transY);
    drawable.draw(canvas);
    canvas.restore();
}
 
Example 6
Source File: SpansV1.java    From Pioneer with Apache License 2.0 6 votes vote down vote up
@Override
public int getSize(@NonNull Paint paint, CharSequence text, @IntRange(from = 0) int start, @IntRange(from = 0) int end, @Nullable Paint.FontMetricsInt fm) {
    width = 0;
    for (CharacterStyle style : styles) {
        if (style instanceof ReplacementSpan) {
            width = Math.max(width, ((ReplacementSpan) style).getSize(paint, text, start, end, fm));
        } else if (paint instanceof TextPaint) {
            if (style instanceof MetricAffectingSpan) {
                ((MetricAffectingSpan) style).updateMeasureState((TextPaint) paint);
            }
        }
    }
    if (fm != null) {
        paint.getFontMetricsInt(fm);
    }
    paint.getFontMetricsInt(fontMetricsInt);
    width = Math.max(width, (int) Math.ceil(paint.measureText(text, start, end)));
    frame.right = width;
    frame.top = fontMetricsInt.top;
    frame.bottom = fontMetricsInt.bottom;
    if (bitmap == null) {
        bitmap = Bitmap.createBitmap(width, frame.bottom - frame.top, Bitmap.Config.ARGB_8888);
        bitmapCanvas = new Canvas(bitmap);
    }
    return width;
}
 
Example 7
Source File: VerticalImageSpan.java    From Twire with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void draw(@NonNull Canvas canvas, CharSequence text,
                 int start, int end, float x,
                 int top, int y, int bottom, @NonNull Paint paint) {
    Drawable drawable = getDrawable();
    Rect bounds = drawable.getBounds();
    canvas.save();

    Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
    int descent = fontMetricsInt.descent;
    int transY = (y + descent) - ((descent - fontMetricsInt.ascent) / 2);
    canvas.translate(x, (float) (transY - (bounds.height() / 2)));

    drawable.draw(canvas);
    canvas.restore();
}
 
Example 8
Source File: SpansV1.java    From Pioneer with Apache License 2.0 6 votes vote down vote up
@Override
        public int getSize(@NonNull Paint paint, CharSequence text, @IntRange(from = 0) int start, @IntRange(from = 0) int end, @Nullable Paint.FontMetricsInt fm) {
            paint.getFontMetricsInt(fontMetricsInt);
            frame.right = (int) Math.ceil(paint.measureText(text, start, end));
            frame.top = fontMetricsInt.top;
            frame.bottom = fontMetricsInt.bottom;

            if (fm != null) {
                paint.getFontMetricsInt(fm);
//                fm.ascent = -frame.bottom;
//                fm.descent = 0;
//
//                fm.top = fm.ascent;
//                fm.bottom = 0;
            }

            return frame.right;
        }
 
Example 9
Source File: CenterImageSpan.java    From RichEditor with MIT License 6 votes vote down vote up
@Override public int getSize(Paint paint, CharSequence text, int start, int end,
                             Paint.FontMetricsInt fontMetricsInt) {
    Drawable drawable = getDrawable();
    Rect rect = drawable.getBounds();
    if (fontMetricsInt != null) {
        Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
        int fontHeight = fmPaint.descent - fmPaint.ascent;
        int drHeight = rect.bottom - rect.top;
        int centerY = fmPaint.ascent + fontHeight / 2;

        fontMetricsInt.ascent = centerY - drHeight / 2;
        fontMetricsInt.top = fontMetricsInt.ascent;
        fontMetricsInt.bottom = centerY + drHeight / 2;
        fontMetricsInt.descent = fontMetricsInt.bottom;
    }
    return rect.right;
}
 
Example 10
Source File: IconSpan.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {

    final int b = bottom - drawable.getBounds().bottom;

    final int save = canvas.save();
    try {
        final int translationY;
        if (ALIGN_CENTER == alignment) {
            translationY = b - ((bottom - top - drawable.getBounds().height()) / 2);
        } else if (ALIGN_BASELINE == alignment) {
            translationY = b - paint.getFontMetricsInt().descent;
        } else {
            translationY = b;
        }
        canvas.translate(x, translationY);
        drawable.draw(canvas);
    } finally {
        canvas.restoreToCount(save);
    }
}
 
Example 11
Source File: CenteredImageSpan.java    From chaoli-forum-for-android-2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint)
{
	Drawable d = getDrawable();
	Paint.FontMetricsInt fm = paint.getFontMetricsInt();
	int transY = y + (fm.descent + fm.ascent - d.getBounds().bottom) / 2;
	canvas.save();
	canvas.translate(x, transY);
	d.draw(canvas);
	canvas.restore();
}
 
Example 12
Source File: TextViewUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取画布中间居中位置
 * @param targetRect {@link Rect} 目标坐标
 * @param paint      {@link TextView#getPaint()}
 * @return 画布 Y 轴居中位置
 */
public static int getCenterRectY(final Rect targetRect, final Paint paint) {
    if (targetRect != null && paint != null) {
        // 获取字体高度
        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
        // 获取底部 Y 轴居中位置
        return targetRect.top + (targetRect.bottom - targetRect.top) / 2 - (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.top;
        // canvas.drawText(testString, targetRect.centerX(), baseline, paint);
    }
    return -1;
}
 
Example 13
Source File: Spans.java    From Pioneer with Apache License 2.0 5 votes vote down vote up
public int getMinSize(@NonNull Rect outRect, @NonNull Paint paint, CharSequence text, @IntRange(from = 0) int start, @IntRange(from = 0) int end, @Nullable Paint.FontMetricsInt fm) {
    if (fm != null) {
        paint.getFontMetricsInt(fm);
    }
    int width = fixedWidth >= 0 ? fixedWidth : (int) Math.ceil(paint.measureText(text, start, end));
    paint.getFontMetricsInt(fontMetricsInt);
    frame.right = width;
    frame.top = fontMetricsInt.top;
    frame.bottom = fontMetricsInt.bottom;
    return width;
}
 
Example 14
Source File: DegreeSeekBar.java    From imsdk-android with MIT License 5 votes vote down vote up
private void init() {
    mPointColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_primary);
    mTextColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_primary);
    mCenterTextColor = ContextCompat.getColor(getContext(), R.color.easy_photos_fg_accent);

    mPointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPointPaint.setStyle(Paint.Style.STROKE);
    mPointPaint.setColor(mPointColor);
    mPointPaint.setStrokeWidth(2);

    mTextPaint = new Paint();
    mTextPaint.setColor(mTextColor);
    mTextPaint.setStyle(Paint.Style.STROKE);
    mTextPaint.setAntiAlias(true);
    mTextPaint.setTextSize(24f);
    mTextPaint.setTextAlign(Paint.Align.LEFT);
    mTextPaint.setAlpha(100);

    mFontMetrics = mTextPaint.getFontMetricsInt();

    mTextWidths = new float[1];
    mTextPaint.getTextWidths("0", mTextWidths);

    mCirclePaint = new Paint();
    mCirclePaint.setStyle(Paint.Style.FILL);
    mCirclePaint.setAlpha(255);
    mCirclePaint.setAntiAlias(true);
}
 
Example 15
Source File: DialogCell.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    if (fm == null) {
        fm = paint.getFontMetricsInt();
    }
    if (fm != null) {
        int h = fm.descent - fm.ascent;
        fm.bottom = fm.descent = 1 - h;
        fm.top = fm.ascent = -1;
    }
    return width;
}
 
Example 16
Source File: WaveView.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
/**
 * 绘制文字
 *
 * @param canvas 画布
 * @param paint  画笔
 * @param text   画的文字
 */
private void drawText(Canvas canvas, Paint paint, String text) {
    //画布的大小
    Rect targetRect = new Rect(0, 0, width, height);
    Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
    int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
    // 下面这行是实现水平居中,drawText对应改为传入targetRect.centerX()
    paint.setTextAlign(Paint.Align.CENTER);
    canvas.drawText(text, targetRect.centerX(), baseline, paint);
}
 
Example 17
Source File: CenterAlignImageSpan.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom,
                 Paint paint) {

    Drawable b = getDrawable();
    Paint.FontMetricsInt fm = paint.getFontMetricsInt();
    int transY = (y + fm.descent + y + fm.ascent) / 2 - b.getBounds().bottom / 2;//计算y方向的位移
    canvas.save();
    canvas.translate(x, transY);//绘制图片位移一段距离
    b.draw(canvas);
    canvas.restore();
}
 
Example 18
Source File: MyAbstractDetailsPresenter.java    From jellyfin-androidtv with GNU General Public License v2.0 4 votes vote down vote up
private Paint.FontMetricsInt getFontMetricsInt(TextView textView) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(textView.getTextSize());
    paint.setTypeface(textView.getTypeface());
    return paint.getFontMetricsInt();
}
 
Example 19
Source File: SoftKeyboardView.java    From AndroidTVWidget with Apache License 2.0 4 votes vote down vote up
private void init(Context context, AttributeSet attrs) {
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mFmi = mPaint.getFontMetricsInt();
}
 
Example 20
Source File: YummyTextSwitcher.java    From YummyTextSwitcher with MIT License 4 votes vote down vote up
private void init() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            //View从API Level 11才加入setLayerType方法
            //设置myView以软件渲染模式绘图
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }


        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setTextAlign(Paint.Align.CENTER);

        mFirstFramePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mFirstFramePaint.setTextSize(mTextSize);
        mFirstFramePaint.setAntiAlias(true);
        mFirstFramePaint.setTextSize(mTextSize);
        mFirstFramePaint.setColor(mTextColor);
        mFirstFramePaint.setStyle(Paint.Style.FILL);
        mFirstFramePaint.setTextAlign(Paint.Align.CENTER);

        mSecondFramePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mSecondFramePaint.setTextSize(mTextSize);
        mSecondFramePaint.setAntiAlias(true);
        mSecondFramePaint.setTextSize(mTextSize);
        mSecondFramePaint.setColor(mTextColor);
        mSecondFramePaint.setStyle(Paint.Style.FILL);
        mSecondFramePaint.setTextAlign(Paint.Align.CENTER);

        mMiddleFramePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mMiddleFramePaint.setTextSize(mTextSize);
        mMiddleFramePaint.setAntiAlias(true);
        mMiddleFramePaint.setTextSize(mTextSize);
        mMiddleFramePaint.setColor(mTextColor);
        mMiddleFramePaint.setStyle(Paint.Style.FILL);
        mMiddleFramePaint.setTextAlign(Paint.Align.CENTER);
        
        mMaskFilterFirst = new BlurMaskFilter(3, BlurMaskFilter.Blur.NORMAL);
        mMaskFilterSecond = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
        mMaskFilterMiddle = new BlurMaskFilter(18, BlurMaskFilter.Blur.NORMAL);
        
        mFirstFramePaint.setMaskFilter(mMaskFilterFirst);
        mSecondFramePaint.setMaskFilter(mMaskFilterSecond);
        mMiddleFramePaint.setMaskFilter(mMaskFilterMiddle);
        
        testPaint = new Paint();
        testPaint.setStyle(Paint.Style.FILL);
        testPaint.setColor(Color.WHITE);
        
        if (mFrameOffset <= 0) {
            Paint.FontMetricsInt fmi = mTextPaint.getFontMetricsInt();
            mFrameOffset = fmi.bottom - fmi.top;
        }

    }