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

The following examples show how to use android.graphics.Paint#getTextSize() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: SimplifySpan   File: CustomLabelSpan.java    License: MIT License 6 votes vote down vote up
private float initFinalWidth(Paint paint) {
    if (mFinalWidth <= 0) {
        float labelTextSize = mSpecialLabelUnit.getLabelTextSize();
        if (labelTextSize > 0 && labelTextSize != paint.getTextSize()) {
            paint.setTextSize(labelTextSize);
        }

        int labelBgWidth = mSpecialLabelUnit.getLabelBgWidth();
        mSpecialTextWidth = paint.measureText(mSpecialText, 0, mSpecialText.length());
        if (labelBgWidth > 0 && labelBgWidth > mSpecialTextWidth) {
            mFinalWidth = labelBgWidth;
        } else {
            mFinalWidth = mSpecialTextWidth + mPaddingLeft + mPaddingRight;
        }
    }

    return mFinalWidth;
}
 
Example 2
Source Project: CommonUtils   File: DrawingHelper.java    License: Apache License 2.0 6 votes vote down vote up
public void draw(@NonNull String letters, boolean circle, Canvas canvas) {
    int cx = canvas.getWidth() / 2;
    int cy = canvas.getHeight() / 2;
    int r = Math.min(cx, cy);

    float reduce;
    Paint lettersPaint;
    if (circle) {
        reduce = REDUCE_FACTOR_CIRCLE;
        r = r - SHADOW_RADIUS;
        canvas.drawCircle(cx, cy, r, shapePaint);
        lettersPaint = lettersPaintWithCircle;
    } else {
        reduce = REDUCE_FACTOR_NO_CIRCLE;
        lettersPaint = lettersPaintNoCircle;
    }

    lettersPaint.getTextBounds(letters, 0, letters.length(), lettersBounds);
    float factor = hypotenuse(lettersBounds.width() / 2, lettersBounds.height() / 2) / lettersPaint.getTextSize();
    lettersPaint.setTextSize(r / factor * reduce);
    lettersPaint.getTextBounds(letters, 0, letters.length(), lettersBounds);

    canvas.drawText(letters, cx - lettersBounds.exactCenterX(), cy - lettersBounds.exactCenterY(), lettersPaint);
}
 
Example 3
public float calculateMatchWidthSize(Paint paint,String text,int maxWidth){
    float textSize = paint.getTextSize();
    float width = paint.measureText(text);
    Log.d("hyh", "TextSizeAdjustHelper: calculateMatchWidthSize: width="+width+" ,maxWidth="+maxWidth);
    if(maxWidth >= width && maxWidth - width <= text.length()){
        Log.d("hyh", "TextSizeAdjustHelper: calculateMatchWidthSize: return 1 textSize="+textSize);
        return textSize;
    }

    if(width > maxWidth){
        textSize = getNarrowFitTextSize(paint,text,maxWidth,1);
    }else{
        textSize = getZoomFitTextSize(paint,text,maxWidth,1);
    }
    Log.d("hyh", "TextSizeAdjustHelper: calculateMatchWidthSize: return 2 textSize="+textSize);
    return textSize;
}
 
Example 4
Source Project: Pioneer   File: Spans.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void draw(@NonNull Rect outRect, @NonNull Canvas canvas, CharSequence text, @IntRange(from = 0) int start, @IntRange(from = 0) int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
    float oldStrokeWidth = paint.getStrokeWidth();
    Paint.Style oldStyle = paint.getStyle();
    int oldColor = paint.getColor();
    paint.setStrokeWidth(strokeWidth);
    paint.setStyle(style);
    paint.setColor(color);
    canvas.save();
    if (includePad) {
        canvas.translate(x + strokeWidth / 2, strokeWidth / 2);
    } else {
        canvas.translate(x + strokeWidth / 2, fontMetricsInt.ascent - fontMetricsInt.top + strokeWidth / 2);
    }
    shape.draw(canvas, paint);
    canvas.restore();
    paint.setColor(oldColor);
    paint.setStyle(oldStyle);
    paint.setStrokeWidth(oldStrokeWidth);

    float oldTextSize = paint.getTextSize();
    paint.setTextSize(oldTextSize * 0.8f);
    super.draw(outRect, canvas, text, start, end, x, top, y, bottom, paint);
    paint.setTextSize(oldTextSize);
}
 
