Java Code Examples for android.graphics.Paint#FontMetrics

The following examples show how to use android.graphics.Paint#FontMetrics . 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: CommaMonthView.java    From android with MIT License 6 votes vote down vote up
public CommaMonthView(Context context) {
    super(context);

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

    mSchemeBasicPaint.setAntiAlias(true);
    mSchemeBasicPaint.setStyle(Paint.Style.FILL);
    mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
    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);

    //兼容硬件加速无效的代码
    setLayerType(View.LAYER_TYPE_SOFTWARE, mSchemeBasicPaint);
    //4.0以上硬件加速会导致无效
    mSchemeBasicPaint.setMaskFilter(new BlurMaskFilter(25, BlurMaskFilter.Blur.SOLID));
}
 
Example 2
Source File: IndexBar.java    From ContactsList with Apache License 2.0 6 votes vote down vote up
private int measureHeight(int heightMeasureSpec) {
    int result;
    int specMode = MeasureSpec.getMode(heightMeasureSpec);
    int specSize = MeasureSpec.getSize(heightMeasureSpec);
    if (specMode == MeasureSpec.EXACTLY) {
        result = specSize;
    } else {
        Paint.FontMetrics fm = mPaint.getFontMetrics();
        float singleHeight = fm.bottom - fm.top;
        mBaseLineHeight = fm.bottom * mLetterSpacingExtra;
        result = (int) (mNavigators.size() * singleHeight * mLetterSpacingExtra);
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return result;
}
 
Example 3
Source File: ClockPieView.java    From AndroidCharts with MIT License 5 votes vote down vote up
public ClockPieView(Context context, AttributeSet attrs) {
    super(context, attrs);
    textSize = MyUtils.sp2px(context, 15);
    lineThickness = MyUtils.dip2px(context, 1);
    lineLength = MyUtils.dip2px(context, 10);

    textPaint = new Paint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(TEXT_COLOR);
    textPaint.setTextSize(textSize);
    textPaint.setTextAlign(Paint.Align.CENTER);
    Paint.FontMetrics fm = new Paint.FontMetrics();
    textPaint.getFontMetrics(fm);
    textRect = new Rect();
    textPaint.getTextBounds("18", 0, 1, textRect);
    redPaint = new Paint(textPaint);
    redPaint.setColor(RED_COLOR);
    linePaint = new Paint(textPaint);
    linePaint.setColor(GRAY_COLOR);
    linePaint.setStrokeWidth(lineThickness);
    whitePaint = new Paint(linePaint);
    whitePaint.setColor(Color.WHITE);
    tempPoint = new Point();
    pieCenterPoint = new Point();
    tempPointRight = new Point();
    cirRect = new RectF();
    leftTextWidth = textPaint.measureText("18");
    rightTextWidth = textPaint.measureText("6");
    topTextHeight = textRect.height();
}
 
Example 4
Source File: EmojiMaterialRadioButton.java    From Emoji with Apache License 2.0 5 votes vote down vote up
@Override @CallSuper public void setText(final CharSequence rawText, final BufferType type) {
  final CharSequence text = rawText == null ? "" : rawText;
  final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
  final Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
  final float defaultEmojiSize = fontMetrics.descent - fontMetrics.ascent;
  EmojiManager.getInstance().replaceWithImages(getContext(), spannableStringBuilder, emojiSize, defaultEmojiSize);
  super.setText(spannableStringBuilder, type);
}
 
Example 5
Source File: AnswerTextBuilder.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor for TopAlignedSpan.
 *
 * @param textHeightSp The total height in SP of the text covered by this span.
 * @param maxTextHeightSp The total height in SP of the text we wish to top-align with.
 * @param metrics The font metrics used to determine what proportion of the font height is
 *                the ascent.
 * @param density The display density.
 */
public TopAlignedSpan(
        int textHeightSp, int maxTextHeightSp, Paint.FontMetrics metrics, float density) {
    float ascentProportion = metrics.ascent / (metrics.top - metrics.bottom);

    int textAscentPx = (int) (textHeightSp * ascentProportion * density);
    int maxTextAscentPx = (int) (maxTextHeightSp * ascentProportion * density);

    this.mBaselineShift = -(maxTextAscentPx - textAscentPx);  // Up is -y.
}
 
Example 6
Source File: WaveSideBarView.java    From GetApk with MIT License 5 votes vote down vote up
private void drawLetters(Canvas canvas) {
    canvas.save();
    canvas.translate(mTranslateX, 0);
    mLettersPaint.reset();
    mLettersPaint.setStyle(Paint.Style.FILL);
    mLettersPaint.setColor(mBarColor);
    mLettersPaint.setAntiAlias(true);
    canvas.drawRoundRect(mLettersRect, mTextSize, mTextSize, mLettersPaint);

    mLettersPaint.reset();
    mLettersPaint.setStyle(Paint.Style.STROKE);
    mLettersPaint.setColor(mTextColor);
    mLettersPaint.setAntiAlias(true);
    canvas.drawRoundRect(mLettersRect, mTextSize, mTextSize, mLettersPaint);

    for (int i = 0; i < mLetters.size(); i++) {
        mLettersPaint.reset();
        mLettersPaint.setColor(mTextColor);
        mLettersPaint.setAntiAlias(true);
        mLettersPaint.setTextSize(mTextSize);
        mLettersPaint.setTextAlign(Paint.Align.CENTER);

        Paint.FontMetrics fontMetrics = mLettersPaint.getFontMetrics();
        float baseline = Math.abs(-fontMetrics.bottom - fontMetrics.top);

        float posY = mItemHeight * i + baseline / 2 + mPadding;

        if (i == mChoose) {
            mPosY = posY;
        } else {
            canvas.drawText(mLetters.get(i), mPosX, posY, mLettersPaint);
        }
    }
    canvas.restore();
}
 
Example 7
Source File: RongGenerate.java    From LQRWeChat with MIT License 5 votes vote down vote up
public static String generateDefaultAvatar(String username, String userid) {

        String s = null;
        if (!TextUtils.isEmpty(username)) {
            s = String.valueOf(username.charAt(0));
        }
        if (s == null) {
            s = "A";
        }
        String color = getColorRGB(userid);
        String string = getAllFirstLetter(username);
        createDir(SAVEADDRESS);
        File f = new File(SAVEADDRESS, string + "_" + userid);
        if (f.exists()) {
            return SCHEMA + f.getPath();
        }
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(220);
        paint.setAntiAlias(true);
        int width = 480;
        int height = 480;
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.parseColor(color));
        Rect rect = new Rect();
        paint.getTextBounds(s, 0, s.length(), rect);
        Paint.FontMetrics fm = paint.getFontMetrics();
        int textLeft = (int) ((width - paint.measureText(s)) / 2);
        int textTop = (int) (height - width / 2 + Math.abs(fm.ascent) / 2 - 25);
        canvas.drawText(s, textLeft, textTop, paint);
        return saveBitmap(bitmap, string + "_" + userid);
    }
 
