Paint getTextBounds - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Paint getTextBounds method.

This code example shows how to use the following methods:getFontMetrics, getTextBounds, setColor, setTextSize
		mLabel = string;
 
		mWidth = drawable.getIntrinsicWidth();
		mHeight = drawable.getIntrinsicHeight();
 
		mTextPaint = new Paint( Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG | Paint.DEV_KERN_TEXT_FLAG | Paint.HINTING_ON );
		mTextPaint.setColor( Color.WHITE );
		mBounds = new Rect();
		mMetrics = new FontMetrics();
 
		onTextBoundsChanged(); 
	} 
 
	public void setTitle( final String value ) {
		mLabel = value;
		invalidateSelf(); 
		onTextBoundsChanged(); 
	} 
 
	public final String getTitle() {

8
CodeRank
This code example shows how to use the following methods:getTextBounds, setAntiAlias, setColor, setTextSize
	} 
 
	@Override 
	protected synchronized void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint textPaint = new Paint();
		textPaint.setAntiAlias(true);
		textPaint.setColor(textColor);
		textPaint.setTextSize(textSize);
		Rect bounds = new Rect();
		textPaint.getTextBounds(text, 0, text.length(), bounds);
		int x = getWidth() / 2 - bounds.centerX();
		int y = getHeight() / 2 - bounds.centerY();
		canvas.drawText(text, x, y, textPaint);
	} 
 
	public String getText() {
		return text;
	} 
 