Example 5
private float getZoomFitTextSize(Paint paint,String text,int maxWidth,float rate){
    float textSize = paint.getTextSize();
    textSize += 1 * rate;
    paint.setTextSize(textSize);
    float width = paint.measureText(text);
    Log.d("hyh", "TextSizeAdjustHelper: getZoomFitTextSize: width="+width);
    if(maxWidth >= width && maxWidth - width <= text.length()){
        return textSize;
    }
    //结束条件
    if(width < maxWidth){
        return getZoomFitTextSize(paint,text,maxWidth,rate);
    }else{
        return getNarrowFitTextSize(paint,text,maxWidth,rate);
    }
}
 
Example 6
private void drawText(float x, float y, Note note, Paint textPaint) {
    String noteText = getNote(note.getName());
    float offset = textPaint.measureText(noteText) / 2F;

    String sign = note.getSign();
    String octave = String.valueOf(getOctave(note.getOctave()));

    TextPaint paint = new TextPaint(ANTI_ALIAS_FLAG);
    paint.setColor(textColor);
    int textSize = (int) (textPaint.getTextSize() / 2);
    paint.setTextSize(textSize);

    float factor = 0.75f;
    if (useScientificNotation) {
        factor = 1.5f;
    }

    canvas.drawText(sign, x + offset * 1.25f, y - offset * factor, paint);
    canvas.drawText(octave, x + offset * 1.25f, y + offset * 0.5f, paint);

    canvas.drawText(noteText, x - offset, y, textPaint);
}
 
Example 7
Source Project: memoir   File: NumberSpan.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom,
                              CharSequence text, int start, int end, boolean first, Layout l) {

    Spanned spanned = (Spanned) text;
    if (!mIgnoreSpan && spanned.getSpanStart(this) == start) {
        // set paint
        Paint.Style oldStyle = p.getStyle();
        float oldTextSize = p.getTextSize();
        p.setStyle(Paint.Style.FILL);
        mTextSize = baseline - top;
        p.setTextSize(mTextSize);
        mWidth = p.measureText(mNr + ".");

        // draw the number
        c.drawText(mNr + ".", x, baseline, p);

        // restore paint
        p.setStyle(oldStyle);
        p.setTextSize(oldTextSize);
    }
}
 
Example 8
Source Project: Markdown   File: CodeSpan.java    License: 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 9
Source Project: Markdown   File: CodeSpan.java    License: MIT License 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) {
    float size = paint.getTextSize();
    paint.setTextSize(size * TEXT_SIZE_SCALE);
    paint.setTypeface(Typeface.MONOSPACE);

    mBackground.setBounds((int) (x + MARGIN), (int) (top - PADDING_VERTICAL), (int) (x + mWidth - MARGIN), (int) (top + mHeight + PADDING_VERTICAL));
    mBackground.draw(canvas);

    int color = paint.getColor();
    paint.setColor(mTextColor);
    canvas.drawText(text, start, end, x + MARGIN + PADDING_HORIZONTAL, y - mHeight * (1 - TEXT_SIZE_SCALE) * 0.5f, paint);
    paint.setColor(color);

    paint.setTextSize(size);
}
 
Example 10
Source Project: Markdown   File: CodeBlockSpan.java    License: MIT License 6 votes vote down vote up
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    float size = paint.getTextSize();
    paint.setTextSize(size * TEXT_SIZE_SCALE);
    paint.setTypeface(Typeface.MONOSPACE);

    if (fm != null && lines == null) {
        lines = new ArrayList<>();
        for (CharSequence c : mLines) {
            lines.addAll(measureTextLine(c, 0, c.length(), paint));
        }
    }

    paint.setTextSize(size);
    return mWidth;
}
 
Example 11
private static float calcTextSizeForRect(String _text, Paint _textPaint, RectF _rectBounds) {

        Matrix matrix = new Matrix();
        Rect textBoundsTmp = new Rect();
        //replace ones because for some fonts the 1 takes less space which causes issues
        String text = _text.replace('1', '0');

        //get current mText bounds
        _textPaint.getTextBounds(text, 0, text.length(), textBoundsTmp);

        RectF textBoundsTmpF = new RectF(textBoundsTmp);

        matrix.setRectToRect(textBoundsTmpF, _rectBounds, Matrix.ScaleToFit.CENTER);
        float values[] = new float[9];
        matrix.getValues(values);
        return _textPaint.getTextSize() * values[Matrix.MSCALE_X];
    }
 
