Drawing an outlined RoundRect

by TheFluffyOne » Thu, 08 Jul 2010 02:06:33 GMT

Sponsored Links

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


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.


        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        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,
        float[] innerR = new float[] { radius, radius, radius, radius,
radius, radius, radius, radius };

        ShapeDrawable back = new ShapeDrawable(new RoundRectShape(outerR,
null, null));
        back.setBounds(x - border, y + fm.ascent - border, x + textWidth +
border, y + fm.descent + border);

        ShapeDrawable outline = new ShapeDrawable(new RoundRectShape(outerR,
inset, innerR));
        outline.setBounds(x - border, y + fm.ascent - border, x + textWidth +
border, y + fm.descent + border);

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


Drawing an outlined RoundRect

by blcooley » Thu, 08 Jul 2010 02:33:51 GMT

 I don't know the answer to your question directly, but you can set the
stroke of a shape drawable specified in xml. I don't see a method for
that in the API docs, but it doesn't seem like you need both the back
and outline ShapeDrawables if you go that route. Here's a link:

 http://developer.android.com/guide/topics/resources/drawable-resource.html #Shape

Good luck!

Best regards,
Brian Cooley


Sponsored Links

Drawing an outlined RoundRect

by blcooley » Thu, 08 Jul 2010 02:37:31 GMT

 Or, maybe you could try a GradientDrawable? Looks like that's what you
are after. It has a setStroke method.


Best regards,
Brian Cooley


Other Threads

1. Any Harm in Tying GPS listening to onWindowFocusChanged()

So I have received complaints from users that they think our app was
using GPS why they were not using the app because the lock screen was
displaying the GPS icon. I previously within a common root activity,
registered for GPS events in the onResume and deregistered in the
OnPause(). Turns out that the lock screens will resume the underlying
app that will be in the foreground after unlock (perfectly

I wanted to do something about the customers feedback though because I
know not all users are really techies so I since I couldn't provide an
answer that wasn't of just the nature "trust me it's not" I wanted to
see if I could at least calm their nerves. I put the register and
deregister hooks into the onWindowFocusChanged of the activities and
while it fixes the lock screen problems, I am starting to notice some
lags. Is this to be expected? Like I am seeing my app take much more
time in GPS than previously. It seems like the event is not always
guaranteed to be called immediately for when the app loses focus.

Is this correct? I noticed the google maps application seems to
register and deregister on resume and pause since they have the same
lock screen issue. Has anyone dealt with this before? I am going to
switch it back to onResume and onPause for the time being.


2. YouTube player terhenti

Para suhu dan master, mohon petunjuknya
Saya pakai Huawei Aviator, pertama kali pakai dengan provider Axis
pada saat saya pke youtube tdk pernah bermasalah pada saat streaming.
kemudian saya berganti ke Telkomsel dan menginstall bbrp aplikasi
tambahan,nah pada saat saya pakai youtubenya pada saat memutarkan
videonya tiba2 saja terhenti biasanya di 3detik pertama,kemudian
videonya terhenti. kalaupun jalan lagi tapi tetap menunggu lbh dr
apakah memory internal berpengaruh terhadap youtube player tsb?atau
mohon petunjuk para suhu dan master

"Indonesian Android Community [id-android]" 

3. Programming a target in an Android Application

4. Webview not fit to screen

5. Data-at Rest Encryptio

6. Sqlite segfault anyone ever see this??

7. Dev/Debug on same device that has Marketplace Version of App