Java Code Examples for android.text.TextPaint#getFontMetricsInt()

The following examples show how to use android.text.TextPaint#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: ContactChipDrawable.java    From MDPreference with Apache License 2.0 6 votes vote down vote up
public ContactChipDrawable(int paddingLeft, int paddingRight, Typeface typeface, int textColor, int textSize, int backgroundColor) {
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(textColor);
    mPaint.setTypeface(typeface);
    mPaint.setTextSize(textSize);

    mTextPaint = new TextPaint(mPaint);
    mMetrics = new BoringLayout.Metrics();
    Paint.FontMetricsInt temp = mTextPaint.getFontMetricsInt();
    mMetrics.ascent = temp.ascent;
    mMetrics.bottom = temp.bottom;
    mMetrics.descent = temp.descent;
    mMetrics.top = temp.top;
    mMetrics.leading = temp.leading;

    mRect = new RectF();

    mMatrix = new Matrix();

    mPaddingLeft = paddingLeft;
    mPaddingRight = paddingRight;
    mBackgroundColor = backgroundColor;
}
 
Example 2
Source File: ContactChipDrawable.java    From material with Apache License 2.0 6 votes vote down vote up
public ContactChipDrawable(int paddingLeft, int paddingRight, Typeface typeface, int textColor, int textSize, int backgroundColor) {
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(textColor);
    mPaint.setTypeface(typeface);
    mPaint.setTextSize(textSize);

    mTextPaint = new TextPaint(mPaint);
    mMetrics = new BoringLayout.Metrics();
    Paint.FontMetricsInt temp = mTextPaint.getFontMetricsInt();
    mMetrics.ascent = temp.ascent;
    mMetrics.bottom = temp.bottom;
    mMetrics.descent = temp.descent;
    mMetrics.top = temp.top;
    mMetrics.leading = temp.leading;

    mRect = new RectF();

    mMatrix = new Matrix();

    mPaddingLeft = paddingLeft;
    mPaddingRight = paddingRight;
    mBackgroundColor = backgroundColor;
}
 
Example 3
Source File: StringTexture.java    From document-viewer with GNU General Public License v3.0 6 votes vote down vote up
public void setText(final String text, final TextPaint paint) {
    final String oldText = mText;
    final int oldTextHeight = mTextHeight;

    mPaint = paint;
    mMetrics = paint.getFontMetricsInt();
    mText = text;
    mTextWidth = (int) Math.ceil(mPaint.measureText(mText));
    mTextHeight = mMetrics.bottom - mMetrics.top;

    if (mTextWidth > mCanvasWidth) {
        mText = TextUtils.ellipsize(mText, mPaint, mCanvasWidth, TextUtils.TruncateAt.END).toString();
        mTextWidth = (int) Math.ceil(mPaint.measureText(mText));
    }
    if (!CompareUtils.equals(mText, oldText) || mTextHeight != oldTextHeight) {
        if (mBitmap != null) {
            mBitmap.eraseColor(0);
        }
        mContentValid = false;
        mWidth = UNSPECIFIED;
        mHeight = UNSPECIFIED;
    }
}
 
Example 4
Source File: Styled.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
/**
     * Returns the advance widths for a uniform left-to-right run of text with
     * no style changes in the middle of the run. If any style is replacement
     * text, the first character will get the width of the replacement and the
     * remaining characters will get a width of 0.
     * 
     * @param paint the paint, will not be modified
     * @param workPaint a paint to modify; on return will reflect the original
     *        paint plus the effect of all spans on the run
     * @param text the text
     * @param start the start of the run
     * @param end the limit of the run
     * @param widths array to receive the advance widths of the characters. Must
     *        be at least a large as (end - start).
     * @param fmi FontMetrics information; can be null
     * @return the actual number of widths returned
     */
    public static int getTextWidths(TextPaint paint,
                                    TextPaint workPaint,
                                    Spanned text, int start, int end,
                                    float[] widths, Paint.FontMetricsInt fmi) {
// Jota Text Editor
//        MetricAffectingSpan[] spans =
//            text.getSpans(start, end, MetricAffectingSpan.class);
//
//		ReplacementSpan replacement = null;
        workPaint.set(paint);
//
//		for (int i = 0; i < spans.length; i++) {
//			MetricAffectingSpan span = spans[i];
//			if (span instanceof ReplacementSpan) {
//				replacement = (ReplacementSpan)span;
//			}
//			else {
//				span.updateMeasureState(workPaint);
//			}
//		}
//
//        if (replacement == null) {
            workPaint.getFontMetricsInt(fmi);
            workPaint.getTextWidths(text, start, end, widths);
//        } else {
//            int wid = replacement.getSize(workPaint, text, start, end, fmi);
//
//            if (end > start) {
//                widths[0] = wid;
//                for (int i = start + 1; i < end; i++)
//                    widths[i - start] = 0;
//            }
//        }
        return end - start;
    }
 
