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

The following are Jave code examples for showing how to use getFontMetricsInt() 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: GitHub   File: BetterImageSpan.java   View Source Code Vote up 6 votes
@Override
public void draw(
    Canvas canvas,
    CharSequence text,
    int start,
    int end,
    float x,
    int top,
    int y,
    int bottom,
    Paint paint) {
  paint.getFontMetricsInt(mFontMetricsInt);
  int iconTop = y + getOffsetAboveBaseline(mFontMetricsInt);
  canvas.translate(x, iconTop);
  mDrawable.draw(canvas);
  canvas.translate(-x, -iconTop);
}
 
Example 2
Project: Android-UtilCode   File: SpannableStringUtils.java   View Source Code Vote up 6 votes
@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 d = getCachedDrawable();
    Rect rect = d.getBounds();
    canvas.save();
    final float fontHeight = paint.getFontMetrics().descent - paint.getFontMetrics().ascent;
    int transY = bottom - rect.bottom;
    if (rect.height() < fontHeight) { // this is the fucking code which I waste 3 days
        if (mVerticalAlignment == ALIGN_BASELINE) {
            transY -= paint.getFontMetricsInt().descent;
        } else if (mVerticalAlignment == ALIGN_CENTER) {
            transY -= (fontHeight - rect.height()) / 2;
        } else if (mVerticalAlignment == ALIGN_TOP) {
            transY -= fontHeight - rect.height();
        }
    } else {
        if (mVerticalAlignment == ALIGN_BASELINE) {
            transY -= paint.getFontMetricsInt().descent;
        }
    }
    canvas.translate(x, transY);
    d.draw(canvas);
    canvas.restore();
}
 
Example 3
Project: QMUI_Android   File: QMUIAlignMiddleImageSpan.java   View Source Code Vote up 6 votes
@Override
    public void draw(Canvas canvas, CharSequence text, int start, int end,
                     float x, int top, int y, int bottom, Paint paint) {
        if (mVerticalAlignment == ALIGN_MIDDLE) {
            Drawable d = getDrawable();
            canvas.save();

//            // 注意如果这样实现会有问题:TextView 有 lineSpacing 时,这里 bottom 偏大,导致偏下
//            int transY = bottom - d.getBounds().bottom; // 底对齐
//            transY -= (paint.getFontMetricsInt().bottom - paint.getFontMetricsInt().top) / 2 - d.getBounds().bottom / 2; // 居中对齐
//            canvas.translate(x, transY);
//            d.draw(canvas);
//            canvas.restore();

            Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
            int fontTop = y + fontMetricsInt.top;
            int fontMetricsHeight = fontMetricsInt.bottom - fontMetricsInt.top;
            int iconHeight = d.getBounds().bottom - d.getBounds().top;
            int iconTop = fontTop + (fontMetricsHeight - iconHeight) / 2;
            canvas.translate(x, iconTop);
            d.draw(canvas);
            canvas.restore();
        } else {
            super.draw(canvas, text, start, end, x, top, y, bottom, paint);
        }
    }
 
Example 4
Project: browser   File: VerticalImageSpan.java   View Source Code Vote up 6 votes
@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 5
Project: qmui   File: EmojiconSpan.java   View Source Code Vote up 6 votes
@Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
//        return super.getSize(paint, text, start, end, fm);

        // fm 以 Paint 的 fm 为基准,避免 Span 改变了 fm 导致文字行高变化 -- chant
        Drawable d = getCachedDrawable();
        Rect rect = d.getBounds();

        if (fm != null) {
            Paint.FontMetricsInt pfm = paint.getFontMetricsInt();
            // keep it the same as paint's fm
            fm.ascent = pfm.ascent;
            fm.descent = pfm.descent;
            fm.top = pfm.top;
            fm.bottom = pfm.bottom;
        }

        return rect.right;
    }
 
