Java Code Examples for android.graphics.Paint.FontMetricsInt

The following are Jave code examples for showing how to use FontMetricsInt 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: HeadlineNews   File: SpanUtils.java   View Source Code Vote up 6 votes
@Override
public void chooseHeight(final CharSequence text, final int start, final int end, final int spanstartv, final int v, final Paint.FontMetricsInt fm) {
    int need = height - (v + fm.descent - fm.ascent - spanstartv);
    if (need > 0) {
        if (mVerticalAlignment == ALIGN_TOP) {
            fm.descent += need;
        } else if (mVerticalAlignment == ALIGN_CENTER) {
            fm.descent += need / 2;
            fm.ascent -= need / 2;
        } else {
            fm.ascent -= need;
        }
    }
    need = height - (v + fm.bottom - fm.top - spanstartv);
    if (need > 0) {
        if (mVerticalAlignment == ALIGN_TOP) {
            fm.top += need;
        } else if (mVerticalAlignment == ALIGN_CENTER) {
            fm.bottom += need / 2;
            fm.top -= need / 2;
        } else {
            fm.top -= need;
        }
    }
}
 
Example 2
Project: CornerTipView   File: CornerTipView.java   View Source Code Vote up 6 votes
/**
 * 左上角画梯形
 * @param canvas
 * @param realWaist 腰长
 */
private void drawTopLeftTip(Canvas canvas, float realWaist) {
    path.moveTo(this.realStartDisH, 0);
    path.lineTo(this.realStartDisH + realWaist, 0);
    path.lineTo(0, this.realStartDisH + realWaist);
    path.lineTo(0, this.realStartDisH);
    path.close();
    canvas.drawPath(path, backPaint);
    canvas.save();

    Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
    float dis = (waistDis /2  + this.startDisH) /2;
    float disY = dis - (fontMetrics.bottom + fontMetrics.top) / 2f;
    canvas.rotate(-45, dis, dis);
    canvas.drawText(tipContent, dis, disY, textPaint);
    canvas.rotate(45, dis, dis);
    canvas.restore();
}
 
Example 3
Project: tuxguitar   File: TGPopwindowView.java   View Source Code Vote up 6 votes
@Override
    public void onDraw(Canvas canvas) {
//        paint.setColor(Color.WHITE);
//        paint.setAlpha(0xff);
//        canvas.drawRect(0,0,SIZE,SIZE,paint);

        paint.setColor(Color.BLACK);
        paint.setAlpha(0x33);
        canvas.drawCircle(SIZE / 2, SIZE / 2, SIZE / 2, paint);


        paint.setColor(Color.WHITE);
        paint.setAlpha(0xff);
        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
        int baseline = (SIZE - fontMetrics.bottom - fontMetrics.top) / 2;
        canvas.drawText(count, SIZE / 2, baseline, paint);
    }
 
Example 4
Project: QMUI_Android   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 5
Project: airgram   File: ChipSpan.java   View Source Code Vote up 6 votes
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    if (fm == null) {
        fm = new Paint.FontMetricsInt();
    }

    int sz = super.getSize(paint, text, start, end, fm);
    int offset = AndroidUtilities.dp(6);
    int w = (fm.bottom - fm.top) / 2;
    fm.top = -w - offset;
    fm.bottom = w - offset;
    fm.ascent = -w - offset;
    fm.leading = 0;
    fm.descent = w - offset;
    return sz;
}
 
Example 6
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 7
Project: snippety   File: CenteredImageSpan.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();
    Rect rect = d.getBounds();

    if (fm != null) {
        // Centers the text with the ImageSpan
        if (rect.bottom - (fm.descent - fm.ascent) >= 0){
            // Stores the initial descent and computes the margin available
            initialDescent = fm.descent;
            extraSpace = rect.bottom - (fm.descent - fm.ascent);
        }

        fm.descent = extraSpace / 2 + initialDescent;
        fm.bottom = fm.descent;

        fm.ascent = -rect.bottom + fm.descent;
        fm.top = fm.ascent;
    }

    return rect.right;
}
 
Example 8
Project: mobile-store   File: CategorySpan.java   View Source Code Vote up 6 votes
@Override
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    CharSequence categoryName = getCategoryName(text, start, end);
    if (categoryName == null) {
        return 0;
    }

    float density = context.getResources().getDisplayMetrics().density;

    int iconBackgroundSize = (int) (ICON_BACKGROUND_SIZE * density);
    int textLeadingPadding = (int) (TEXT_LEADING_PADDING * density);
    int textWidth = (int) paint.measureText(categoryName.toString());
    int textTrailingPadding = (int) (TEXT_TRAILING_PADDING * density);
    int whiteSpacePadding = (int) (WHITE_SPACE_PADDING_AT_END * density);

    return iconBackgroundSize + textLeadingPadding + textWidth + textTrailingPadding + whiteSpacePadding;
}
 
Example 9
Project: qmui   File: QMUIAlignMiddleImageSpan.java   View Source Code Vote up 5 votes
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    if (mAvoidSuperChangeFontMetrics) {
        Drawable d = getDrawable();
        Rect rect = d.getBounds();
        mWidth = rect.right;
    } else {
        mWidth = super.getSize(paint, text, start, end, fm);
    }
    if (mFontWidthMultiple > 0) {
        mWidth = (int) (paint.measureText("子") * mFontWidthMultiple);
    }
    return mWidth;
}
 
Example 10
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 11
Project: weex-uikit   File: WXLineHeightSpan.java   View Source Code Vote up 5 votes
@Override
public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm) {
  if(WXEnvironment.isApkDebugable()) {
    WXLogUtils.d("LineHeight", text + " ; start " + start + "; end " + end + "; spanstartv "
            + spanstartv + "; v " + v + "; fm " + fm);
  }
  int halfLeading=(lineHeight-(fm.descent-fm.ascent))/2;
  fm.top-=halfLeading;
  fm.bottom+=halfLeading;
  fm.ascent-=halfLeading;
  fm.descent+=halfLeading;
}
 
Example 12
Project: RNLearn_Project1   File: InlineImageSpanWithPipeline.java   View Source Code Vote up 5 votes
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
  if (fm != null) {
    fm.ascent = -Math.round(mHeight);
    fm.descent = 0;

    fm.top = fm.ascent;
    fm.bottom = 0;
  }

  return Math.round(mWidth);
}
 
Example 13
Project: QMUI_Android   File: QMUIProgressBar.java   View Source Code Vote up 5 votes
private void drawRect(Canvas canvas) {
    canvas.drawRect(mBgRect, mBackgroundPaint);
    mProgressRect.set(getPaddingLeft(), getPaddingTop(), getPaddingLeft() + parseValueToWidth(), getPaddingTop() + mHeight);
    canvas.drawRect(mProgressRect, mPaint);
    if (mText != null && mText.length() > 0) {
        Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
        float baseline = mBgRect.top + (mBgRect.height() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
        canvas.drawText(mText, mBgRect.centerX(), baseline, mTextPaint);
    }
}
 
Example 14
Project: chilly   File: LoginFragment.java   View Source Code Vote up 5 votes
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_trakt_login, container, false);

        mErrorFrame = (ViewGroup) root.findViewById(android.support.v17.leanback.R.id.error_frame);
        updateBackground();

//        installTitleView(inflater, mErrorFrame, savedInstanceState);

        mImageView = (ImageView) root.findViewById(android.support.v17.leanback.R.id.image);
        setImageDrawable((getActivity().getResources().getDrawable(R.drawable.logo_trakt)));
        updateImageDrawable();

        mTextView = (TextView) root.findViewById(android.support.v17.leanback.R.id.message);
        updateMessage();

        mButton = (Button) root.findViewById(android.support.v17.leanback.R.id.button);
        updateButton();

        mSiteText = (TextView) root.findViewById(R.id.text_url);
        mCodeText = (TextView) root.findViewById(R.id.text_code);

        Paint.FontMetricsInt metrics = getFontMetricsInt(mTextView);
        int underImageBaselineMargin = container.getResources().getDimensionPixelSize(
                android.support.v17.leanback.R.dimen.lb_error_under_image_baseline_margin);
        setTopMargin(mTextView, underImageBaselineMargin + metrics.ascent);

        int underMessageBaselineMargin = container.getResources().getDimensionPixelSize(
                android.support.v17.leanback.R.dimen.lb_error_under_message_baseline_margin);
        setTopMargin(mButton, underMessageBaselineMargin - metrics.descent);

        return root;
    }
 
Example 15
Project: PlusGram   File: Emoji.java   View Source Code Vote up 5 votes
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    if (fm == null) {
        fm = new Paint.FontMetricsInt();
    }

    if (fontMetrics == null) {
        int sz = super.getSize(paint, text, start, end, fm);

        int offset = AndroidUtilities.dp(8);
        int w = AndroidUtilities.dp(10);
        fm.top = -w - offset;
        fm.bottom = w - offset;
        fm.ascent = -w - offset;
        fm.leading = 0;
        fm.descent = w - offset;

        return sz;
    } else {
        if (fm != null) {
            fm.ascent = fontMetrics.ascent;
            fm.descent = fontMetrics.descent;

            fm.top = fontMetrics.top;
            fm.bottom = fontMetrics.bottom;
        }
        if (getDrawable() != null) {
            getDrawable().setBounds(0, 0, size, size);
        }
        return size;
    }
}
 
Example 16
Project: PlusGram   File: Emoji.java   View Source Code Vote up 5 votes
public EmojiSpan(EmojiDrawable d, int verticalAlignment, int s, Paint.FontMetricsInt original) {
    super(d, verticalAlignment);
    fontMetrics = original;
    if (original != null) {
        size = Math.abs(fontMetrics.descent) + Math.abs(fontMetrics.ascent);
        if (size == 0) {
            size = AndroidUtilities.dp(20);
        }
    }
}
 