7
CodeRank
This code example shows how to use the following methods:getTextBounds, setAntiAlias, setColor, setStyle, setTextSize
This code example shows how to use the following methods:getTextBounds, setColor, setShadowLayer, setTextSize
			bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
		} 
		bitmap = bitmap.copy(bitmapConfig, true);
 
		Canvas canvas = new Canvas(bitmap);
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.BLACK);
		paint.setTextSize((int) (15 * scale));
		paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
 
		Rect bounds = new Rect();
		paint.getTextBounds(gText, 0, gText.length(), bounds);
		int x = (bitmap.getWidth() - bounds.width()) / 2;
		int y = (bitmap.getHeight() + bounds.height()) * 5/12; // At 5/12 from the top so it stays on the center
 
		canvas.drawText(gText, x, y, paint);
 
		return bitmap;
	} 
} 
7
CodeRank
This code example shows how to use the following methods:ascent, descent, getFontSpacing, getTextBounds, getTypeface
 
  public void getFontMetrics(int[] metrics) {
    Rect bounds = new Rect();
    char[] m = new char[1];
    m[0] = 'M';
    paint.getTextBounds(m, 0, 1, bounds);
 
    metrics[0] = Math.round(paint.descent() - paint.ascent());
    metrics[1] = paint.getTypeface().getStyle();
    metrics[2] = Math.round(-paint.ascent());
    metrics[3] = bounds.height();
    metrics[4] = Math.round(paint.getFontSpacing());
  } 
 
  public int[] getTextBounds(String text) {
    /* we cannot simply use getTextBounds() here, because xcsoar will not 
     * know where the baseline of the text is inside the texture 
     */ 
    extent[0] = Math.round(paint.measureText(text, 0, text.length()));
    extent[1] = metrics.descent - metrics.ascent;
6
CodeRank
This code example shows how to use the following methods:getTextBounds, measureText, setTextSize, setTypeface
    public void drawText(Canvas canvas, String text) {
        if (text == null) {
            return; 
        } 
        text = text.toUpperCase();
        mPaint.setTextSize(sTextSize);
        mPaint.setTypeface(Typeface.DEFAULT_BOLD);
        float textWidth = mPaint.measureText(text);
        mPaint.getTextBounds(text, 0, text.length(), mTextBounds);
        int x = (int) (canvas.getWidth() - textWidth - sMargin);
        int y = canvas.getHeight() - sMargin;
        canvas.drawText(text, x, y, mPaint);
    } 
 
    @Override 
    public void onDraw(Canvas canvas) {
        canvas.drawColor(mBackgroundColor);
        if (mAction != null) {
            mPaint.reset();
            mPaint.setAntiAlias(true);
6
CodeRank
This code example shows how to use the following methods:getFontMetrics, getTextBounds, setColor, setTextSize, setTypeface
        createFont(text, typeface, fontSize);
    } 
 
    protected Bitmap createFontImage(String text, Typeface typeface, int fontSize) {
 
        paint.setTextSize(fontSize);
        paint.setTypeface(typeface);
        paint.getTextBounds(text, 0, text.length(), bounds);
 
        FontMetrics fontMetrics = paint.getFontMetrics();
 
        final int IMAGE_WIDTH = Math.max(1, bounds.width());
        final int IMAGE_HEIGHT = (int) Math.max(//
                Math.abs(fontMetrics.ascent) + Math.abs(fontMetrics.descent),
                // 
                (Math.abs(fontMetrics.top) + Math.abs(fontMetrics.bottom)));
        Bitmap bitmap = Bitmap.createBitmap(IMAGE_WIDTH, IMAGE_HEIGHT, Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Graphics graphics = new Graphics();
        graphics.setCanvas(canvas);

7
CodeRank
This code example shows how to use the following methods:getTextBounds, setAntiAlias, setColor, setTextAlign, setTextSize
        canvas.drawBitmap(image, left, top, p);
        drawMessage(canvas, p);
    } 
 
    private void drawMessage(Canvas canvas, Paint p) {
        p.setTextSize(BIG_FONT_SIZE);
        p.setTextAlign(Paint.Align.CENTER);
        p.setTypeface(Typeface.DEFAULT_BOLD);
        p.setAntiAlias(true);
        Rect bounds = mRect;
        p.getTextBounds(msg, 0, msg.length(), bounds);
        int x = width / 2;
        int y = height - bounds.height() / 2;
        p.setColor(Color.WHITE);
        canvas.drawText(msg, x, y, p);
    } 
 
    public void update() { 
        if (image == null || width == -1 || height == -1)
            return; 
7
CodeRank
This code example shows how to use the following methods:getTextBounds, setAntiAlias, setColor, setShadowLayer, setTextSize
                                final Color color,
                                final Color shadowColor,
                                final IImageListener listener,
                                final boolean autodelete) {
      //final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
      final Paint paint = new Paint();
      paint.setAntiAlias(true);
      paint.setColor(toAndroidColor(color));
      paint.setTextSize((fontSize * 3) / 2);
      if (shadowColor != null) {
         paint.setShadowLayer(1f, 2f, 2f, toAndroidColor(shadowColor));
      } 
 
      final Rect textBounds = new Rect();
      paint.getTextBounds(label, 0, label.length(), textBounds);
      int width = textBounds.width();
      int height = textBounds.height();
      if (shadowColor != null) {
         width += 2;
         height += 2;
6
CodeRank
This code example shows how to use the following methods:getTextBounds, setAntiAlias, setColor, setTextAlign
		float radiansSecondary = (float) (360.0f * mSecondary / mMax);
		float start = 270.0f;
		RectF ovalPrimary = new RectF(0, 0, w, h);
		RectF ovalSecondary = new RectF(w/4, h/4, 3 * w / 4, 3 * h / 4);
		 
		Paint p = new Paint();
		p.setAntiAlias(true);
		 
		p.setColor(0xAA00FF00);
		if(mPrimary > 0) canvas.drawArc(ovalPrimary, start, radiansPrimary, true, p);
		 
		p.setColor(0xAA0000FF);
		if(mSecondary > 0) canvas.drawArc(ovalSecondary, start, radiansSecondary, true, p);
		 
		 
		if(mShowValue) {
			p.setColor(Color.WHITE);
			p.setTextAlign(Align.CENTER);
			String text = (mPrimary > 0 || mAlternateText == null)
					? String.format("%1.1f", mPrimary)

6
CodeRank

Related Paint getTextBounds Questions & Answers:

getTextBounds Method Overview
Return in bounds (allocated by the caller) the smallest rectangle that encloses all of the characters, with an implied origin at (0,0).
See Code Examples for other Android Paint Methods: