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

The following are Jave code examples for showing how to use getTextSize() 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: CustomAlignmentSpan.java   View Source Code Vote up 6 votes
@Override public void draw(Canvas canvas, CharSequence text, int start, int end,
                           float defaultX, int top, int defaultY, int bottom, Paint paint) {
    float x = 0;
    float y = 0;
    switch (position) {
        case RIGHT_TOP:
            x = canvas.getWidth() - paint.measureText(text, start, end);
            y = paint.getTextSize();
            break;
        case RIGHT_CENTER:
            x = canvas.getWidth() - paint.measureText(text, start, end);
            y = canvas.getHeight() / 2;
            break;
        case RIGHT_BOTTOM:
            x = canvas.getWidth() - paint.measureText(text, start, end);
            y = canvas.getHeight() - paint.getFontMetricsInt().bottom;
            break;
    }
    if (color != -1)
        paint.setColor(color);
    canvas.drawText(text, start, end, x, y, paint);
}
 
Example 2
Project: android_ui   File: MonthView.java   View Source Code Vote up 6 votes
/**
 * Invoked to draw first letters of days in week from the specified <var>top</var> position.
 * <p>
 * Default implementation draws day letters in one row on the specified <var>canvas</var>.
 *
 * @param canvas The canvas on which to draw day letters.
 * @param top    Top position from which to start the drawing. This is the position returned from
 *               {@link #onDrawTitle(Canvas, float, Paint)} method {@code +} {@link #getDayLettersOffsetVertical()}.
 * @param paint  Paint for day letters ready to be used for drawing.
 * @return Updated top position from which following graphic components (day numbers) of this view
 * will be drawn.
 * @see #onDrawDayNumbers(Canvas, float, Paint)
 */
protected float onDrawWeekDayLetters(@NonNull Canvas canvas, float top, @NonNull Paint paint) {
	/**
	 * We will draw 7 columns for each day in week.
	 */
	int dayIndex = mFirstDayOfWeek - 1;
	paint.getTextBounds(Integer.toString(MAX_DAY_IN_MONTH), 0, 2, TEXT_BOUNDS);
	float left = getPaddingLeft() + TEXT_BOUNDS.width() / 2f;
	top += (int) paint.getTextSize();
	for (int i = 0; i < DAYS_IN_WEEK; i++) {
		final String dayLetter = DAY_LETTERS[dayIndex];
		canvas.drawText(dayLetter, 0, 1, left, top, paint);
		left += mSpacingHorizontal;
		if (++dayIndex >= DAYS_IN_WEEK) {
			dayIndex = 0;
		}
	}
	return top;
}
 
Example 3
Project: greendao_expand   File: CircleProgressView.java   View Source Code Vote up 6 votes
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 4
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 5
Project: SmartMath   File: FlatChart.java   View Source Code Vote up 6 votes
/**
 * Draw a single line text, x, y is in the middle of the line.
 */
public void draw1LineTextAnchorMid(Canvas canvas, String text, double x, double y,
		Paint paint, com.cyzapps.VisualMFP.Color color, double dTextSize, double dRotateDegree) {
	
	float fRotateDegree = (float)dRotateDegree, fX = (float)x, fY = (float)y;
	if (dRotateDegree != 0)	{
		canvas.rotate(fRotateDegree, fX, fY);
	}
	int nPaintOriginalColor = paint.getColor();
	float fPaintOriginalTxtSize = paint.getTextSize();
	if (color != null)	{
		paint.setColor(color.getARGB());
	}	// otherwise, use paint's color.
	paint.setTextSize((float)dTextSize);
	float fWidth = paint.measureText(text);
	canvas.drawText(text, fX - fWidth/2, fY + (float)dTextSize/2 - paint.descent(), paint);
	paint.setTextSize(fPaintOriginalTxtSize);
	paint.setColor(nPaintOriginalColor);
	if (dRotateDegree != 0) {
		canvas.rotate(-fRotateDegree, fX, fY);
	}
}
 
Example 6
Project: SmartMath   File: OGLChart.java   View Source Code Vote up 6 votes
public Rect calcTextBounds(String text, Paint paint, double dTextSize, double dSpaceBtwTxtLines)	{
    float fPaintOriginalTxtSize = paint.getTextSize();
    Align alignOriginal = paint.getTextAlign();
    Rect rectTxtBox = new Rect(); // rectTxtBox gives original text box and returns the actual text box
    if (text == null)	{
    	return rectTxtBox;	// null protection.
    }
    paint.setTextSize((float)dTextSize);
    paint.setTextAlign(Align.LEFT);
    String[] lines = text.split("\n");
    for (int i = 0; i < lines.length; ++i) {
    	int nTxtWidth = (int)Math.ceil(paint.measureText(lines[i]));
        if (i == 0)    {
            rectTxtBox.left = 0;
            rectTxtBox.right = nTxtWidth;	//rect.right;
            rectTxtBox.top = 0;
            rectTxtBox.bottom = (int)Math.ceil(-paint.ascent() + paint.descent());//rect.height();
        } else    {
            rectTxtBox.right = Math.max(rectTxtBox.right, nTxtWidth);
            rectTxtBox.bottom = (int) (rectTxtBox.bottom + (int)Math.ceil(-paint.ascent() + paint.descent()) + dSpaceBtwTxtLines);
        }
    }
    paint.setTextAlign(alignOriginal);
    paint.setTextSize(fPaintOriginalTxtSize);
    return rectTxtBox;
}
 