Example 17
Project: QMUI_Android   File: QMUIMarginImageSpan.java   View Source Code Vote up 5 votes
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    if (mSpanMarginLeft != 0 || mSpanMarginRight != 0) {
        super.getSize(paint, text, start, end, fm);
        Drawable d = getDrawable();
        return d.getIntrinsicWidth() + mSpanMarginLeft + mSpanMarginRight;
    } else {
        return super.getSize(paint, text, start, end, fm);
    }
}
 
Example 18
Project: airgram   File: Emoji.java   View Source Code Vote up 5 votes
public EmojiSpan(EmojiDrawable d, int verticalAlignment, int s, Paint.FontMetricsInt original) {
    super(d, verticalAlignment);
    fontMetrics = original;
    if (original != null) {
        size = Math.abs(fontMetrics.descent) + Math.abs(fontMetrics.ascent);
        if (size == 0) {
            size = AndroidUtilities.dp(20);
        }
    }
}
 
Example 19
Project: MontageCam   File: TextStickerView.java   View Source Code Vote up 4 votes
public void drawText(Canvas canvas, int _x, int _y, float scale, float rotate) {
    if (ListUtil.isEmpty(mTextContents))
        return;

    int x = _x;
    int y = _y;
    int text_height = 0;

    mTextRect.set(0, 0, 0, 0);//clear
    Rect tempRect = new Rect();
    Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
    int charMinHeight = Math.abs(fontMetrics.top) + Math.abs(fontMetrics.bottom);//字体高度
    text_height = charMinHeight;
    //System.out.println("top = "+fontMetrics.top +"   bottom = "+fontMetrics.bottom);
    for (int i = 0; i < mTextContents.size(); i++) {
        String text = mTextContents.get(i);
        mPaint.getTextBounds(text, 0, text.length(), tempRect);
        //System.out.println(i + " ---> " + tempRect.height());
        //text_height = Math.max(charMinHeight, tempRect.height());
        if (tempRect.height() <= 0) {//处理此行文字为空的情况
            tempRect.set(0, 0, 0, text_height);
        }

        RectUtil.rectAddV(mTextRect, tempRect, 0, charMinHeight);
    }//end for i

    mTextRect.offset(x, y);

    mHelpBoxRect.set(mTextRect.left - PADDING, mTextRect.top - PADDING
            , mTextRect.right + PADDING, mTextRect.bottom + PADDING);
    RectUtil.scaleRect(mHelpBoxRect, scale);

    canvas.save();
    canvas.scale(scale, scale, mHelpBoxRect.centerX(), mHelpBoxRect.centerY());
    canvas.rotate(rotate, mHelpBoxRect.centerX(), mHelpBoxRect.centerY());

    //canvas.drawRect(mTextRect, debugPaint);
    //float left = mHelpBoxRect.left - mTextRect.left;
    //float right = mHelpBoxRect.right - mTextRect.right;

    //System.out.println("left = "+left +"   right = "+right);
    int draw_text_y = y + (text_height >> 1) + PADDING;
    for (int i = 0; i < mTextContents.size(); i++) {
        canvas.drawText(mTextContents.get(i), x, draw_text_y, mPaint);
        draw_text_y += text_height;
    }//end for i
    canvas.restore();
}
 
Example 20
Project: AndroidUtilCode-master   File: SpanUtils.java   View Source Code Vote up 4 votes
public void chooseHeight(CharSequence text, int start, int end, int istartv, int v, Paint.FontMetricsInt fm) {
    if (lineHeight == 0) {
        lineHeight = v - istartv;
    }
    if (need0 == 0 && end == ((Spanned) text).getSpanEnd(this)) {
        int ht = mBitmap.getHeight();
        need0 = ht - (v + fm.descent - fm.ascent - istartv);
        need1 = ht - (v + fm.bottom - fm.top - istartv);
        totalHeight = v - istartv + lineHeight;
        return;
    }
    if (need0 > 0 || need1 > 0) {
        if (mVerticalAlignment == ALIGN_TOP) {
            // the rest space should be filled with the end of line
            if (end == ((Spanned) text).getSpanEnd(this)) {
                if (need0 > 0) fm.descent += need0;
                if (need1 > 0) fm.bottom += need1;
            }
        } else if (mVerticalAlignment == ALIGN_CENTER) {
            if (start == ((Spanned) text).getSpanStart(this)) {
                if (need0 > 0) fm.ascent -= need0 / 2;
                if (need1 > 0) fm.top -= need1 / 2;
            } else {
                if (!flag) {
                    if (need0 > 0) fm.ascent += need0 / 2;
                    if (need1 > 0) fm.top += need1 / 2;
                    flag = true;
                }
            }
            if (end == ((Spanned) text).getSpanEnd(this)) {
                if (need0 > 0) fm.descent += need0 / 2;
                if (need1 > 0) fm.bottom += need1 / 2;
            }
        } else {
            // the top space should be filled with the first of line
            if (start == ((Spanned) text).getSpanStart(this)) {
                if (need0 > 0) fm.ascent -= need0;
                if (need1 > 0) fm.top -= need1;
            } else {
                if (!flag) {
                    if (need0 > 0) fm.ascent += need0;
                    if (need1 > 0) fm.top += need1;
                    flag = true;
                }
            }
        }
    }
}