Example 8
Source File: Ruler.java    From DS4Android with MIT License 5 votes vote down vote up
public static void handleTextByPos(Object str, Paint paint, Canvas canvas, int x, int y, Rect rect) {
    String res = str.toString();
    paint.getTextBounds(res, 0, res.length(), rect);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setColor(Color.BLACK);
    Paint.FontMetrics fm = paint.getFontMetrics();
    rect.offset(-rect.width() / 2 + x, y);


    canvas.drawRect(rect, paint);

    paint.setColor(Color.WHITE);
    canvas.drawText(res, x, y, paint);

}
 
Example 9
Source File: SubtitleLayer.java    From PhotoMovie with Apache License 2.0 5 votes vote down vote up
private Bitmap genBitmapFromStr(String str, TextPaint textPaint, float density) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }

        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        int h = (int) (Math.abs(fontMetrics.ascent) + Math.abs(fontMetrics.descent));
        int w = (int) textPaint.measureText(str);
        Bitmap bitmap = PhotoUtil.createBitmapOrNull(w, h, Bitmap.Config.ARGB_4444);
        if (bitmap == null) {
            return null;
        }
        Canvas canvas = new Canvas(bitmap);
        // 描外层
        textPaint.setColor(0xFF343434);
        textPaint.setStrokeWidth(density * STROKE_WIDTH_DP + 0.5f);  // 描边宽度
        textPaint.setStyle(Paint.Style.FILL_AND_STROKE); //描边种类
        textPaint.setFakeBoldText(true); // 外层text采用粗体
//        textPaint.setShadowLayer(1, 0, 0, 0); //字体的阴影效果,可以忽略
        canvas.drawText(str, 0, Math.abs(fontMetrics.ascent), textPaint);

        // 描内层,恢复原先的画笔
        textPaint.setColor(0xFFFFFFFF);
        textPaint.setStrokeWidth(0);
        textPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        textPaint.setFakeBoldText(false);