Example 6
Project: decoy   File: ImageSpanAlignCenter.java   View Source Code Vote up 6 votes
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y,
                 int bottom, Paint paint) {
    final String s = text.toString();
    String subS = s.substring(start, end);
    if (ELLIPSIS_NORMAL[0] == subS.charAt(0)
            || ELLIPSIS_TWO_DOTS[0] == subS.charAt(0)) {
        canvas.save();
        canvas.drawText(subS, x, y, paint);
        canvas.restore();
    } else {
        Drawable d = getCachedDrawable(paint);
        canvas.save();
        int transY;
        Paint.FontMetricsInt fontMetrics = new Paint.FontMetricsInt();
        paint.getFontMetricsInt(fontMetrics);
        transY = y + fontMetrics.ascent;
        canvas.translate(x, transY);
        d.draw(canvas);
        canvas.restore();
    }
}
 
Example 7
Project: Tusky   File: CustomEmojiHelper.java   View Source Code Vote up 6 votes
@Override
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end,
                   @Nullable Paint.FontMetricsInt fm) {

    /* update FontMetricsInt or otherwise span does not get drawn when
       it covers the whole text */
    Paint.FontMetricsInt metrics = paint.getFontMetricsInt();
    if (fm != null) {
        fm.top = metrics.top;
        fm.ascent = metrics.ascent;
        fm.descent = metrics.descent;
        fm.bottom = metrics.bottom;
    }

    return (int) (paint.getTextSize()*1.2);
}
 
Example 8
Project: decoy   File: ImageSpanAlignCenter.java   View Source Code Vote up 6 votes
@Override
public int getSize(Paint paint, CharSequence text,
                   int start, int end,
                   Paint.FontMetricsInt fm) {
    Drawable d = getCachedDrawable(paint);
    Rect rect = d.getBounds();

    if (fm != null) {
        Paint.FontMetricsInt fontMetrics = new Paint.FontMetricsInt();
        paint.getFontMetricsInt(fontMetrics);

        fm.ascent = fontMetrics.ascent;
        fm.descent = fontMetrics.descent;

        fm.top = fontMetrics.top;
        fm.bottom = fontMetrics.bottom;
    }
    return rect.right;
}
 
Example 9
Project: RNLearn_Project1   File: InlineImageSpanWithPipeline.java   View Source Code Vote up 6 votes
@Override
public void draw(
    Canvas canvas,
    CharSequence text,
    int start,
    int end,
    float x,
    int top,
    int y,
    int bottom,
    Paint paint) {
  if (mRequestHelper == null) {
    return;
  }

  Bitmap bitmap = mRequestHelper.getBitmap();
  if (bitmap == null) {
    return;
  }

  float bottomFloat = (float) bottom - paint.getFontMetricsInt().descent;
  TMP_RECT.set(x, bottomFloat - mHeight, x + mWidth, bottomFloat);

  canvas.drawBitmap(bitmap, null, TMP_RECT, paint);
}
 
Example 10
Project: QMUI_Android   File: QMUITextSizeSpan.java   View Source Code Vote up 5 votes
@Override
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    mPaint = new Paint(paint);
    mPaint.setTextSize(mTextSize);
    if(mTextSize > paint.getTextSize() && fm != null){
        Paint.FontMetricsInt newFm = mPaint.getFontMetricsInt();
        fm.descent = newFm.descent;
        fm.ascent = newFm.ascent;
        fm.top = newFm.top;
        fm.bottom = newFm.bottom;
    }
    return (int) mPaint.measureText(text, start, end);
}
 
Example 11
Project: QMUI_Android   File: QMUIBlockSpaceSpan.java   View Source Code Vote up 5 votes
@Override
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    if (fm != null && !QMUIDeviceHelper.isMeizu()) {
        //return后宽度为0,因此实际空隙和段落开始在同一行,需要加上一行的高度
        fm.ascent = fm.top = -mHeight - paint.getFontMetricsInt(fm);
        fm.descent = fm.bottom = 0;
    }
    return 0;
}
 
Example 12
Project: LaunchEnr   File: TintedDrawableSpan.java   View Source Code Vote up 5 votes
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, FontMetricsInt fm) {
    fm = fm == null ? paint.getFontMetricsInt() : fm;
    int iconSize = fm.bottom - fm.top;
    mDrawable.setBounds(0, 0, iconSize, iconSize);
    return super.getSize(paint, text, start, end, fm);
}
 
Example 13
Project: boohee_v5.6   File: CircleIndicator.java   View Source Code Vote up 5 votes
private void drawCircle(Canvas canvas) {
    Paint textPaint = new Paint();
    textPaint.setColor(this.mCircleWhite);
    textPaint.setAntiAlias(true);
    textPaint.setTextSize(getResources().getDimension(R.dimen.out_indicator_size));
    float textHeight = (float) ViewUtils.getTextHeight(textPaint);
    float perAngle = 270.0f / (this.mEndIndicator - this.mStartIndicator);
    int radius = (int) (((float) getViewRadius()) - (2.6f * ((float) this.mDividerWidth)));
    FontMetricsInt fmi = textPaint.getFontMetricsInt();
    float textRadius = (float) (radius - (Math.abs(fmi.bottom + fmi.top) / 2));
    RectF oval = new RectF((float) (getCenterX() - radius), (float) (getCenterY() - radius),
            (float) (getCenterX() + radius), (float) (getCenterY() + radius));
    Path path = new Path();
    path.addCircle((float) getCenterX(), (float) getCenterY(), textRadius, Direction.CW);
    Paint circlePaint = new Paint();
    circlePaint.setAntiAlias(true);
    circlePaint.setStrokeWidth(16.0f + textHeight);
    circlePaint.setStyle(Style.STROKE);
    if (this.mDividerIndicator.size() == 0) {
        circlePaint.setStrokeCap(Cap.ROUND);
        circlePaint.setColor(this.mCircleGray);
        canvas.drawArc(oval, 135.0f, 270.0f, false, circlePaint);
        return;
    }
    circlePaint.setStrokeCap(Cap.ROUND);
    drawCircleContent(canvas, (IndicatorItem) this.mDividerIndicator.get(0), oval, perAngle,
            textRadius, path, textPaint, circlePaint);
    Canvas canvas2 = canvas;
    drawCircleContent(canvas2, (IndicatorItem) this.mDividerIndicator.get(this
            .mDividerIndicator.size() - 1), oval, perAngle, textRadius, path, textPaint,
            circlePaint);
    circlePaint.setStrokeCap(Cap.BUTT);
    for (int i = 1; i < this.mDividerIndicator.size() - 1; i++) {
        drawCircleContent(canvas, (IndicatorItem) this.mDividerIndicator.get(i), oval,
                perAngle, textRadius, path, textPaint, circlePaint);
    }
}
 
Example 14
Project: TextTag   File: TextTagSpan.java   View Source Code Vote up 5 votes
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
    float height = bottom - top;
    float textHeight = height * textSizeRatio;
    float minus = (height - textHeight) / 2;
    RectF rect = new RectF(x, top + minus, x + measureText(paint, text, start, end), bottom - minus);
    paint.setColor(bgColor);
    canvas.drawRect(rect, paint);

    paint.setColor(textColor);
    Paint.FontMetricsInt fm = paint.getFontMetricsInt();
    canvas.drawText(text, start, end,
            x, y - ((y + fm.ascent + y + fm.descent) / 2 - (bottom + top) / 2), paint);
}
 
Example 15
Project: LiveGiftLayout   File: AnimatedImageSpan.java   View Source Code Vote up 5 votes
@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();
    canvas.save();

    int transY = bottom - b.getBounds().bottom;
    if (mVerticalAlignment == ALIGN_BASELINE) {
        transY -= paint.getFontMetricsInt().descent;
    }

    canvas.translate(x, transY);
    b.draw(canvas);
    canvas.restore();

}
 
Example 16
Project: OpenHub   File: EllipsizeLineSpan.java   View Source Code Vote up 5 votes
@Override
public int getSize(@NonNull Paint paint, CharSequence text,
                   int start, int end, Paint.FontMetricsInt fm) {
    if (fm != null) {
        paint.getFontMetricsInt(fm);
    }
    int textWidth = (int) Math.ceil(paint.measureText(text, start, end));
    return Math.min(textWidth, mClipRect.width());
}
 