Example 5
Source File: ThermometerView.java    From ThermometerView with MIT License 5 votes vote down vote up
public void initConfig() {
    if (minThermometerRadius >= maxThermometerRadius) {
        throw new UnsupportedOperationException("温度计形状设置有误。");
    }
    if (minMercuryRadius >= maxMercuryRadius || minMercuryRadius >= minThermometerRadius) {
        throw new UnsupportedOperationException("水银形状设置有误。");
    }
    if (minScaleValue >= maxScaleValue) {
        throw new UnsupportedOperationException("刻度值设置不正确");
    }
    setResetCurValue(curScaleValue);

    sumScaleValue = (maxScaleValue - minScaleValue) * 10;

    mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);

    mLinePaint = new Paint();
    mLinePaint.setAntiAlias(true);
    mLinePaint.setStyle(Paint.Style.FILL);
    mLinePaint.setStrokeWidth(scaleLineWidth);

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setStyle(Paint.Style.FILL);

    String title = "℃";
    mTextPaint.setTextSize(unitTextSize);
    textWidth = Layout.getDesiredWidth(title, mTextPaint);
    Paint.FontMetricsInt fmi = mTextPaint.getFontMetricsInt();
    titleHeight = -(float) (fmi.bottom + fmi.top);

    this.setLayerType(View.LAYER_TYPE_SOFTWARE, null); // 关闭硬件加速,否则阴影无效
}
 
Example 6
Source File: ContactChipSpan.java    From MDPreference with Apache License 2.0 5 votes vote down vote up
public ContactChipSpan(CharSequence name, int height, int maxWidth, int paddingLeft, int paddingRight, Typeface typeface, int textColor, int textSize, int backgroundColor) {
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(textColor);
    mPaint.setTypeface(typeface);
    mPaint.setTextSize(textSize);

    mTextPaint = new TextPaint(mPaint);


    mRect = new RectF();

    mMatrix = new Matrix();

    mContactName = name;
    mPaddingLeft = paddingLeft;
    mPaddingRight = paddingRight;
    mBackgroundColor = backgroundColor;
    mHeight = height;
    mWidth = Math.round(Math.min(maxWidth, mPaint.measureText(name, 0, name.length()) + paddingLeft + paddingRight + height));

    int outerWidth = Math.max(0, mWidth - mPaddingLeft - mPaddingRight - mHeight);
    Paint.FontMetricsInt temp = mTextPaint.getFontMetricsInt();
    BoringLayout.Metrics mMetrics = new BoringLayout.Metrics();
    mMetrics.width = Math.round(mTextPaint.measureText(mContactName, 0, mContactName.length()) + 0.5f);
    mMetrics.ascent = temp.ascent;
    mMetrics.bottom = temp.bottom;
    mMetrics.descent = temp.descent;
    mMetrics.top = temp.top;
    mMetrics.leading = temp.leading;
    mBoringLayout = BoringLayout.make(mContactName, mTextPaint, outerWidth, Layout.Alignment.ALIGN_NORMAL, 1f, 1f, mMetrics, true, TextUtils.TruncateAt.END, outerWidth);
}
 
Example 7
Source File: Styled.java    From JotaTextEditor with Apache License 2.0 5 votes vote down vote up
/**
     * Returns the advance widths for a uniform left-to-right run of text with
     * no style changes in the middle of the run. If any style is replacement
     * text, the first character will get the width of the replacement and the
     * remaining characters will get a width of 0.
     * 
     * @param paint the paint, will not be modified
     * @param workPaint a paint to modify; on return will reflect the original
     *        paint plus the effect of all spans on the run
     * @param text the text
     * @param start the start of the run
     * @param end the limit of the run
     * @param widths array to receive the advance widths of the characters. Must
     *        be at least a large as (end - start).
     * @param fmi FontMetrics information; can be null
     * @return the actual number of widths returned
     */
    public static int getTextWidths(TextPaint paint,
                                    TextPaint workPaint,
                                    Spanned text, int start, int end,
                                    float[] widths, Paint.FontMetricsInt fmi) {
// Jota Text Editor
//        MetricAffectingSpan[] spans =
//            text.getSpans(start, end, MetricAffectingSpan.class);
//
//		ReplacementSpan replacement = null;
        workPaint.set(paint);
//
//		for (int i = 0; i < spans.length; i++) {
//			MetricAffectingSpan span = spans[i];
//			if (span instanceof ReplacementSpan) {
//				replacement = (ReplacementSpan)span;
//			}
//			else {
//				span.updateMeasureState(workPaint);
//			}
//		}
//
//        if (replacement == null) {
            workPaint.getFontMetricsInt(fmi);
            workPaint.getTextWidths(text, start, end, widths);
//        } else {
//            int wid = replacement.getSize(workPaint, text, start, end, fmi);
//
//            if (end > start) {
//                widths[0] = wid;
//                for (int i = start + 1; i < end; i++)
//                    widths[i - start] = 0;
//            }
//        }
        return end - start;
    }
 
