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. apk pasar malem

Gan,
Koq belakangan download dari pasar malem bnyk yg error parsing package apk nya 
ya?




Willy Mularto
F300HD+MR18DE (NLC1725)







-- 
"

2. Help me in detecting sound pulses (basically frequency detection)

Hi all,
I am soumyajit. Presently working on a App that needs sound pulse
detection ( basically reception and detection of frequency). Can
anybody help me? there are no codes in the web..and neither I found
any libraries...
Thanks,
Soumyajit Roy

-- 

3. CameraService interface for StageFright

4. Internet Sharing .

5. SE X10mini PRO message bug?

6. USB Network Card

7. KERNEL 3.3 WTA: Saran untuk Custom Rom untuk Gingerbread