Example 7
Project: qmui   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 8
Project: simple-keyboard   File: TypefaceUtils.java   View Source Code Vote up 5 votes
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 9
Project: Markwon   File: SpannableTheme.java   View Source Code Vote up 5 votes
public void applyCodeTextStyle(@NonNull Paint paint) {

        if (codeTextColor != 0) {
            paint.setColor(codeTextColor);
        }

        // custom typeface was set
        if (codeTypeface != null) {

            paint.setTypeface(codeTypeface);

            // please note that we won't be calculating textSize
            // (like we do when no Typeface is provided), if it's some specific typeface
            // we would confuse users about textSize
            if (codeTextSize != 0) {
                paint.setTextSize(codeTextSize);
            }

        } else {
            paint.setTypeface(Typeface.MONOSPACE);
            final float textSize;
            if (codeTextSize != 0) {
                textSize = codeTextSize;
            } else {
                textSize = paint.getTextSize() * CODE_DEF_TEXT_SIZE_RATIO;
            }
            paint.setTextSize(textSize);
        }
    }
 
Example 10
Project: Tusky   File: CustomEmojiHelper.java   View Source Code Vote up 5 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) {
    if (imageDrawable == null) return;
    canvas.save();

    int emojiSize = (int) (paint.getTextSize() * 1.1);
    imageDrawable.setBounds(0, 0, emojiSize, emojiSize);

    int transY = bottom - imageDrawable.getBounds().bottom;
    transY -= paint.getFontMetricsInt().descent/2;
    canvas.translate(x, transY);
    imageDrawable.draw(canvas);
    canvas.restore();
}
 
Example 11
Project: AOSP-Kayboard-7.1.2   File: TypefaceUtils.java   View Source Code Vote up 5 votes
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 12
Project: live_master   File: SimpleTextCacheStuffer.java   View Source Code Vote up 5 votes
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 13
Project: SmartMath   File: FlatChart.java   View Source Code Vote up 5 votes
/**
 * Draw a multiple lines text.
 */
public void drawText(Canvas canvas, String text, double x, double y,
		Paint paint, com.cyzapps.VisualMFP.Color color, double dTextSize, double dRotateDegree) {
	
	float fRotateDegree = (float)dRotateDegree, fX = (float)x, fY = (float)y;
	if (dRotateDegree != 0)	{
		canvas.rotate(fRotateDegree, fX, fY);
	}
	int nPaintOriginalColor = paint.getColor();
	float fPaintOriginalTxtSize = paint.getTextSize();
	if (color != null)	{
		paint.setColor(color.getARGB());
	}	// otherwise, use paint's color.
	paint.setTextSize((float)dTextSize);
	String[] lines = text.split("\n");
	Rect rect = new Rect();
	int yOff = 0;
	for (int i = 0; i < lines.length; ++i) {
		canvas.drawText(lines[i], fX, fY + paint.getFontMetrics().leading + paint.ascent() + yOff, paint);
		paint.getTextBounds(lines[i], 0, lines[i].length(), rect);
		yOff = (int) (yOff + rect.height() + mdSpaceBtwTxtLines); // space between lines
	}
	paint.setTextSize(fPaintOriginalTxtSize);
	paint.setColor(nPaintOriginalColor);
	if (dRotateDegree != 0) {
		canvas.rotate(-fRotateDegree, fX, fY);
	}
}
 
Example 14
Project: SmartMath   File: FlatChart.java   View Source Code Vote up 5 votes
public void draw(Canvas canvas, double x, double y, double width, double height, Paint paint) {
	drawBackground(canvas, x, y, width, height, paint, null);
	float fPaintOriginalTxtSize = paint.getTextSize();
	paint.setTextSize((float)(width/2));
	draw1LineTextAnchorMid(canvas, mstrChartTitle, x / 2, y + mdSmallSize, paint, null, mdSmallSize, 0);
       drawButtons(canvas, x, y, width, height, paint);
	paint.setTextSize(fPaintOriginalTxtSize);
}
 
Example 15
Project: FriendBook   File: StringAdapter.java   View Source Code Vote up 4 votes
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 16
Project: PSNine   File: DrawTableLinkSpan.java   View Source Code Vote up 4 votes
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    int width = (int) paint.measureText(mTableLinkText, 0, mTableLinkText.length());
    mTextSize = paint.getTextSize();
    return width;
}
 