Example 12
Source Project: Android-RTEditor   File: NumberSpan.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom,
                              CharSequence text, int start, int end, boolean first, Layout l) {

    Spanned spanned = (Spanned) text;
    if (!mIgnoreSpan && spanned.getSpanStart(this) == start) {
        // set paint
        Paint.Style oldStyle = p.getStyle();
        float oldTextSize = p.getTextSize();
        p.setStyle(Paint.Style.FILL);
        float textSize = determineTextSize(spanned, start, end, oldTextSize);
        p.setTextSize(textSize);
        mWidth = p.measureText(mNr + ".");

        // draw the number
        c.drawText(mNr + ".", x, baseline, p);

        // restore paint
        p.setStyle(oldStyle);
        p.setTextSize(oldTextSize);
    }
}
 
Example 13
@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout) {
    if (text instanceof Spanned &&
            ((Spanned) text).getSpanStart(this) == start) {
        float textSize = tp.getTextSize();
        if (textSize > p.getTextSize())
            tp.setTextSize(p.getTextSize());
        c.drawText(number, x + dir, baseline, tp);
        tp.setTextSize(textSize);
    }
}
 
Example 14
protected Float getCacheHeight(BaseDanmaku danmaku, Paint paint) {
    Float textSize = paint.getTextSize();
    Float textHeight = sTextHeightCache.get(textSize);
    if (textHeight == null) {
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        textHeight = fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading;
        sTextHeightCache.put(textSize, textHeight);
    }
    return textHeight;
}
 
Example 15
Source Project: simple-keyboard   File: TypefaceUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static int getCharGeometryCacheKey(final char referenceChar, final Paint paint) {
    final int labelSize = (int)paint.getTextSize();
    final Typeface face = paint.getTypeface();
    final int codePointOffset = referenceChar << 15;
    if (face == Typeface.DEFAULT) {
        return codePointOffset + labelSize;
    } else if (face == Typeface.DEFAULT_BOLD) {
        return codePointOffset + labelSize + 0x1000;
    } else if (face == Typeface.MONOSPACE) {
        return codePointOffset + labelSize + 0x2000;
    } else {
        return codePointOffset + labelSize;
    }
}
 
Example 16
private static int getCharGeometryCacheKey(final char referenceChar, final Paint paint) {
    final int labelSize = (int)paint.getTextSize();
    final Typeface face = paint.getTypeface();
    final int codePointOffset = referenceChar << 15;
    if (face == Typeface.DEFAULT) {
        return codePointOffset + labelSize;
    } else if (face == Typeface.DEFAULT_BOLD) {
        return codePointOffset + labelSize + 0x1000;
    } else if (face == Typeface.MONOSPACE) {
        return codePointOffset + labelSize + 0x2000;
    } else {
        return codePointOffset + labelSize;
    }
}
 
Example 17
Source Project: Indic-Keyboard   File: TypefaceUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static int getCharGeometryCacheKey(final char referenceChar, final Paint paint) {
    final int labelSize = (int)paint.getTextSize();
    final Typeface face = paint.getTypeface();
    final int codePointOffset = referenceChar << 15;
    if (face == Typeface.DEFAULT) {
        return codePointOffset + labelSize;
    } else if (face == Typeface.DEFAULT_BOLD) {
        return codePointOffset + labelSize + 0x1000;
    } else if (face == Typeface.MONOSPACE) {
        return codePointOffset + labelSize + 0x2000;
    } else {
        return codePointOffset + labelSize;
    }
}
 
