Saturday, July 3, 2010

[android-developers] Drawing an outlined RoundRect

Hi,

I have a method that, when passed some text, draws an outlined round
rectangle with that text inside, viz:

http://ubergeek.org.uk/images/roundrect.png

At the moment I'm making two draw calls in order to draw this outlined
round rectangle, and see slight bulging at the corners that I assume
is due to the use of the same radius for the corners for both the
inner and outer rectangle. Is there a better way? Main code is below.

Thanks!


Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(0xFFFFFFFF);
paint.setTextSize(fontsize);
paint.setTypeface(Typeface.SANS_SERIF);

Rect textBounds = new Rect();
paint.getTextBounds(nodeText, 0, nodeText.length(), textBounds);
FontMetricsInt fm = paint.getFontMetricsInt();
int textWidth = textBounds.width();

float[] outerR = new float[] { radius, radius, radius, radius,
radius, radius, radius, radius };
RectF inset = new RectF(linewidth, linewidth, linewidth,
linewidth);
float[] innerR = new float[] { radius, radius, radius, radius,
radius, radius, radius, radius };

ShapeDrawable back = new ShapeDrawable(new RoundRectShape(outerR,
null, null));
back.getPaint().setColor(0xFFB0B070);
back.setBounds(x - border, y + fm.ascent - border, x + textWidth +
border, y + fm.descent + border);
back.draw(canvas);

ShapeDrawable outline = new ShapeDrawable(new RoundRectShape(outerR,
inset, innerR));
outline.getPaint().setColor(0xFFFFFFB0);
outline.setBounds(x - border, y + fm.ascent - border, x + textWidth +
border, y + fm.descent + border);
outline.draw(canvas);

canvas.drawText(nodeText, x, y, paint);

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment