Drawing an outlined RoundRect

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


Sponsored Links
 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);

--



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.

 http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html 

Best regards,
Brian Cooley




--



Other Threads

1. Node creation

Hi,

In Streaming Manager node, nodes are created to receive rtsp and rtp
packets.
Please help me in understanding how to create a node to receive rtp packets
from server.


-Regards,

--~--~---------~--~----~------------~-------~--~----~

2. Skia Graphics Library

Can anybody help me with using the Skia Graphis Library for
implementation of various controls or even if someone can help me find
the .h files related to SKIA in myandroid/

It's a bit urgent...

Regards,
Sunit
--~--~---------~--~----~------------~-------~--~----~

3. how to build and step through HelloAndroid in the debugger using the open source framework?

4. onActivityResult returns RESULT_CANCELED immediately :(

5. Dalvik spends 7.4% of its time garbage collecting, Android UI spends 7.4% of its time unresponsive

6. How to pass data when switching between activities in an application

7. Dalvik spends 7.4% of its time garbage collecting, Android UI spends 7.4% of its time unresponsive