Example 17
Project: SDHtmlTextView   File: DrawTableLinkSpan.java   View Source Code Vote up 4 votes
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    int width = (int) paint.measureText(mTableLinkText, 0, mTableLinkText.length());
    mTextSize = paint.getTextSize();
    return width;
}
 
Example 18
Project: SmartMath   File: OGLChart.java   View Source Code Vote up 4 votes
/**
 * Draw a multiple lines text bitmap. The size of the bitmap is returned in rectTxtBox
 */
public Bitmap createTextBitmap(String text, Paint paint, com.cyzapps.VisualMFP.Color color, double dTextSize, Rect rectOrigTxtBox) {
	if (text == null)	{
		text = "";	// null pointer protection.
	}
    // First of all, calculate text bound
    int nPaintOriginalColor = paint.getColor();
    if (color != null)    {
        paint.setColor(color.getARGB());
    }    // otherwise, use paint's color.
    float fOriginalTxtSize = paint.getTextSize();
    paint.setTextSize((float) dTextSize);
    
    String[] lines = text.split("\n");
    double dSpaceBtwTxtLines = dTextSize/5.0;
    Rect rectTxtBox = calcTextBounds(text, paint, dTextSize, dSpaceBtwTxtLines);
    rectOrigTxtBox.set(rectTxtBox);
    Rect rectAdjustBox = new Rect();
    rectAdjustBox.left = 0;
    rectAdjustBox.top = 0;
    // need 2 ** integer power otherwise some devices cannot show.
    if (rectTxtBox.width() <= 0)	{
    	rectAdjustBox.right = 2;	// was 0, but createBitmap would fail if 0
    } else	{
    	rectAdjustBox.right = getNextHighestPO2(rectTxtBox.width());
    }
    if (rectTxtBox.height() <= 0)	{
    	rectAdjustBox.bottom = 2;	// was 0, but createBitmap would fail if 0
    } else	{
    	rectAdjustBox.bottom = getNextHighestPO2(rectTxtBox.height());
    }
   
    // Create an empty, mutable bitmap
    Bitmap bitmap = Bitmap.createBitmap(rectAdjustBox.width(), rectAdjustBox.height(), Bitmap.Config.ARGB_8888);
    // get a canvas to paint over the bitmap
    Canvas canvas = new Canvas(bitmap);
    bitmap.eraseColor(0);
    // get a background image from resources
    // note the image format must match the bitmap format

    Drawable background = new ColorDrawable(0x00000000);
    background.setBounds(0, 0, rectAdjustBox.width(), rectAdjustBox.height());
    background.draw(canvas); // draw the background to our bitmap
   
    float fX = 0, fY = /*rectTxtBox.height();*/-(float)paint.ascent();    // drawText's y parameter is the baseline of the character. Note that paint.ascent() is negative.
    int yOff = 0;   
    for (int i = 0; i < lines.length; ++i) {
        canvas.drawText(lines[i], fX, fY + yOff, paint);
        yOff = (int) (yOff + (int)Math.ceil(-paint.ascent() + paint.descent()) + dSpaceBtwTxtLines); // space between lines
    }

    paint.setTextSize(fOriginalTxtSize);
    paint.setColor(nPaintOriginalColor);
   
    return bitmap;
}
 
Example 19
Project: android_ui   File: MonthView.java   View Source Code Vote up 3 votes
/**
 * Invoked to draw title of this month view from the specified <var>top</var> position.
 * <p>
 * Default implementation draws the name of month and current year number for the date specified
 * for this month view side by side (horizontally) on the specified <var>canvas</var>.
 *
 * @param canvas The canvas on which to draw the title (month name + year number).
 * @param top    Top position from which to start the drawing. By default a value from
 *               {@link #getPaddingTop()} is used.
 * @param paint  Paint for title text ready to be used for drawing.
 * @return Updated top position from which following graphic components (week day letters, day numbers)
 * of this view will be drawn.
 * @see #onDrawWeekDayLetters(Canvas, float, Paint)
 * @see #onDrawDayNumbers(Canvas, float, Paint)
 */
protected float onDrawTitle(@NonNull Canvas canvas, float top, @NonNull Paint paint) {
	final CharSequence titleText = mMonthName + " " + Integer.toString(mYear);
	top += (int) paint.getTextSize();
	canvas.drawText(
			titleText,
			0,
			titleText.length(),
			mWidth / 2,
			top,
			paint
	);
	return top;
}
 
Example 20
Project: android_ui   File: TextAppearance.java   View Source Code Vote up 3 votes
/**
 * Updates text size setting for the given <var>paint</var> to the specified one.
 *
 * @param paint    The paint to be updated.
 * @param textSize The desired text size in floating pixels.
 * @return {@code True} if paint's text setting has changed, {@code false} otherwise.
 * @see Paint#setTextSize(float)
 */
public static boolean updatePaintTextSize(@NonNull Paint paint, float textSize) {
	if (paint.getTextSize() != textSize) {
		paint.setTextSize(textSize);
		return true;
	}
	return false;
}