Example 8
Source File: ContactChipSpan.java    From material with Apache License 2.0 5 votes vote down vote up
public ContactChipSpan(CharSequence name, int height, int maxWidth, int paddingLeft, int paddingRight, Typeface typeface, int textColor, int textSize, int backgroundColor) {
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(textColor);
    mPaint.setTypeface(typeface);
    mPaint.setTextSize(textSize);

    mTextPaint = new TextPaint(mPaint);


    mRect = new RectF();

    mMatrix = new Matrix();

    mContactName = name;
    mPaddingLeft = paddingLeft;
    mPaddingRight = paddingRight;
    mBackgroundColor = backgroundColor;
    mHeight = height;
    mWidth = Math.round(Math.min(maxWidth, mPaint.measureText(name, 0, name.length()) + paddingLeft + paddingRight + height));

    int outerWidth = Math.max(0, mWidth - mPaddingLeft - mPaddingRight - mHeight);
    Paint.FontMetricsInt temp = mTextPaint.getFontMetricsInt();
    BoringLayout.Metrics mMetrics = new BoringLayout.Metrics();
    mMetrics.width = Math.round(mTextPaint.measureText(mContactName, 0, mContactName.length()) + 0.5f);
    mMetrics.ascent = temp.ascent;
    mMetrics.bottom = temp.bottom;
    mMetrics.descent = temp.descent;
    mMetrics.top = temp.top;
    mMetrics.leading = temp.leading;
    mBoringLayout = BoringLayout.make(mContactName, mTextPaint, outerWidth, Layout.Alignment.ALIGN_NORMAL, 1f, 1f, mMetrics, true, TextUtils.TruncateAt.END, outerWidth);
}
 
Example 9
Source File: WaveTextView.java    From WanAndroid with Apache License 2.0 4 votes vote down vote up
private void init(Context context, AttributeSet attrs) {
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WaveTextView);
    mText = typedArray.getString(R.styleable.WaveTextView_text);
    mTextSize = typedArray.getDimension(R.styleable.WaveTextView_size, 26);
    mTextColor = typedArray.getColor(R.styleable.WaveTextView_color, Color.BLACK);
    A = typedArray.getInteger(R.styleable.WaveTextView_amplitude, 100);
    isBold = typedArray.getInteger(R.styleable.WaveTextView_style, 0);
    typedArray.recycle();

    mTextPaint = new TextPaint();
    mTextPaint.setAntiAlias(true);
    mTextPaint.setTextSize(mTextSize);
    mTextPaint.setColor(mTextColor);
    if(isBold == 1) mTextPaint.setFakeBoldText(true);
    //mTextPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    mTextPath = new Path();
    mTextWidth = (int) mTextPaint.measureText(mText);
    Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
    mTextHeight = fontMetrics.bottom - fontMetrics.top;
    Rect rect = new Rect();
    mTextPaint.getTextBounds(mText, 0, mText.length(), rect);
    mTextCenter = rect.height() / 2;

    //φ的取值范围为正弦函数的一个周期:2π
    mValueAnimator = ValueAnimator.ofFloat(0, (float) (2 * Math.PI));
    mValueAnimator.setInterpolator(new LinearInterpolator());
    mValueAnimator.addUpdateListener(animation -> {
        float progress = (float) animation.getAnimatedValue();
        φ = progress;
        a = (float) (1 - progress / (2 * Math.PI)) * A;
        LogUtil.d(TAG, "addUpdateListener(), a = " + a + ", φ = " + φ);
        postInvalidate();
    });
    mValueAnimator.setDuration(2500);
    postDelayed(() -> mValueAnimator.start(), 100);

    for(int i = 0; i <= SAMPLING_SIZE; i++) {
        float gap = (float) mTextWidth / SAMPLING_SIZE;
        mSamplingX[i] = i * gap;
        mMapX[i] = mSamplingX[i] / (float) mTextWidth * 4 - 2;
    }
}