//        textPaint.setShadowLayer(0, 0, 0, 0);
        canvas.drawText(str, 0, Math.abs(fontMetrics.ascent), textPaint);

        return bitmap;
    }
 
Example 10
Source File: CircleProgressBar.java    From CircleProgressBar with Apache License 2.0 5 votes vote down vote up
private void drawProgressText(Canvas canvas) {
    if (!mCpbNeedShowText) return;
    String text = String.valueOf(mCurrentProgress);
    int textSize = (int) ((mMinWidth - mCpbStrokeWidth * 2) / 2.5);
    mCpbTextPaint.setTextSize(textSize);
    Paint.FontMetrics fm = mCpbTextPaint.getFontMetrics();
    float textHeight = (float) Math.ceil(fm.descent - fm.top);
    float textWidth = mCpbTextPaint.measureText(text);
    float x = (mMinWidth - textWidth) / 2;
    float y = (mMinWidth - textHeight) / 2 + textSize;
    canvas.drawText(text, x, y, mCpbTextPaint);
}
 
Example 11
Source File: Slider.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private void updateTextSize() {
    int length = CHARACTERS.length;
    float[] widths = new float[length];
    mPaint.getTextWidths(CHARACTERS, 0, length, widths);
    mCharWidth = 0.0f;
    for (float f : widths) {
        mCharWidth = Math.max(mCharWidth, f);
    }

    Paint.FontMetrics fm = mPaint.getFontMetrics();
    mCharHeight = fm.bottom - fm.top;
}
 
Example 12
Source File: QrCodeFinderView.java    From QrCodeScanner with GNU General Public License v3.0 5 votes vote down vote up
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.qr_code_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 13
Source File: EmoticonsEditText.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
private int getFontHeight() {
    Paint paint = new Paint();
    paint.setTextSize(getTextSize());
    Paint.FontMetrics fm = paint.getFontMetrics();
    return (int) Math.ceil(fm.bottom - fm.top);
}
 
Example 14
Source File: MyDecorationThree.java    From MaterialDesignDemo with MIT License 4 votes vote down vote up
/**
 * 返回指定笔的文字高度
 */
private float getFontHeight(Paint paint) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    return fm.descent - fm.ascent;
}
 
Example 15
Source File: BaseView.java    From kAndroid with Apache License 2.0 4 votes vote down vote up
protected float getFontHeight(Paint paint) {
    Paint.FontMetrics fm = paint.getFontMetrics();
    float textHeight = fm.descent - fm.ascent;
    return textHeight;
}
 
Example 16
Source File: JPTabItem.java    From JPTabBar with Apache License 2.0 4 votes vote down vote up
/**
 * 得到字体的X轴坐标
 */
private float getTextY(Rect textBound, Paint.FontMetrics fontMetrics) {
    return (getMeasuredHeight() - mMargin - textBound.height() / 2f - fontMetrics.descent + (fontMetrics.descent - fontMetrics.ascent) / 2);
}
 
Example 17
Source File: HappyNewYear2019.java    From customview-samples with Apache License 2.0 4 votes vote down vote up
private int getBaseLine(){
    Paint.FontMetrics fontMetrics = mBubbleTextPaint.getFontMetrics();
    return (int) ((fontMetrics.descent - fontMetrics.ascent) - fontMetrics.descent);
}
 
Example 18
Source File: MaterialSpinner.java    From XERUNG with Apache License 2.0 4 votes vote down vote up
private void updateBottomPadding() {
    Paint.FontMetrics textMetrics = textPaint.getFontMetrics();
    extraPaddingBottom = (int) ((textMetrics.descent - textMetrics.ascent) * currentNbErrorLines) + underlineTopSpacing + underlineBottomSpacing;
    setPadding();
}
 
Example 19
Source File: LemonBubbleInfo.java    From LemonBubble4Android with MIT License 4 votes vote down vote up
int getLineHeight(TextView textView) {
    Paint.FontMetrics fontMetrics = textView.getPaint().getFontMetrics();
    return _PST.pxToDp((int) (fontMetrics.descent - fontMetrics.top)) + 2;
}
 
Example 20
Source File: CreditScoreView.java    From SesameCreditScore with Apache License 2.0 2 votes vote down vote up
/**
 * 获取文本的高度
 *
 * @param paint 文本绘制的画笔
 * @return 文本高度
 */
private int getTextHeight(Paint paint) {
    Paint.FontMetrics fontMetrics = paint.getFontMetrics();
    return (int) (fontMetrics.descent - fontMetrics.ascent);
}