Example 17
Project: JKCloud   File: TipView.java   View Source Code Vote up 4 votes
private int getTextY(Paint paint) {
    Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
    return ((getMeasuredHeight() - fontMetrics.bottom - fontMetrics.top) / 2);
}
 
Example 18
Project: snippety   File: HorizontalLineSpan.java   View Source Code Vote up 4 votes
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y,
                 int bottom, Paint paint) {

    Paint.FontMetricsInt fmi = paint.getFontMetricsInt();

    int height = fmi.descent - fmi.ascent; // set the drawable height to the height of the current font
    Drawable b = getDrawable(canvas.getWidth(), height);

    canvas.save();

    int transY = bottom - b.getBounds().bottom;

    canvas.translate(x, transY);
    b.draw(canvas);

    canvas.restore();
}
 
Example 19
Project: aos-Video   File: TvshowDetailsDescriptionPresenter.java   View Source Code Vote up 4 votes
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 20
Project: letv   File: VideoShotEditActivity.java   View Source Code Vote up 4 votes
private String getFileAddedHybridWatermark(String path, int logoResId, String text, String voice) {
    if (!new File(path).exists()) {
        return null;
    }
    Options options = new Options();
    options.inJustDecodeBounds = false;
    Bitmap bitmap0 = BitmapFactory.decodeFile(path, options);
    int width = options.outWidth;
    int height = options.outHeight;
    Bitmap bitmapPic = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapPic);
    canvas.drawBitmap(bitmap0, null, new Rect(0, 0, width, height), null);
    Bitmap bitmapLogo = ((BitmapDrawable) getResources().getDrawable(logoResId)).getBitmap();
    int widthLogo = bitmapLogo.getWidth();
    int heightLogo = bitmapLogo.getHeight();
    float scale = getScaleForShare();
    int widthOffset = (int) (((float) widthLogo) * scale);
    int heightOffset = (int) (((float) heightLogo) * scale);
    LogInfo.log("fornia", "widthLogo:" + widthLogo + "|heightLogo:" + heightLogo + "scale:" + scale + "text:" + text);
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    LogInfo.log("fornia", "widthLogo:" + widthLogo + "|heightLogo:" + heightLogo + "widthOffset:" + widthOffset + "|heightOffset:" + heightOffset);
    if (bitmapLogo != null) {
        canvas.drawBitmap(Bitmap.createBitmap(bitmapLogo, 0, 0, widthLogo, heightLogo, matrix, true), 30.0f, 25.0f, null);
    }
    if (!TextUtils.isEmpty(text)) {
        Paint textPaint = new Paint();
        textPaint.setColor(-1);
        textPaint.setTextSize((float) getTextSize());
        textPaint.setFlags(2);
        canvas.drawText(text, 0, text.length(), (float) (widthOffset + 42), (float) (heightOffset + 22), textPaint);
    }
    Rect rect = new Rect(0, height - getTextBgHeight(), width, height);
    if (!isOrigPicMode) {
        Paint voiceBgPaint = new Paint();
        voiceBgPaint.setColor(getResources().getColor(2131493164));
        voiceBgPaint.setStyle(Style.FILL);
        voiceBgPaint.setFlags(2);
        canvas.drawRect(rect, voiceBgPaint);
        voiceBgPaint.setColor(-1);
        voiceBgPaint.setTextSize((float) getTextSize());
        voiceBgPaint.setTextAlign(Align.CENTER);
        FontMetricsInt fontMetrics = voiceBgPaint.getFontMetricsInt();
        canvas = canvas;
        String str = voice;
        canvas.drawText(str, (float) rect.centerX(), (float) ((rect.top + ((((rect.bottom - rect.top) - fontMetrics.bottom) + fontMetrics.top) / 2)) - fontMetrics.top), voiceBgPaint);
    }
    return saveBitmap(bitmapPic);
}