Example 18
Source Project: satstat   File: GpsSnrView.java    License: GNU General Public License v3.0 4 votes vote down vote up
private void doInit(Context context) {
	DisplayMetrics metrics = context.getResources().getDisplayMetrics();
	density = metrics.density;
	
	gridStrokeWidth = Math.max(1, (int) (density));
	
	activePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	activePaint.setColor(Color.parseColor("#FF80CBC4")); // Teal 200
	activePaint.setStyle(Paint.Style.FILL);

	inactivePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	inactivePaint.setColor(Color.parseColor("#FFF44336")); // Red 500
	inactivePaint.setStyle(Paint.Style.FILL);

	gridPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	gridPaint.setColor(Color.parseColor("#FF424242")); // Gray 800
	gridPaint.setStyle(Paint.Style.STROKE);
	gridPaint.setStrokeWidth(gridStrokeWidth);

	gridPaintStrong = new Paint(gridPaint);
	gridPaintStrong.setColor(Color.parseColor("#FFFFFFFF"));

	gridPaintNone = new Paint(gridPaint);
	gridPaintNone.setColor(Color.parseColor("#00000000"));
	
	// FIXME style text properly
	labelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	labelPaint.setStyle(Paint.Style.FILL);
	labelPaint.setTextAlign(Paint.Align.CENTER);
	labelPaint.setColor(context.getResources().getColor(R.color.secondary_text_default_material_dark));
	labelPaint.setTextSize(context.getResources().getDimensionPixelSize(R.dimen.abc_text_size_small_material));
	
	/*
	int ap = R.style.TextAppearance_AppCompat_Medium;
	TypedArray appearance = null;
	appearance = context.getTheme().obtainStyledAttributes(ap, R.styleable.AppCompatTextView);
	if (appearance != null) {
		int n = appearance.getIndexCount();
		for (int i = 0; i < n; i++) {
			int attr = appearance.getIndex(i);
			
			switch (attr) {
			case R.styleable.TextAppearance_android_textColor:
				labelPaint.setColor(appearance.getColor(attr, labelPaint.getColor()));
				break;
				
			case R.styleable.TextAppearance_android_textSize:
				//labelPaint.setTextSize(appearance.getDimensionPixelSize(attr, (int) labelPaint.getTextSize()));
				break;
				
			case R.styleable.TextAppearance_android_typeface:
				//labelPaint.setTypeface(); // typefaceIndex = appearance.getInt(attr, -1);
				break;
				
			case R.styleable.TextAppearance_android_shadowColor:
			case R.styleable.TextAppearance_android_shadowDx:
			case R.styleable.TextAppearance_android_shadowDy:
			case R.styleable.TextAppearance_android_shadowRadius:
				// not yet implemented
				break;
			}
		}
		appearance.recycle();
	}
	*/

	/*
	 * Get the total height of the text. Note that this is not the same as getTextSize/setTextSize.
	 * Also note that the ascent is negative and descent is positive, hence descent - ascent will give us
	 * absolute text height (a positive number).
	 */
	textHeight = (int) Math.ceil(labelPaint.descent() - labelPaint.ascent());
	
	/*
	 * Height should be the same as two rows of small text plus a row of medium text. This is a
	 * rough approximation based on text sizes and the ratio between text size and actual height.
	 */
	preferredHeight = (int) (
			(2 * labelPaint.getTextSize() + context.getResources().getDimensionPixelSize(R.dimen.abc_text_size_medium_material))
			* textHeight / labelPaint.getTextSize()
			);
}
 
Example 19
public static SparseArray<ArrayList<String>> loadPages(String source, Paint textPaint, int visibleHeight, int visibleWidth, int intervalSize, int paragraphSize) {
    SparseArray<ArrayList<String>> pageArray = new SparseArray<>();
    List<String> lines = new ArrayList<>();
    if (source != null && source.length() > 0) {
        String[] split = source.split("\n");
        //剩余高度
        int rHeight = visibleHeight + intervalSize + paragraphSize;
        for (String paragraph : split) {
            boolean hasContent=false;
            //如果只有换行符,那么就不执行
            if (StringUtils.isBlank(paragraph)) continue;
            //重置段落
            paragraph = StringUtils.halfToFull("  " + paragraph + "\n");
            paragraph = StringUtils.trimBeforeReplace(paragraph, "  ");
            while (paragraph.length() > 0) {


                //测量一行占用的字节数
                int count = textPaint.breakText(paragraph, true, visibleWidth, null);
                String subStr = paragraph.substring(0, count);
                String trim = subStr.trim();
                if (trim.length()>0&&!trim.equals("\n") && !trim.equals("\r\n")&&!StringUtils.isBlank(trim)) {
                    //重置剩余距离
                    rHeight -= (textPaint.getTextSize() + intervalSize);

                    //达到行数要求,创建Page
                    if (rHeight < 0) {
                        //创建Page
                        pageArray.put(pageArray.size(), new ArrayList<>(lines));
                        //重置Lines
                        lines.clear();
                        rHeight = visibleHeight;
                        continue;
                    }
                    //将一行字节,存储到lines中
                    lines.add(subStr);
                    hasContent=true;
                }


                //裁剪
                paragraph = paragraph.substring(count);
            }

            if (lines.size() > 0&&hasContent) {
                rHeight -= paragraphSize;
            }
        }

        if (lines.size() != 0) {
            pageArray.put(pageArray.size(), new ArrayList<>(lines));
            //重置Lines
            lines.clear();

        }
    }
    return pageArray;
}
 
Example 20
Source Project: SimplifySpan   File: CustomLabelSpan.java    License: MIT License 4 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) {
    float finalUnitHeight = bottom - top;
    float bgTop = bottom - finalUnitHeight;
    if (isClickable && isSelected && pressBgColor != 0) {
        // click background
        paint.setColor(pressBgColor);
        canvas.drawRect(x, bgTop, x + mFinalWidth, bgTop + finalUnitHeight, paint);
    } else {
        // normal background
        if (mBgColor != 0) {
            paint.setColor(mBgColor);
            canvas.drawRect(x, bgTop, x + mFinalWidth, bgTop + finalUnitHeight, paint);
        }
    }

    float labelTextSize = mSpecialLabelUnit.getLabelTextSize();
    if (labelTextSize > 0 && labelTextSize != paint.getTextSize()) {
        paint.setTextSize(labelTextSize);
    }

    float newStartY = y;
    int newTextY = y;

    switch (mSpecialLabelUnit.getGravity()) {
        case SpecialGravity.TOP:
            newStartY -= (mLineTextHeight - mLineTextBaselineOffset);
            newTextY -= (mLineTextHeight - mSpecialTextHeight - (mLineTextBaselineOffset - mSpecialTextBaselineOffset) - mPaddingTop);
            break;
        case SpecialGravity.CENTER:
            newStartY -= (mLineTextHeight / 2 + mFinalHeight / 2 - mLineTextBaselineOffset);
            newTextY -= (mLineTextHeight / 2 - mSpecialTextHeight / 2 - (mLineTextBaselineOffset - mSpecialTextBaselineOffset));
            break;
        case SpecialGravity.BOTTOM:
            newStartY -= mFinalHeight - mLineTextBaselineOffset;
            newTextY -= mPaddingBottom - (mLineTextBaselineOffset - mSpecialTextBaselineOffset);
            break;
    }

    if (isDrawBitmap) {
        canvas.drawBitmap(mBitmap, x, newStartY, paint);
    } else {
        paint.setColor(mSpecialLabelUnit.getLabelBgColor());
        if (mLabelBgRadius > 0) {
            bgRect.top = newStartY;
            bgRect.bottom = newStartY + mFinalHeight;
            bgRect.left = x;
            bgRect.right = x + mFinalWidth;
            if (mSpecialLabelUnit.isShowBorder()) {
                float borderSize = mSpecialLabelUnit.getBorderSize();
                // draw background
                canvas.drawRect(x, newStartY, x + mFinalWidth, newStartY + mFinalHeight, paint);

                // draw border
                paint.setColor(mSpecialLabelUnit.getLabelBgBorderColor());
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(borderSize);
                canvas.drawRoundRect(bgRect, mLabelBgRadius, mLabelBgRadius, paint);

                // recover paint
                paint.setStyle(Paint.Style.FILL);
            } else {
                // draw background
                canvas.drawRoundRect(bgRect, mLabelBgRadius, mLabelBgRadius, paint);
            }
        } else {
            // draw background
            canvas.drawRect(x, newStartY, x + mFinalWidth, newStartY + mFinalHeight, paint);

            if (mSpecialLabelUnit.isShowBorder()) {
                // draw border
                paint.setColor(mSpecialLabelUnit.getLabelBgBorderColor());
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(mSpecialLabelUnit.getBorderSize());
                canvas.drawRect(x, newStartY, x + mFinalWidth, newStartY + mFinalHeight, paint);

                // recover paint
                paint.setStyle(Paint.Style.FILL);
            }
        }
    }

    paint.setColor(mSpecialLabelUnit.getLabelTextColor());
    float newTextX;
    if (isLabelBgCenter) {
        newTextX = x + Math.round(mFinalWidth / 2f - mSpecialTextWidth / 2f);
    } else {
        newTextX = x + mPaddingLeft;
    }

    if (mSpecialLabelUnit.isTextBold())
        paint.setFakeBoldText(true);

    if (mSpecialLabelUnit.isTextItalic())
        paint.setTypeface(Typeface.create((String) null, Typeface.ITALIC));

    canvas.drawText(mSpecialText, newTextX, newTextY, paint);
}